none
Populate a text based based on value selected in combo box RRS feed

  • Question

  • I would like to populate the contents of a text box based based on the selection of value in a combo box. 

    I do not have an external data source to link to, the values in my combo box were entered via the "Drop-Down Form Field Options Window."  I would like to do something like this in VBA:


         If ActiveDocument.FormFields("cboBusDesc").DropDown.Value = "Accomodation and Food Services" Then
             ActiveDocument.FormFields("txtBusCode").Value = "7200"         Exit Sub
          End If
         
         If ActiveDocument.FormFields("cboBusDesc").DropDown.Value = "Agriculture" Then
             ActiveDocument.FormFields("txtBusCode").Value = "1100"        

             
    But that doesn't work.  Can somebody help me out?
              

    Wednesday, October 15, 2014 4:09 PM

Answers

  • Hi,

    According to your description, I think you are working with Drop-Down Form Field in Word document.

    As far as I know, the first item of Drop-Down Form Field items is the one that appears by default in the Drop-Down Form Field. We can only change the selection of Drop-Down Form Field by moving item position. And there is no event to capture the change of Drop-Down Form Field. So it's hard to auto fill the content of a text box based on the selection in Drop-Down Form Field.

    In fact, to achieve your goal, I think Combo Box ActiveX control and Text Box ActiveX control are more suitable than Drop-Down Form Field and Text Form Field.

    You can resort to the Change event of the Combo Box control and set the value of Text Box based on the selected value of Combo Box.

    Here is a sample for your reference.

    Private Sub ComboBox1_Change()
        If ComboBox1.Value = "aaa" Then
            TextBox1.Value = "100"
        Else
            TextBox1.Value = "200"
        End If
    End Sub
    
    Sub ComboBox1AddItems()
    ComboBox1.Clear
    ComboBox1.AddItem "aaa"
    ComboBox1.AddItem "bbb"
    ComboBox1.AddItem "ccc"
    
    End Sub

    The result:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sandi V Friday, October 17, 2014 1:52 PM
    Thursday, October 16, 2014 7:23 AM
    Moderator
  • Hi Sandi,

    Great to hear that this works for you. You can fill the combo also in VBA. double click the "ThisDocument" object in the vba object explorer and in the editor window typ:

    Private Sub Document_Open()
        Me.cboBusDesc.AddItem "test"
    End Sub

    Of course you choose your own text here.

    Now when you open your document your combo will be filled!


    Maurice

    • Marked as answer by Sandi V Friday, October 17, 2014 1:52 PM
    Thursday, October 16, 2014 9:18 PM

All replies

  • Hi,

    According to your description, I think you are working with Drop-Down Form Field in Word document.

    As far as I know, the first item of Drop-Down Form Field items is the one that appears by default in the Drop-Down Form Field. We can only change the selection of Drop-Down Form Field by moving item position. And there is no event to capture the change of Drop-Down Form Field. So it's hard to auto fill the content of a text box based on the selection in Drop-Down Form Field.

    In fact, to achieve your goal, I think Combo Box ActiveX control and Text Box ActiveX control are more suitable than Drop-Down Form Field and Text Form Field.

    You can resort to the Change event of the Combo Box control and set the value of Text Box based on the selected value of Combo Box.

    Here is a sample for your reference.

    Private Sub ComboBox1_Change()
        If ComboBox1.Value = "aaa" Then
            TextBox1.Value = "100"
        Else
            TextBox1.Value = "200"
        End If
    End Sub
    
    Sub ComboBox1AddItems()
    ComboBox1.Clear
    ComboBox1.AddItem "aaa"
    ComboBox1.AddItem "bbb"
    ComboBox1.AddItem "ccc"
    
    End Sub

    The result:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Sandi V Friday, October 17, 2014 1:52 PM
    Thursday, October 16, 2014 7:23 AM
    Moderator
  • THANK YOU!  I got rid of the legacy fields and used ActiveX like you suggested.   Once I realized the code needs to be store in This Document, not new Macros, the combobox is working great for the change event when I type in text manually.

    Private Sub cboBusDesc_Change()
    
        If cboBusDesc.Value = "aaa" Then
            txtBusCode.Value = "100"
        ElseIf cboBusDesc.Value = "bbb" Then
            txtBusCode.Value = "200"
        ElseIf cboBusDesc.Value = "ccc" Then
            txtBusCode.Value = "300"
        
        
        End If
    
    End Sub

    However, I'm not sure how to go about populating the combo box with the .additems.

    Where does that code go?  I'm used to using "macro to run on enter" in the interface with the legacy cbos, but can't find that in the active x cbo.

    Thanks so much for your help!  I really appreciate it!

    Sandi

    Thursday, October 16, 2014 8:58 PM
  • Hi Sandi,

    Great to hear that this works for you. You can fill the combo also in VBA. double click the "ThisDocument" object in the vba object explorer and in the editor window typ:

    Private Sub Document_Open()
        Me.cboBusDesc.AddItem "test"
    End Sub

    Of course you choose your own text here.

    Now when you open your document your combo will be filled!


    Maurice

    • Marked as answer by Sandi V Friday, October 17, 2014 1:52 PM
    Thursday, October 16, 2014 9:18 PM
  • Ah -- nice!  Thanks so much.
    Friday, October 17, 2014 1:53 PM