Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required Fields

Status
Not open for further replies.

lbunch

Programmer
Sep 5, 2006
120
0
0
US


This is a sample of some required fields I am validating to see if null - if condition is null then I want the user to see a message box "All Required fields must be entered" and disable the AddNew button else Enable the AddNew button. Is there a way to check the text boxes to see if any one is null and then prompt for message and disable button else enable button???


If Len(Me![cmbType]) = 0 Then
If IsNull(Me![cmbType]) Then
Forms![frms19_1]![cmdaddnew].Enabled = False
txtMessage = "All Required fields must be entered before the record can be added." & vbNewLine & _
"Enter a value or press 'Cancel' to about the record."
Me![cmbType].SetFocus
Else
Forms![frms19_1]![cmdaddnew].Enabled = True ' if this is what you want
End If

ElseIf Len(Me![cmbID]) Then
If IsNull(Me![cmbID]) Then
Forms![frms19_1]![cmdaddnew].Enabled = False
txtMessage = "All Required fields must be entered before the record can be added." & vbNewLine & _
"Enter a value or press 'Cancel' to about the record."
Me![cmbID].SetFocus
Else
Me![cmbID].SetFocus
End If
 
Here is a sketch of an idea, it is typed, not tested. You will probably wish to add other control types or perhaps use the tag property to mark controls to test. You may also wish to test for several data types.

Code:
For Each ctl In Me.Controls
   If ctl.ControlType=acTextBox Then
      If Trim(ctl.Value & "") = "" Then
         'Highlight missing data fields
         Me(ctl.Name).BackColor=vbYellow
         blnError=True
      Else
        Me(ctl.Name).BackColor=vbWhite
      End If
   End If
Next

If blnError=True Then
    Me.[cmdaddnew].Enabled = False
    txtMessage = "All Required fields must be entered before the record can be added." & vbNewLine & _
        "Enter a value or press 'Cancel' to about the record."
Else
    Me.[cmdaddnew].Enabled = True
End If

 
And if you don't need to validate all the text boxes, but just some of them, you could use TheAceMan1's favorite trick of marking the ones you want validated using the Tag property.

Under the control's Tag property enter "?" (without the quotes) then in Remou's code, replace

If ctl.ControlType=acTextBox Then

with

If ctl.ControlType = acTextBox And ctl.Tag = "?" Then

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top