Me Key word refs a Form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How would I refer to an entire form for setting it's properties (ocering all it's controls in one fell swoop), AllowEdits, AllowDeletes, AllowUpdates

Thanks
 
How would I refer to an entire form for setting it's properties (ocering all
it's controls in one fell swoop), AllowEdits, AllowDeletes, AllowUpdates?

If setting form properties, you would use (for example):

Me.AllowEdits = True
Me.AllowDeletions = True
Me.AllowAdditions = True

To set individual control properties, you still would need to loop through all
the controls to set their properties, ensuring first to see that the current
control was of a type that has that property:

'***EXAMPLE START
'Declare object variable
Dim ctl As Control

'Cycle through controls
For Each ctl In Me.Controls
Select Case ctl.ControlType 'Find relevant controls
Case acTextBox, acComboBox, acListBox
ctl.Locked = True 'Set a property
'{... etc. ...}
End Select
Next
'***EXAMPLE END

As an alternative to checking the control type, you could also set each relevant
control's "Tag" property to a specific value to act as a flag for this purpose.
You could your code in a sub or function to allow you to set properties by
making a single call.
 
You stated: You could your code in a sub or function to allow you to set
properties by
making a single call. How would I do that?

First of all, I meant to say the "You could place your code ..." (apologies for
the typo).

Anyway, here is a sample function that demonstrates what I was referring to - I
have placed code to Lock/Unlock specific types of controls in a subroutine that
can be called to perform the locking/unlocking with one line of code. If you
want to try it on more than one form, copy and paste it to a standard module (a
"global" module, not a module behind a form or report):

'***EXAMPLE START
Public Sub subCtlsLockOrNot(frm As Form, bLockState As Boolean)
On Error GoTo subCtlsLockOrNot_ERR

'Make sure that form is not "dirty" or the
' "locking/unlocking" code will fail
If frm.Dirty = True Then
If MsgBox("This form has unsaved data. OK to save?", _
vbYesNo + vbQuestion, "Unsaved Data") = vbYes Then
frm.Dirty = False
Else
MsgBox "The Locked property will not be set.", _
vbOKOnly + vbInformation, "Property Not Set"
Exit Sub
End If
End If


'Declare object variable
Dim ctl As Control

'Cycle through controls
For Each ctl In frm.Controls
Select Case ctl.ControlType 'Find relevant controls
Case acTextBox, acComboBox, acCheckBox
ctl.Locked = bLockState 'Set a property
End Select
Next

subCtlsLockOrNot_EXIT:
Exit Sub

subCtlsLockOrNot_ERR:
MsgBox "Error " & Err.Number & _
" occurred in subCtlsLockOrNot: " & Err.Description
Resume subCtlsLockOrNot_EXIT

End Sub
'***EXAMPLE END

The sub can be called to lock controls of the specified types as follows:

subCtlsLockOrNot Me, True 'from within a form
subCtlsLockOrNot Forms("Form1"), True 'from anywhere

Of course, to unlock the controls, you simple pass a False instead of True as
the second argument.
 
Back
Top