Help, I can't find the right code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was trying to code using If Then based on a post I read but I think I'm
using the wrong code.
In my form I want if you answer "yes" to variable 1 then variable 2 must be
completed however the code I am currently using required variable 2 to be
completed regardless if the answer is "yes" or "no"
I am new to access and the world of coding please help! Here is the code I
am using:
If Not IsNull(Me.Injuries_to_Staff) And IsNull(Me.Explain_Injury) Then
Cancel = True
MsgBox "Explanation of Injury Is Required"
Me.Explain_Injury.SetFocus
End If

THANKS!!!!!!!!!!!
 
You might want to provide a little more information, such as what type of
controls you are using on your form an where this code is located. Is it in
the After Update event of a control, the On Current event of your form, etc.?
 
What's probably happening is that whatever control you're using to represent
the "Injuries_to_Staff" is never Null. It's either On or Off for instance. Or
Yes or No in your case.

You may want to look into what the "Value" of Injuries_to_Staff is. This
could be done using a msgbox right before your current if statement.
 
Hi Michelle

Is Injuries_to_Staff a boolean (yes/no) field? If so, then it is never
Null - it can only be True (-1) or False (0).

Change your condition to:
If Not (Me.Injuries_to_Staff <> 0) And IsNull(Me.Explain_Injury) Then
 
Graham Mandeno Thanks for the help. that solved one issue but maybe you can
assist me with a similar problem as well??

I want to do the same thing make one field required if another field is
filled out but this it is with date and time......therefor if the date is
entered then the time must be completed. Any ideas on how I would code that??
Would I still use an If/Then?

Thanks!

Graham Mandeno said:
Hi Michelle

Is Injuries_to_Staff a boolean (yes/no) field? If so, then it is never
Null - it can only be True (-1) or False (0).

Change your condition to:
If Not (Me.Injuries_to_Staff <> 0) And IsNull(Me.Explain_Injury) Then
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand


Michelle said:
I was trying to code using If Then based on a post I read but I think I'm
using the wrong code.
In my form I want if you answer "yes" to variable 1 then variable 2 must
be
completed however the code I am currently using required variable 2 to be
completed regardless if the answer is "yes" or "no"
I am new to access and the world of coding please help! Here is the code I
am using:
If Not IsNull(Me.Injuries_to_Staff) And IsNull(Me.Explain_Injury) Then
Cancel = True
MsgBox "Explanation of Injury Is Required"
Me.Explain_Injury.SetFocus
End If

THANKS!!!!!!!!!!!
 
Hi Michelle

May I ask why you are using two fields? The date/time data type stores both
the date AND the time.

If you really must use two fields then test for Null:

If Not IsNull(DateField) and IsNull(TimeField) Then ...

If you are using a single field, then entering a date with no time will give
a TimeValue of zero, which translates to midnight. You can check for that
like this:

If TimeValue(DateTimeField) = 0 Then ...

However, this would not allow a legitimate time of exactly midnight to be
entered.
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Michelle said:
Graham Mandeno Thanks for the help. that solved one issue but maybe you
can
assist me with a similar problem as well??

I want to do the same thing make one field required if another field is
filled out but this it is with date and time......therefor if the date is
entered then the time must be completed. Any ideas on how I would code
that??
Would I still use an If/Then?

Thanks!

Graham Mandeno said:
Hi Michelle

Is Injuries_to_Staff a boolean (yes/no) field? If so, then it is never
Null - it can only be True (-1) or False (0).

Change your condition to:
If Not (Me.Injuries_to_Staff <> 0) And IsNull(Me.Explain_Injury) Then
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand


Michelle said:
I was trying to code using If Then based on a post I read but I think
I'm
using the wrong code.
In my form I want if you answer "yes" to variable 1 then variable 2
must
be
completed however the code I am currently using required variable 2 to
be
completed regardless if the answer is "yes" or "no"
I am new to access and the world of coding please help! Here is the
code I
am using:
If Not IsNull(Me.Injuries_to_Staff) And IsNull(Me.Explain_Injury) Then
Cancel = True
MsgBox "Explanation of Injury Is Required"
Me.Explain_Injury.SetFocus
End If

THANKS!!!!!!!!!!!
 
Hi

Well because I didn't know that you could put them both in one field. When I
look at the formats for the date/time it shows the general date option which
does have both however it shows the time using am/pm and I need it to be in
military format cause we are a healthcare institution. If I can do them both
in one field, please let me know how and I will certianlly try to do it that
way. Thanks for the help!

Graham Mandeno said:
Hi Michelle

May I ask why you are using two fields? The date/time data type stores both
the date AND the time.

If you really must use two fields then test for Null:

If Not IsNull(DateField) and IsNull(TimeField) Then ...

If you are using a single field, then entering a date with no time will give
a TimeValue of zero, which translates to midnight. You can check for that
like this:

If TimeValue(DateTimeField) = 0 Then ...

However, this would not allow a legitimate time of exactly midnight to be
entered.
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Michelle said:
Graham Mandeno Thanks for the help. that solved one issue but maybe you
can
assist me with a similar problem as well??

I want to do the same thing make one field required if another field is
filled out but this it is with date and time......therefor if the date is
entered then the time must be completed. Any ideas on how I would code
that??
Would I still use an If/Then?

Thanks!

Graham Mandeno said:
Hi Michelle

Is Injuries_to_Staff a boolean (yes/no) field? If so, then it is never
Null - it can only be True (-1) or False (0).

Change your condition to:
If Not (Me.Injuries_to_Staff <> 0) And IsNull(Me.Explain_Injury) Then
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand


I was trying to code using If Then based on a post I read but I think
I'm
using the wrong code.
In my form I want if you answer "yes" to variable 1 then variable 2
must
be
completed however the code I am currently using required variable 2 to
be
completed regardless if the answer is "yes" or "no"
I am new to access and the world of coding please help! Here is the
code I
am using:
If Not IsNull(Me.Injuries_to_Staff) And IsNull(Me.Explain_Injury) Then
Cancel = True
MsgBox "Explanation of Injury Is Required"
Me.Explain_Injury.SetFocus
End If

THANKS!!!!!!!!!!!
 
Hi

Well because I didn't know that you could put them both in one field. When I
look at the formats for the date/time it shows the general date option which
does have both however it shows the time using am/pm and I need it to be in
military format cause we are a healthcare institution. If I can do them both
in one field, please let me know how and I will certianlly try to do it that
way. Thanks for the help!

Note that you are NOT limited to the named formats. You can use a format such
as

"dd-mmm-yyyy hh:nn"

to show a time like

19-Jul-2007 14:22


John W. Vinson [MVP]
 
Hi Michelle

I guess John has answered your question, but just as a word of warning, note
that the format code for minutes is "nn" not "mm".

Using "mm" will give you the numeric form of the *month*. I know of several
novices who have been caught with this, so just thought it worth a mention.
--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand
 
Hi Michelle

I guess John has answered your question, but just as a word of warning, note
that the format code for minutes is "nn" not "mm".

Using "mm" will give you the numeric form of the *month*. I know of several
novices who have been caught with this, so just thought it worth a mention.

Quite true, and good advice - but to my astonishment, Access is clever enough
to interpret hh:mm as hours and minutes. Context sensitive I guess!

I would't USE that quirk but it's interesting that it lets you do it.


John W. Vinson [MVP]
 
Back
Top