• Populating text box based on option button (Word 2000 VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Populating text box based on option button (Word 2000 VBA)

    Author
    Topic
    #362058

    surrender OK, I’m stumped. I’ve been spending an embarrasing number of hours trying to get around this problem. I have three option buttons on what I will call UserForm2. Depending on which option button is selected, different sample text should appear on UserForm1. Unfortunately, what appears in this text field is very random. I could cycle through the option buttons, displaying UserForm1 after selecting each one, and get different results each time.

    I have tried putting the code to populate the text box in the Click event of the option buttons (which is what I have in the attached example), and have put it in the initialize event of UserForm1, and have even tried putting it in both.

    I have tried populating the list box by explicitly defining the Text and/or Value property of the text box (which is what I have in the attached example), and have tried populating it with a variable. I have the same results each time. brickwall

    I have added some labels to UserForm2 to make sure that the Boolean variables are being set properly. They serve no other purpose.

    PLEASE NOTE: The attached example is a crude recreation of a much larger project. I know there are probably a lot of things I could do to make it better, but it is just for the purpose of illustrating the issue mentioned above.

    To run the form, click RunForm. Choose an option button. Then click CommandButton1 to view the result in the text box. Clicking User Document should populate the text box with the text “Month YYYY”. Clicking User Document option button should populate the text box with “Month DD, YYYY”. Clicking the Technical Document option button should populate the text box with “month DD, YYYY”.

    P.S. I use the DATE method to format user input based on a variable set from the option buttons. This picks up the variable value just fine and formats the text correctly every time. I just can’t get the stinking sample text to show up properly. bash

    I would be truly greatful for any help!!
    Troy

    Viewing 0 reply threads
    Author
    Replies
    • #549009

      Troy,

      I think your main problem stems from the fact that your code displays UserForm1 before it actually assigns the value to the Textbox, so it is in fact returning th epreviuous selection. Try placing the UserForm1.Show as the last line in the UserForm2.CommandButton1_Click() event.

      I have not read all the code, but is it necessary to assign the state of the option buttons to variables. Try The following, which might get rid of the need of some variable assignments :

      Private Sub CommandButton1_Click()
      If obBusinessDocument  = True Then
          UserForm1.tbDate.Text = "Month DD, YYYY"
      ElseIf obUserDocument = True Then
          UserForm1.tbDate.Text = "Month YYYY"
      ElseIf obTechnicalDocument = True Then
          UserForm1.tbDate.Text = "month DD, YYYY"
      End If
      UserForm1.Show
      End Sub

      That of course belongs in UserForm2. Changes highlighted in red.

      Hope it helps,

      Andrew C

      • #549028

        I can’t believe it was that simple. Yes, moving the UserForm1.Show to the last step solved my problem.

        F.Y.I., the reason I was using variables is simply because they already existed and are set by the click event on the object buttons for other reasons within the larger application (which I did not include in the sample).

        Thanks so much for your help!!
        Troy blush

    Viewing 0 reply threads
    Reply To: Populating text box based on option button (Word 2000 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: