Validation On An InputBox

S

Steve Mulhall

Hi all,

Can I add validation to an input box? i.e. I have a button
that when pressed loads a box in which the user can enter
an e-mail address and then press 'OK' and an e-mail is
loaded containing data from the database. The box that is
loaded is called using the InputBox function and thus
already has an 'OK' & 'Cancel' button.

The problem is, regardless of whether the user
presses 'OK' or 'Cancel', an e-mail is loaded and
populated with the correct data. But what I need is if the
user presses 'Cancel' to simply do nothing and return to
the database, or if the user doesn't enter anything in the
box and presses 'OK' for an error message to be generated
(telling them to enter an e-mail address - without this
validation if the user does not enter anything in the box
and presses 'OK' and e-mail is loaded but with a
blank "To:" field - I'd like to avoid this if possible).

The problem I have is that I have usually done this sort
of validation based on whether a user presses a certain
button on a message box e.g. if user presses OK then do
something otherwise do something else (If Response = 1
then "Action1" Else If Response = 0 Then "Action2") the
problem is, regardless of whether the user presses 'OK'
or 'Cancel' on the on the InputBox, the response value is
always 1 and therefore I cannot validate in the way I have
described above. Any ideas?

Hope this all makes sense?

Steve
 
N

Norman Fritag

Steve,
try creating a invisible txtbox that you assign the value of the inputbox.
then use the before update event to validate the information.

if then all is as you like it trigger (call) the email event.

kind regards

Norman
 
T

TC

s = inputbox (enter something")
if strptr (s) = 0 then
' user clicked Cancel; this is the ONLY WAY to
' reliably determine that the user clicked Cancel.
else
' he didn't click Cancel.
s = trim$ (s) ' delete any leading/trailing spaces.
if s = "" then
' he entered, nothing or just blanks.
else
' he entered something nonblank.
endif
endif

HTH,
TC
 
G

Guest

Norman and TC, thanks for your replies.

I should really have mentioned in my original post that I
am a relative beginner to VBA programming and as such have
pretty much taught myself everything so far meaning a lot
of what you both wrote didn't make a great deal of sense
to me, but I've tried to work it out and incorporate your
suggestions into my code. Unfortunately, it's not quite
working yet in that although, when the user
presses 'Cancel' the command is cancelled (as I would
hope) and the user returned to the DB window,
unfortunatley the same also happens if the user enters an
e-mail address and presses 'OK', so I clearly have to re-
work my code logic I guess. Rest assured though guys, I
don't expect you to hold my hand and walk me through this,
I will try and work it out on my own.

One further question though, TC, in your example code you
have the following line:

s = InputBox("Enter Something")

I assume I have to first declare s as something at the
start of the procedure code? What to I declare it as? At
the moment I have: Dim s as Object.

Thanks again guys

Steve
 
T

TC

Hi Steve

The InputBox() function returns a string value, so you would define s as a
string, not an Object:

Dim s As String

Personally, I am happy to use the variable name "s" for the temporary
storage of a string value. But others would prefer a more formal name, eg.
strTemp. In that name, the prefix "str" identifies a string variable, and
"Temp" implies that it is only used in a few lines of code; the value would
not be used hundreds of lines further down in the code; if that were the
case, the variable would be called strInputBoxReply, or somesuch. These are
just >conventions< to make your code easier for other people to understand.
Access itself, does not care what you call your variables, within reason. So
you could have a variable called jh__efyu543267 - not that this would be
sensible, as you'd never remember what it stood for!

HTH,
TC
 
S

Steve Mulhall

Just wanted to pass on my thanks TC as I've got it all
working how I'd like now, thanks in no small part to your
advice.

Steve
 

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

Similar Threads

Inputbox ok only 1
Input box question 3
Inputbox Value Reference 3
Data Entry VBA 4
Inputbox problem 2
How to Cancel a Cancel Button event 3
Cancel an InputBox? 4
Ensuring a Field is Populated 3

Top