Disable the form controls if the record's field Saved is true

Derek

Registered User.
Local time
Today, 06:02
Joined
May 4, 2010
Messages
234
Hi All

I have a form named "Frmsearch" and there is a listbox so on the double click event of the listbox , another form will open with all the data relevant to item selected from the listbox.

Now I want to check if Saved field is True and if yes then make all the controls disabled. The checkbox named "chkSaved" has a control source "Saved".

Now the thing is If I write the code in Form Current event then it works but In Form Open event it doesn't.

Code:
 Private Sub Form_Current()
Dim ctr As Control
If Me.chkSaved = True Then
MsgBox "True"
For Each ctr In Me.Controls
'        MsgBox (ctr.Name & " " & ctr.Tag)
    If ctr.Tag = "C" Then
        ctr.Enabled = False
    End If
Next ctr
Else
MsgBox "False"
End If
End Sub

but when I write the code in Form open event then it doesn't work?It doesn't disable the controls and gives me "False" in the message box.

Code:
 Private Sub Form_Open(Cancel As Integer)
Dim ctr As Control
If IsNull(Me.cboAgent) Then
    Me.cboAgent = Forms!frmFormCatalogue!cboStaff
End If
 If Me.chkSaved = True Then
MsgBox "True"
For Each ctr In Me.Controls
'        MsgBox (ctr.Name & " " & ctr.Tag)
    If ctr.Tag = "C" Then
        ctr.Enabled = False
    End If
Next ctr
Else
MsgBox "False"
End If
End Sub

ANy help will be much appreciated.

Thanks.
 
Data isn't yet available in the open event. I'd use the current event anyway, as it would catch the changing of records.
 
One flaw in this code is that the controls will never be re-enabled if they are ever disabled.
Code:
Private Sub Form_Current()
   Dim ctr As Control

   If Me.chkSaved = True Then
      MsgBox "True"
      For Each ctr In Me.Controls
         If ctr.Tag = "C" Then
            ctr.Enabled = False
         End If
      Next ctr
   Else
      MsgBox "False"
   End If
End Sub
We can refactor this code as follows, so it's simpler, and so it does both jobs . . .
Code:
Private Sub Form_Current()
   Dim ctr As Control

   For Each ctr In Me.Controls
      If ctr.Tag = "C" Then ctr.Enabled = Not Me.chkSaved
   Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom