If Then statement help

Clintimania

Registered User.
Local time
Today, 13:49
Joined
Nov 21, 2006
Messages
60
Ok, I do not know much about code and I am trying to learn/get this to work.

I have a form, and I want the form to run a macro if certain fields are left blank. This is what ive tried so far, which obviously is wrong, but I was hoping it would be enough for someone to see what I am trying to do and tell me my error.

If Name = Null Then DoCmd.RunMacro "required_fields" Else
DoCmd.GoToRecord , , acNext[/I]


This is the next record nav button on my form. Name is a field on the form that if left blank, I want it to run a macro. Any tips?
 
A couple of simple points
Don't use Name as a field or other name as ACCESS uses it as a reserved word.

Its not =Null but Is Null
and to access a field on the form use Me. intellisense will then list options

so we now have
If Me.myname Is Null Then
whatever
Else
whatever
End If

Brian
 
Alternatively...

If IsNull(Me!Name) Then
 
That worked great. Thank you! I know I am missing a few things when it comes to access, thanks for filling in the blanks.
 
Ok.. i had to step away from this for a few days, but now here is the deal...

This is what I have so far...

Private Sub Command74_Click()
On Error GoTo Err_Command74_Click

If IsNull(Me!Name) Then DoCmd.RunMacro "required_fields" Else
If IsNull(Me.CID_) Then DoCmd.RunMacro "required_fields" Else
If IsNull(Me!Date) Then DoCmd.RunMacro "required_fields" Else
If IsNull(Me.Intake_Staff) Then DoCmd.RunMacro "required_fields" Else
If IsNull(Me.Location_Code) Then DoCmd.RunMacro "required_fields" Else
If IsNull(Me.Office_Code) Then DoCmd.RunMacro "required_fields" Else
DoCmd.Close

Exit_Command74_Click:
Exit Sub

Err_Command74_Click:
MsgBox Err.Description
Resume Exit_Command74_Click

End Sub


This brings up the error message like I want, but it then runs the DoCmd.Close. Why does it run it If the statement above it is true?
I am not saying If-then-and , im running a If-then-else Also, I know I have 6 if thens, I would like them to be like this...

If IsNull(Me!Name) Then DoCmd.RunMacro "required_fields" Or
If IsNull(Me.CID_) Then DoCmd.RunMacro "required_fields" Or
If IsNull(Me!Date) Then DoCmd.RunMacro "required_fields" Or
If IsNull(Me.Intake_Staff) Then DoCmd.RunMacro "required_fields" Or
If IsNull(Me.Location_Code) Then DoCmd.RunMacro "required_fields" Or
If IsNull(Me.Office_Code) Then DoCmd.RunMacro "required_fields" Or
DoCmd.Close

but apparently that Or is not correct syntax...
I want the form to look to see if any of those fields are empty, if any of them are... I want it to run the macro, and NOT run the DoCmd.Close.

can someone get me pointed in the right direction?
 
TRy
If criteria1 or criteria2 or criteria3.... Then
whatever
Esle
Whatever
Endif

Brian
 
Ok.. so I have...

If IsNull(Me!Name) Or IsNull(Me.CID_) Or IsNull(Me!Date) Or IsNull(Me.Intake_Staff) Or IsNull(Me.Location_Code) Or IsNull(Me.Office_Code) Then DoCmd.RunMacro "required_fields" Else
DoCmd.Close


How or where do I apply the endif?
 
If IsNull(Me!Name) Or IsNull(Me.CID_) Or IsNull(Me!Date) Or IsNull(Me.Intake_Staff) Or IsNull(Me.Location_Code) Or IsNull(Me.Office_Code) Then

DoCmd.RunMacro "required_fields"

Else

DoCmd.Close

End If

EDIT: Code fixed, good catch :)
 
Was that how I laid it out?

I laid out the block If then else. I find them easier to read if multi conditions apply

Brian
 
You don't need the 1st else in DevastatioN's reply which I think is a typo as he reformatted your code.
 

Users who are viewing this thread

Back
Top Bottom