MsgBox Function and Private Variables

  • Thread starter NewKidontheBlock
  • Start date
N

NewKidontheBlock

In my database there are a large number of date fields that I wish to
validate using VBA rather than validation (more user friendly)

Firstly, is it possible to use a private variable instead of a text string
in MsgBox().

For example :

MsgBox(DateEarly)
instead of
MsgBox("The date cannot be before the review date. Please enter a differnt
date")

Secondly, I have defined the variable as follows in the Declarations section
of the code :

Private DateEarly as String
DateEarly = "The date cannot be before the review date. Please enter a
different date" but it results in an invalid outside procedure error.

If this can be achieved it would save having to update 20 or 30 message box
messages

Kind regards

Tony
 
D

Douglas J. Steele

Unless you're trying to capture the return value indicating which button on
the message box was pressed, you don't want the parentheses, but other than
that, yes, you can use a variable:

MsgBox DateEarly

While you can declare the variable outside of a routine, remember that you
can only assign values to it in a routine. In other words, your Private
DateEarly as String can go in the Declaration section, but your assignment
needs to be in a function or sub. An alternative would be to declare
DateEarly as a constant:

Private Const DateEarly as String = "The date cannot be before the review
date. Please enter a different date"

(watch for word-wrap: that should be all on one line.)
 
N

NewKidontheBlock

Thanks Doug. Spot on!

Kind regards

Tony


Douglas J. Steele said:
Unless you're trying to capture the return value indicating which button on
the message box was pressed, you don't want the parentheses, but other than
that, yes, you can use a variable:

MsgBox DateEarly

While you can declare the variable outside of a routine, remember that you
can only assign values to it in a routine. In other words, your Private
DateEarly as String can go in the Declaration section, but your assignment
needs to be in a function or sub. An alternative would be to declare
DateEarly as a constant:

Private Const DateEarly as String = "The date cannot be before the review
date. Please enter a different date"

(watch for word-wrap: that should be all on one line.)
 
Top