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!

Checking for multiple null fields 3

Status
Not open for further replies.

krets

Technical User
Dec 29, 2006
214
US
I have an unbound form so I'm using VB to check that required fields are filled in before saving the record. Initially there was only one required field but now they would like more. I'm checking for nulls using:

Code:
If IsNull(Me!TechRxEInit) Then
    MsgBox "Tech initials cannot be blank!"
    Exit Sub
Else
    Do Stuff
End If

What would be the best way to check for more fields? Add another if statement below this one or can I check for several null fields with one If statement?
 
A common way is to enforce all validation rules in the BeforeUpdate event procedure of the bound form:
If Trim(Me!TechRxEInit & "") = "" Then
MsgBox "Tech initials cannot be blank!"
Cancel = True
Exit Sub
End If
If Trim(Me!AnotherControl & "") = "" Then
MsgBox "Another control cannot be blank!"
Cancel = True
Exit Sub
End If
Do Stuff

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
you can use:

if someCondition then
...
elseif someCondition then
...
elseif someCondition then
...
else someCondition then
...
endif

--------------------
Procrastinate Now!
 
Crowley, why use any kind of Else clause after an Exit Sub instruction ?
 
Howdy krets . . .

. . . or another way . . .
[ol][li]add a question make [blue]?[/blue] to the [blue]Tag[/blue] property of those controls you wish to validate.[/li]
[li]In the [blue]BeforeUpdate[/blue] event of the form, copy/paste the following routine:
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String, ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then
         If Trim(ctl & "") = "" Then
            Msg = ctl.Name & " can't be null!"
            Style = vbInformation + vbOKOnly
            Title = "Empty Control Detected!"
            MsgBox Msg, Style, Title
            Cancel = True
            Exit For
         End If
      End If
   Next[/blue]
[/li][/ol]
The code prevents saving untill all empties with the question mark are corrected!

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top