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!

If /or / then condition

Status
Not open for further replies.

selavarti

Programmer
Feb 24, 2004
33
0
0
US
Hi,

I am having two text fields in my form frist and last that represent names. I wrote a VBA code so that if some one leave those fileds null and saves the form it should show an error message. But for some reason its still taking null values. that is the if statement is not working. Here is the sample code

Dim addfunc As String
If (first.Value = Null) or (last.value= Null) Then
MsgBox "You have left one of the fields blank, please make sure that all fields are filled in before continuing"

Else
addfunc = first.Value & " " & last.Value
empname.Value = addfunc
DoCmd.GoToRecord , , acNewRec

Can someone please tell me what's the problem in this code

Thanks
 
Hi

Where do you have this code?, ie in which event

Also, you code is OK, but

If IsNull(first.Value) or IsNull(last.value) Then

is probably more readable


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Try using the IsNull function:

Dim addfunc As String

If IsNull(first.Value) or IsNull(last.value) Then
MsgBox "You have left one of the fields blank, please make sure that all fields are filled in before continuing"
Else
addfunc = first.Value & " " & last.Value
empname.Value = addfunc
DoCmd.GoToRecord , , acNewRec
End If


-Gary
 
To test for filled out fields on a form I go through each field
separately and test that data is entered, and also, that it is
the right type of data. Perhaps inelegant, but a belt and
suspenders approach has it's merits.

After each test I direct the focus back to the offending
control if needed - with a message specific to the problem.


' Is TxtDate a date ?
If Not IsDate(TxtDate) Then
MsgBox "You need to enter a date", vbOKOnly, "No date is entered"
TxtDate.SetFocus
Exit Sub
End If

' Is an item selected in CboTransPandO?
If [CboTransPandO].[ItemsSelected].[Count] = 0 Then
MsgBox "You need to select a contact", vbOKOnly, "No contact is selected"
CboTransPandO.SetFocus
Exit Sub
End If

' If the code lets me get to here - all is well and I can proceed


Best,

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top