Using If/ElseIf statement

A

Alex Martinez

Hello,

I am having a problem with my ElseIf statement which is in my form event
(Before update) I have two fields one call AssignedOn, which is a date
field and the other is AssignedTo a text field. My problem is I get a
error message -Run time error "424" Object Required using the below
code. What I want is when the user populate the AssignedTo field (User
Name) and forgets to put a date on the AssignedOn date field I want an
error message pop up. This is the last piece of code that I have in my If
statement, but I can't figure it out. I would appreciate any tips. Thank
you.

ElseIf Me.[AssignedOn] Is Null And Me.[AssignedTo] Is Not Null Then
Cancel = True
MsgBox "Sorry, You must assigned this policy to a user. Try Again"


End If
 
A

Allen Browne

Use the IsNull() function to test for Null in VBA:
ElseIf IsNull(Me.[AssignedOn]) And Not IsNull(Me.[AssignedTo]) Then

Unlike the SQL statement context where "... Is Null" is correct, the Is
operator in VBA is used for testing object types.
 
T

tina

the Is operator in VBA is used for testing object types.

hi Allen, could you give me an example of the syntax? i've been fiddling
with this and all i could find in VBA Help was

If ctrl.ControlType = acTextBox Then

it works, as far as that goes, but i'd like to learn the Is syntax anyway.
thx, tina :)


Allen Browne said:
Use the IsNull() function to test for Null in VBA:
ElseIf IsNull(Me.[AssignedOn]) And Not IsNull(Me.[AssignedTo]) Then

Unlike the SQL statement context where "... Is Null" is correct, the Is
operator in VBA is used for testing object types.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Alex Martinez said:
Hello,

I am having a problem with my ElseIf statement which is in my form event
(Before update) I have two fields one call AssignedOn, which is a date
field and the other is AssignedTo a text field. My problem is I get a
error message -Run time error "424" Object Required using the below
code. What I want is when the user populate the AssignedTo field (User
Name) and forgets to put a date on the AssignedOn date field I want an
error message pop up. This is the last piece of code that I have in my If
statement, but I can't figure it out. I would appreciate any tips. Thank
you.

ElseIf Me.[AssignedOn] Is Null And Me.[AssignedTo] Is Not Null Then
Cancel = True
MsgBox "Sorry, You must assigned this policy to a user. Try Again"
End If
 
A

Allen Browne

Yes, that's the way I prefer also, Tina.

There was an archaic syntax that went something like this:
Dim ctl As Control
If ctl Is Nothing Then
Debug.Print "nothing"
Else
If TypeOf ctl Is TextBox Then
Debug.Print "Text box"
End If
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

tina said:
the Is operator in VBA is used for testing object types.

hi Allen, could you give me an example of the syntax? i've been fiddling
with this and all i could find in VBA Help was

If ctrl.ControlType = acTextBox Then

it works, as far as that goes, but i'd like to learn the Is syntax anyway.
thx, tina :)


Allen Browne said:
Use the IsNull() function to test for Null in VBA:
ElseIf IsNull(Me.[AssignedOn]) And Not IsNull(Me.[AssignedTo]) Then

Unlike the SQL statement context where "... Is Null" is correct, the Is
operator in VBA is used for testing object types.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Alex Martinez said:
Hello,

I am having a problem with my ElseIf statement which is in my form
event
(Before update) I have two fields one call AssignedOn, which is a date
field and the other is AssignedTo a text field. My problem is I get a
error message -Run time error "424" Object Required using the below
code. What I want is when the user populate the AssignedTo field (User
Name) and forgets to put a date on the AssignedOn date field I want an
error message pop up. This is the last piece of code that I have in my If
statement, but I can't figure it out. I would appreciate any tips. Thank
you.

ElseIf Me.[AssignedOn] Is Null And Me.[AssignedTo] Is Not Null Then
Cancel = True
MsgBox "Sorry, You must assigned this policy to a user. Try Again"
End If
 
T

tina

ahhh, there it is! "TypeOf"...i tried just about everything *but*...
<bangs head against wall>
thanks, Allen, now maybe i can get some sleep! <smiles, bows, gulps some
aspirin>


Allen Browne said:
Yes, that's the way I prefer also, Tina.

There was an archaic syntax that went something like this:
Dim ctl As Control
If ctl Is Nothing Then
Debug.Print "nothing"
Else
If TypeOf ctl Is TextBox Then
Debug.Print "Text box"
End If
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

tina said:
the Is operator in VBA is used for testing object types.

hi Allen, could you give me an example of the syntax? i've been fiddling
with this and all i could find in VBA Help was

If ctrl.ControlType = acTextBox Then

it works, as far as that goes, but i'd like to learn the Is syntax anyway.
thx, tina :)


Allen Browne said:
Use the IsNull() function to test for Null in VBA:
ElseIf IsNull(Me.[AssignedOn]) And Not IsNull(Me.[AssignedTo]) Then

Unlike the SQL statement context where "... Is Null" is correct, the Is
operator in VBA is used for testing object types.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.


Hello,

I am having a problem with my ElseIf statement which is in my form
event
(Before update) I have two fields one call AssignedOn, which is a date
field and the other is AssignedTo a text field. My problem is I get a
error message -Run time error "424" Object Required using the below
code. What I want is when the user populate the AssignedTo field (User
Name) and forgets to put a date on the AssignedOn date field I want an
error message pop up. This is the last piece of code that I have in
my
If
statement, but I can't figure it out. I would appreciate any tips. Thank
you.

ElseIf Me.[AssignedOn] Is Null And Me.[AssignedTo] Is Not Null Then
Cancel = True
MsgBox "Sorry, You must assigned this policy to a user. Try Again"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top