Enable Control based on value

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

Guest

Hi Everyone
I have data entry form with check box named "completed". I'd like this check
box to be active only if all other text boxes have value in them. So on
Current Event I've placed some if statemnts. My problem is that it works at
first, but then it stops and my check box is not active at all.
Here is thee code

Private Sub Form_Current()
If IsNull(Me.TimeBack) Then
Me.chkCompleted.Enabled = False
Else:
If IsNull(Me.TimeOut) Then
Me.chkCompleted.Enabled = False
Else:
If IsNull(Me.txtMilageIn) Then
Me.chkCompleted.Enabled = False
Else:
If IsNull(Me.txtMilageOut) Then
Me.chkCompleted.Enabled = False
Else
If (Me.txtServiced.Value = 0) Then
Me.chkCompleted.Enabled = False

Else: Me.chkCompleted.Enabled = True
End If
End If
End If
End If
End If
End Sub

I'm just starting with VB, please help.
Barb
 
Here is a function that will do what you want, I think. You stated "all
other text boxes have value", so this does exactly that. It looks at every
control in the form and if it is a text box, it checks for null. The first
time it finds a text box with a null value, it exits and returns false. If
all textboxes are not null, it returns true

Public Function AddControls(frm As Form) As Boolean
Dim ctl As Control
AddControls = True
For Each ctl In frm
If ctl.ControlType = acTextBox Then
If IsNull(ctl) Then
AddControls = False
Exit For
End If
End If
Next ctl

End Function

Now, if it is only some of the text boxes on your form you want to check,
here is a version that will do that. It requires you enter some value in the
control's Tag property. I will use "CheckIt" for example purposes

Public Function AddControls(frm As Form) As Boolean
Dim ctl As Control
AddControls = True
For Each ctl In frm
If ctl.ControlType = acTextBox Then
If ctl.Tag = "CheckIt" And IsNull(ctl) Then
AddControls = False
Exit For
End If
End If
Next ctl

End Function
 
Thank you for you replay. Sorry, I'm new at this. Should I put this code into
On Current event of the form? Or somwhere else?

Thanks
Barb
 
Back
Top