Link to home
Start Free TrialLog in
Avatar of howruaz9
howruaz9Flag for Canada

asked on

Message on a button in Access 2003 form

Hi experts,

Below is my goal, of course it doesn't work for me like this. Could you please help?

Private Sub Command29_GotFocus()

If A=B
Then MsgBox ("Budget OK")
else MsgBox ("Please check Budget")
End if

End Sub

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<doesn't work>
?

Can you post the *entire* code please, then clearly state what does, or does not happen?

Also, are you quite sure the GotFocus event is the best event for this?
(Seems to me like BeforeUpdate or AfterUpdate, of a control might be better)

This is why it is always best if you *First* explain, in detail, what your ultimate goal is ...
no points please.

Agree with MX that you should be using the Click event.  I like to keep my code short, so I would probably use:

Private sub cmd_YourCommandButtonName_Click

    msgbox iif(A=B, "Budget OK", "Please check budget!")

End sub
...Or in the *Click* event of the button....

As the Gotfocus event of the button is rarely used for things like this...
Avatar of howruaz9

ASKER

Hi MX, it's working on click. Thank you so much!
Hi boag2000 and fyed, you both figured out my problem as MX did. Thank you so much for your time, and I wish I could give some points. And I've tried fyed's, it's working perfectly too.
Thanks again.
howruaz9,

mx's code is easier to read, if you don't know what you are looking at or are new to VBA, so that is probably your best solution.

I've been writing and maintaining code for so long, I tend to take shortcuts.  The problem with shortcuts is that you have to understand the pro's and con's.  In this case, the IIF() function has a interesting characteristic that when the function is evaluated both the True and False expressions are evaluated, and if either of those expressions would generate an error, then the error is raised.  So, the following expression would raise an error, even though the first expression evaluates to True, and should return (0.75)

A = 3
B = 4
C = 0

?iif(B>A, A/B, B/C)
howruaz9,

<I wish I could give some points. >

No crybabies here...

;-)

Jeff
Hi fyed,

I'm new to VBA, and not sure that I fully understand the following part. I've applied your IIF() Function to on click. Do I need to do something?
A = 3
B = 4
C = 0

?iif(B>A, A/B, B/C)

Thank you very much!
Let's keep it simple here guys. I realize that is a 'trick' solution, but come on ...

mx
Hi Jeff,

Your posts always make me think, if they don't, they makes me laugh......
howruaz9,

Sorry for the confusion.  Use mx's solution, it is the simplest and is easy to read.

I was attempting to impart some additional knowledge, and just muddied the waters.

Like I said above:

"mx's code is easier to read, if you don't know what you are looking at or are new to VBA, so that is probably your best solution."
<Hi Jeff,
Your posts always make me think, if they don't, they makes me laugh......>

Made My Day!
;-)

Jeff
Thanks, fyed. It's always good to know more. No need to say sorry. For now, just like mx and you suggested, I'm using the simpliest solution to make the database work.

Thanks, everyone.