If Statement not working

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

Guest

Good morning,

Could someone please help me with why the following If Statement is not
working?

Private Sub HourmeterStart_BeforeUpdate(Cancel As Integer)

If Me.HourmeterStart < Me.MaxHMR AND Me.HasMeter = True Then
MsgBox "This hourmeter is less than the last hourmeter!" & Chr(10) & Please
verify meter reading.", vbOkOnly, "Stop!"
Cancel = True
Me.HourmeterStart.Undo

End If

End Sub
*************************

Even if the current meter being entered is greater than the last meter
reading we get the MsgBox error. I've checked all tables, queries involved
and there are no previous readings that are higher than the current & yet we
still get the message. Is there something missing from my code?

Any help would be greatly appreciated.

Thanks
 
Looks like you're missing a " before Please:

MsgBox "This hourmeter is less than the last hourmeter!" & Chr(10) & "Please
verify meter reading.", vbOkOnly, "Stop!"
 
Hi,

Thanks so much for the reply, but unfortunately that was just a typo on my
part. Actual code on my form is correct. Any other suggestions?

Thanks again for your help.
 
Hi Gabby,

Try making it :

If Me.HourmeterStart.text <

If that doesn't work, it may be a problem with data types, but I'm not sure...

CW
 
Put a debug stop on the If line, run the code, and watch your conditions.
Make sure that these all return values (and/or determine a way to handle
Nulls):
Me.HourmeterStart
Me.MaxHMR
Me.HasMeter

Adding parentheses around the two conditions might help.

Gabby said:
Hi,

Thanks so much for the reply, but unfortunately that was just a typo on my
part. Actual code on my form is correct. Any other suggestions?

Thanks again for your help.
Looks like you're missing a " before Please:
[quoted text clipped - 27 lines]
 
Thank You Kingston, BruceM and Cheese_whiz for all your help.

HourmeterStart is a Number data type & MaxHMR is actually a DLookup field
(from a query) and so to tell you the truth I really don't know what data
type that makes it (still pretty new to all this - but Thanks to this
newsgroup learning more each day)

Anyway, Cheeze_whiz your suggestion Me.HourmeterStart.Text fixed the
problem. I don't understand why that is but it did the trick so why question
it!

Again Thank you so much to all of you.
 
Glad it helped, Gabby.

It comes down to when the data you are trying to reference was added. If
you go to a record and type in a new entry for a textbox, that's considered
to be an 'uncommitted' value (read: unsaved). Until you do something to
'commit' the value, you have to refer to it using the .text property.

Navigating to a new record, closing the form, running appropriate vba code,
etc. will all commit the value, after which you don't need the .text anymore.

Hope that helps,
CW
 
Hi CW,

Yes that helped alot. I really do prefer to understand how things work.

Thanks again. I really appreciate all your help/expertise.
Have a great day.

Gabby
 
Back
Top