Input parameter in OnOpen event

J

Jack Isaacs

Hello All

I need an OnOpen event for a form that will carry out one of 5 actions -
depending on a selection made by the user.
i.e.
The user should first be presented with a messagebox along the lines of
"Enter X to do action1, or enter Y to do action2 or enter Z to do action3"
Then
if the user enters "X" the OnOpen event must do action1 (which would be to
set the value of txtName to "Bricks and bats" and then open rpt1)
if the user enters "Y" the OnOpen event must do action2 (which would be to
set the value of txtName to "Trains, Boats and Rockets" then open rpt2)
if the user enters "Z" the OnOpen event must do action3 (which would be to
set the value of txtName to "People and Places" then open rpt3)
if the user enters "V" the OnOpen event must do action3 (which would be to
set the value of txtName to "Let's start here" then open rpt4)
if the user enters "W" the OnOpen event must do action3 (which would be to
set the value of txtName to "My house" then open rpt5)
if the user enters anything else the OnOpen event must do nothing further.
(in fact each of the actions will have 2 or 3 more commands in them)

I know I could do this by getting the user to click one of 5 buttons on the
form, but I would much rather 'force' the user select the action as the form
opens - and also I would prefer not to have to display any further buttons,
textboxes etc on this particular form.

I was hoping to use a MsgBox command for this followed by a Case statement,
but I don't think the MsgBox command allows the user to input a parameter.

If anyone can suggest how this could be done I would be very grateful.

Many thanks
Leslie Isaacs
 
O

OldPro

Hello All

I need an OnOpen event for a form that will carry out one of 5 actions -
depending on a selection made by the user.
i.e.
The user should first be presented with a messagebox along the lines of
"Enter X to do action1, or enter Y to do action2 or enter Z to do action3"
Then
if the user enters "X" the OnOpen event must do action1 (which would be to
set the value of txtName to "Bricks and bats" and then open rpt1)
if the user enters "Y" the OnOpen event must do action2 (which would be to
set the value of txtName to "Trains, Boats and Rockets" then open rpt2)
if the user enters "Z" the OnOpen event must do action3 (which would be to
set the value of txtName to "People and Places" then open rpt3)
if the user enters "V" the OnOpen event must do action3 (which would be to
set the value of txtName to "Let's start here" then open rpt4)
if the user enters "W" the OnOpen event must do action3 (which would be to
set the value of txtName to "My house" then open rpt5)
if the user enters anything else the OnOpen event must do nothing further.
(in fact each of the actions will have 2 or 3 more commands in them)

I know I could do this by getting the user to click one of 5 buttons on the
form, but I would much rather 'force' the user select the action as the form
opens - and also I would prefer not to have to display any further buttons,
textboxes etc on this particular form.

I was hoping to use a MsgBox command for this followed by a Case statement,
but I don't think the MsgBox command allows the user to input a parameter.

If anyone can suggest how this could be done I would be very grateful.

Many thanks
Leslie Isaacs

First, I would put 5 invisible labels on the form, named "Label1"
through "Label5". Then I would use the form_load event to
call a funcion that positions the labels in the middle of the screen,
and makes them visible. The caption for each label would be one of
the five options. Then I would put a function in the OnClick event of
each label, like this: =SetCategory(x) where x is the label number (1
through 5). The SetCategory( ) function would turn the labels
invisible and set up the form based on the selection. I would call
another function in the .mouseover event called SetHilight( ). It
would change the color of the currently selected label to a lighter
color while switching the color of the previous label back to its
original. That way the options will light up as the user passes the
mouse over each one. A static variable in the SetHilight( ) function
can track which label was hilighted last.
 
G

Guest

Sounds like maybe you want to usethe InputBox function. This works similar to
MsgBox, only it allows the user to type in a value. You specify the message
you want displayed for the user, and the defualt value if the user doesn't
enter anything. Something like:

UserReply = InputBox("Enter 'X' for option A" & vbcrlf & "Enter 'Y' for
option B",
,,vbNUllString)
Select Case UserReply
Case "X"
do this...
Case "Y"
do this.....
End Select

This will display the message telling them what to enter (I put vbCrLF in
there so
that each option is shown on a separate line), and allow them to type in an
option. You then check the value of the UserReply variable to test what they
entered - if they didn't enter anything (inlclude an option for this in the
SELECT CASE), it will have a value of vbNUllString (same as "").
 
J

Jack Isaacs

Jim

Thanks for your reply: the InputBox command sounds like exactly what I need.
I won't have chance to try it until tomorrow - but I'll let you know how I
get on.

Thanks again
Les
 
J

Jack Isaacs

Old Pro

Thanks for your reply.
I think I undestand your suggestion, but it does sound a bit 'involved'!
I'm going to try Jim's suggestion with the InputBox command, but if that
doesn't work I'll have a go at your method.

Thanks again for your help
Les
 
L

Leslie Isaacs

Hello Jim

I tried the cose as you suggested and initially got the message that
UserReply wasn't defined, so I added Dim UserReply As String but then I get
a message that there is a type mismatch!
The code I have is below.
Hope you can help.
Thanks again
Les


My code:

Private Sub Command0_Click()

Dim UserReply As String
UserReply = InputBox("Enter 'X' for option A" & vbCrLf & "Enter 'Y' for
option B", , , vbNullString)
Select Case UserReply
Case "X"
MsgBox ("option a")
Case "Y"
MsgBox ("option b")
End Select
End Sub
 
O

OldPro

Hello Jim

I tried the cose as you suggested and initially got the message that
UserReply wasn't defined, so I added Dim UserReply As String but then I get
a message that there is a type mismatch!
The code I have is below.
Hope you can help.
Thanks again
Les

My code:

Private Sub Command0_Click()

Dim UserReply As String
UserReply = InputBox("Enter 'X' for option A" & vbCrLf & "Enter 'Y' for
option B", , , vbNullString)
Select Case UserReply
Case "X"
MsgBox ("option a")
Case "Y"
MsgBox ("option b")
End Select
End Sub

message
Try it without the vbNullString:

UserReply = InputBox("Enter 'X' for option A" & vbcrlf & "Enter 'Y'
for
option B")
 
J

Jack Isaacs

OldPro
Thanks for your further suggestion ... which worked perfectly!!
Thanks again
Les
 

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

Top