Allow Control in Cmd Buttons

  • Thread starter ladybug via AccessMonster.com
  • Start date
L

ladybug via AccessMonster.com

I have a form called frm_ope, that can be opened by two different buttons. I
want one button to allow users only to view the data (no add, no delete, no
edit). The other button I want the users to be able to add, delete, and edit.


I know I cannot set the properties to frm_ope, to allow additions, deletions,
and edits. Then it would not work for the button where I do not want this
allowed. What code can I use in the buttons to determine on the open of the
form, whether the user is allowed to make additions, deletions, and edits?

Right now in the view only button I have this code:
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmView"

stLinkCriteria = "[Number]=" & Me![cboSearch]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command15_Click:
Exit Sub

Err_Command15_Click:
MsgBox Err.Description
Resume Exit_Command15_Click

End Sub


Once I have put the code in, what should I do in the forms properties for
Allow additions, deletions, edits?
 
A

Allen Browne

After your OpenForm line, add:

With Form(stDocName)
.AllowEdits = false
.AllowDeletions = False
.AllowAdditions = False
End With
 
L

ladybug via AccessMonster.com

Thank you for your fast response. I added your piece to my code. It now
looks like this:
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmView"

stLinkCriteria = "[Number]=" & Me![cboSearch]
DoCmd.OpenForm stDocName, , , stLinkCriteria
With Form(stDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With

However, when I click the button the form opens and I get error: Database
can't find the field 'frmView' referred to in your expression. It also still
allows to edit.

Allen said:
After your OpenForm line, add:

With Form(stDocName)
.AllowEdits = false
.AllowDeletions = False
.AllowAdditions = False
End With
I have a form called frmview, that can be opened by two different buttons.
I
[quoted text clipped - 32 lines]
Once I have put the code in, what should I do in the forms properties for
Allow additions, deletions, edits?
 
C

Clif McIrvin

Try

With Forms(stDocName)

instead. (Note Forms, not Form)

--
Clif

ladybug via AccessMonster.com said:
Thank you for your fast response. I added your piece to my code. It
now
looks like this:
On Error GoTo Err_Command15_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmView"

stLinkCriteria = "[Number]=" & Me![cboSearch]
DoCmd.OpenForm stDocName, , , stLinkCriteria
With Form(stDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With

However, when I click the button the form opens and I get error:
Database
can't find the field 'frmView' referred to in your expression. It
also still
allows to edit.

Allen said:
After your OpenForm line, add:

With Form(stDocName)
.AllowEdits = false
.AllowDeletions = False
.AllowAdditions = False
End With
I have a form called frmview, that can be opened by two different
buttons.
I
[quoted text clipped - 32 lines]
Once I have put the code in, what should I do in the forms
properties for
Allow additions, deletions, edits?
 
L

ladybug via AccessMonster.com

That worked! thank you!
One last question. THere are subforms on this form. You cannot edit the
form,but it still allows user to edit, add, delete on the subforms. How do I
add the subforms in this code?

Thank you again for all your help!

Clif said:
Try

With Forms(stDocName)

instead. (Note Forms, not Form)

--
Clif
Thank you for your fast response. I added your piece to my code. It
now
[quoted text clipped - 35 lines]
 
C

Clif McIrvin

ladybug via AccessMonster.com said:
That worked! thank you!
One last question. THere are subforms on this form. You cannot edit
the
form,but it still allows user to edit, add, delete on the subforms.
How do I
add the subforms in this code?

Let's see if i can get this right ... I've seen psotings about how to
reference subforms but not yet used the technique very much.

Essentially, you need to set the same properties for each subform, so
you need code to iterate through each subform and set the properties.

I believe the syntax would be identical, you simply provide the subform
name. You need to make sure the (sub)form is loaded before you try to
set the properties, though.

Glad I was able to spot that typo for you!

--
Clif

Thank you again for all your help!

Clif said:
Try

With Forms(stDocName)

instead. (Note Forms, not Form)

--
Clif
Thank you for your fast response. I added your piece to my code.
It
now
[quoted text clipped - 35 lines]
properties for
Allow additions, deletions, edits?
 
C

Clif McIrvin

Since my last post, I thought some more about how to iterate through
several forms. One way would be to revise Allen's code as follows (note
I changed stDocName from String to Variant):

' change Dim stDocName As String to:
Dim varDocName As Variant
Dim varFormNames As Variant

(your other code)

varFormNames = Array("frmView", "name1", "name2", _
"name3") 'etc, as many names as needed
For Each varDocName In varFormNames
With Forms(varDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With
Next varDocName

(If there are others watching this thread, I'd be interested in any
comments about this iteration technique.)
[/QUOTE]

Let's see if i can get this right ... I've seen psotings about how to
reference subforms but not yet used the technique very much.

Essentially, you need to set the same properties for each subform, so
you need code to iterate through each subform and set the properties.

I believe the syntax would be identical, you simply provide the
subform name. You need to make sure the (sub)form is loaded before you
try to set the properties, though.

Glad I was able to spot that typo for you!

--
Clif

Thank you again for all your help!

Clif said:
Try

With Forms(stDocName)

instead. (Note Forms, not Form)

--
Clif

Thank you for your fast response. I added your piece to my code.
It
now
[quoted text clipped - 35 lines]
properties for
Allow additions, deletions, edits?
[/QUOTE]
 
A

Allen Browne

Thanks for picking up the type Clif

ladybug, I'm not sure if this is what you want, but if you are familiar with
VBA code and you want to handle the forms as well as the subforms, you could
adapt this approach:
Locking bound controls on a form and subforms
at:
http://allenbrowne.com/ser-56.html

That may be more effort than you need, but it adddresses several areas:
- It handles the subforms as well as the main form.
- It locks bound controls only, so you can still use any unbound controls
(e.g. for filtering or navigation.)
- It allows you to specify exceptions (controls that don't get
locked/unlocked.)

Again, just ignore it if it's more than what you need.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Clif McIrvin said:
Since my last post, I thought some more about how to iterate through
several forms. One way would be to revise Allen's code as follows (note I
changed stDocName from String to Variant):

' change Dim stDocName As String to:
Dim varDocName As Variant
Dim varFormNames As Variant

(your other code)

varFormNames = Array("frmView", "name1", "name2", _
"name3") 'etc, as many names as needed
For Each varDocName In varFormNames
With Forms(varDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With
Next varDocName

(If there are others watching this thread, I'd be interested in any
comments about this iteration technique.)

Let's see if i can get this right ... I've seen psotings about how to
reference subforms but not yet used the technique very much.

Essentially, you need to set the same properties for each subform, so you
need code to iterate through each subform and set the properties.

I believe the syntax would be identical, you simply provide the subform
name. You need to make sure the (sub)form is loaded before you try to set
the properties, though.

Glad I was able to spot that typo for you![/QUOTE][/QUOTE]
 
L

ladybug via AccessMonster.com

Sorry, I am very confused. I know very little about vb. It is hard for me
to tell what the difference between the code and your instructions are.
Based on what you last posted I came up with this, which I am sure does not
make any sense.

Dim varDocName As Variant
Dim varFormNames As Variant

stLinkCriteria = "[Number]=" & Me![cboSearch]

varFormNames = Array("frmView", "sfrmproducts", "sfrmcategories",
"sfrmclosed")
With Forms(varDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With
With Forms(varFormNames)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With

Clif said:
Since my last post, I thought some more about how to iterate through
several forms. One way would be to revise Allen's code as follows (note
I changed stDocName from String to Variant):

' change Dim stDocName As String to:
Dim varDocName As Variant
Dim varFormNames As Variant

(your other code)

varFormNames = Array("frmView", "name1", "name2", _
"name3") 'etc, as many names as needed
For Each varDocName In varFormNames
With Forms(varDocName)
.AllowEdits = False
.AllowDeletions = False
.AllowAdditions = False
End With
Next varDocName

(If there are others watching this thread, I'd be interested in any
comments about this iteration technique.)

--
Clif
Let's see if i can get this right ... I've seen psotings about how to
reference subforms but not yet used the technique very much.
[quoted text clipped - 28 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top