Link to home
Start Free TrialLog in
Avatar of smods
smods

asked on

Getting the result for a text box from a question?

Hi all,

I have a button on my main menu to launch frm_form1

One of the fields in this form is called txt_textbox1 which has three options in it.

When the user presses the button to launch the form can a question message box open with the three options and capture this result directly from the question?

Regards

Chris
Avatar of Guru Ji
Guru Ji
Flag of Canada image

You can try creating a Custom Message Box

http://gregmaxey.mvps.org/Custom_MsgBox.htm 
Avatar of omgang
Something like

Private Sub Form_Load()

    Dim strPrompt As String, strText As String
    Dim varResponse As Variant

PromptUser:
    strPrompt = "Please select one of these options:" & vbCrLf & "1 -- This is the first option" & vbCrLF & "2 -- This is the second option" & vbCrLF & "3 -- This is the third option" & vbCrLf & "Enter 1, 2 or 3"
    varResponse = InputBoox(strMsg, , "Make your choice")
        'test the response to make sure we got what we wanted
    Select Case varResponse
        Case "1"
            strText = "some text here"

        Case "2"
            strText = "some other text here"

        Case "3"
            strText = "you get the idea"

        Case Else
            strPrompt = "Apparently you missed the part about entering a 1, 2 or 3"
            MsgBox strPrompt, , "Hello McFly!"
            GoTo PromptUser

    End Select

End Sub


OM Gang
Oops, forgot something and noticed one typo

should be varResponse = InputBox(strMsg, , "Make your choice")


     End Select

        'set the text for form field
    Me.txt_textbox1 = strText

End Sub

OM Gang
Avatar of smods
smods

ASKER

Thanks

I get a message box with an ok and cancel button and a space to type a value but it doesn't list the values in the box?  Also I'd ideally want three buttons on the box.  One labelled Option 1, Option 2, Option 3.  Is that possible?
First, I just provided an example.  You can replace the text that's displayed in the input box with the values from the list box on the form.

Second, we're pretty limited on what we can do with an actual message box or input box.  We can display text but not option buttons, etc.  To do this you can create a custom pop-up form to be used instead.  The link that write2mohit provided describes how to do this.
OM Gang
What's the row source for the list box on your form?
OM Gang
Avatar of smods

ASKER

It's a value list in a combo box

Row Source "Choice 1";"Choice 2";"Choice 3"

The message box is looking like this?
ee.JPG
My bad.  should be
varResponse = InputBox(strPrompt, , "Make your choice")

Now, to change the strPrompt so that it uses the values from your combo box you'll need to parse the row source.  Let me get back to you in a bit.
OM Gang

ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
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
Avatar of smods

ASKER

Thanks OM Gang!

Nearly there.  How would I put a carriage return after please select one of these options: text?

The user is still presented with the error message when pressing cancel instead of the action cancelling?

and finally my choices have quotes around them? but I cannot see why?

Private Sub Form_Load()

On Error GoTo Err_Form_Load

    Dim lngIndex As Long
    Dim strPrompt As String, strText As String, strRowSource As String
    Dim varResponse As Variant, varArray As Variant
    
        'get row source from combo box
    strRowSource = Me.Type.RowSource      '<--- you need to use the name of the actual combo box on your form
    
        'we'll split the string on the semicolons and populate an array with the values
    varArray = Split(strRowSource, ";")

PromptUser:
    strPrompt = "Select the type of count you want to perform:"
        'enumerate combo box array and add a line to the message prompt for each option
    For lngIndex = 0 To UBound(varArray)
        strPrompt = strPrompt & vbCrLf & lngIndex + 1 & " -- " & varArray(lngIndex)
    Next
    strPrompt = strPrompt & vbCrLf & "Please enter 1, 2, 3, etc."
    varResponse = InputBox(strPrompt, , "Make your choice")
        'test the response to make sure we got what we wanted
    Select Case varResponse
        Case "1"
            strText = "AM"

        Case "2"
            strText = "PM"

        Case "3"
            strText = "Spot Check"

        Case Else
            strPrompt = "Please select option 1, 2 or 3"
            MsgBox strPrompt, , "Error!"
            GoTo PromptUser

    End Select
    
    Me.Safe_Type = strText

Exit_Form_Load:
    Exit Sub

Err_Form_Load:
    MsgBox Err.Number & ", " & Err.Description, , "Error in Sub Form_Load of VBA Document frm_Count"
    Resume Exit_Form_Load

Open in new window

SOLUTION
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
Avatar of smods

ASKER

Wicked solution OM Gang.

Thanks

Chris
Glad it's working for you.
OM Gang