• September 22, 2021


Bring Technology Ideas with you

 Microsoft Access Combobox control to add Value List and display into  in Label-VBA Programming

Combobox is one of the favorite control for programmer or developer use to add selected values in the form. We have posted for the previous post about the controls on the form which are important, please check this link understanding-control-in-microsoft-access-form. This tutorial will bring you somehow to work on the Combobox control to add simple value list and display the result in the label.

How to add Combobox in the Form?

By going to Microsoft Access then create a blank database, you can put any name you as you want. Then go to Create Tab and in the Form Object, please select More Forms. After that select Modal Dialog Form

    Creating Modal Dialog Form

    After you the form have already created then please right click and Save the form as Fortune Teller. Why do we save this name? We save this form with this name because we want to allow users to select the value from the list then display the result in the Label for their birth month.

      Save the form name as Fortune Teller

      In the Form Design View, please go to Design tab, please select The Combobox and name its textbox with the name as Please select Month: with the Unbounded Object.

        The Combobox to store values of the Months

        In the same controls panel, please select the tool Aa as it is called Label to draw as the image below:

          Label to display selected month on the combobox

          Tips: In order to get Label store the space on the Form, please draw the label on the form then press Spacebar on your Keyboard; otherwise, it will be disappeared back as default from Microsoft Access.

          How to code with VBA to display the result of selected value from Combobox and display on Label?

          After the controls have already drawn and designed from your form. please right click on the Form and select Build Even then Code Builder. In the Even Form Load, please use this code below to add Month values into the Combobox:

            The even Form Load
            Private Sub Form_Load()
            ' set combobox to be value list
            Me.cmboMonths.RowSourceType = "value list"
            ' insert data into combobox
            Me.cmboMonths.RowSource = "Jan,Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct,Nov, Dec"
            ' hide lable show
            Me.Lbshow.Visible = False
            End Sub

            By go to Form View then result should be show as:

              After Adding the code the above

              As the values have already added into the Combobox, so you need to go to the Code Builder again then on the Combobox Change Even, please copy this code

              Private Sub cmboMonths_Change()
              Select Case cmboMonths
               Case Is = "Jan"
                  Me.Lbshow.Visible = True
                  Me.Lbshow.Caption = "Very good looking"
               Case Is = "Oct"
                  Me.Lbshow.Visible = True
                  Me.Lbshow.Caption = " Handsome and Smart"
               Case Else
                  Me.Lbshow.Visible = True
                  Me.Lbshow.Caption = "Pretty"
              End Select
              End Sub

              The above code, we have used Select Case in VBA programming language to allow user to display any message in the label. We have tried only two month values; however, you can add more if you want. The syntax of the Select Case in VBA is:

              Select Case Variable
              Case Is ="value you set"
              do something
              Case Is ="value you set"
              do something
              Case Is= "N value"
              Case Else
              End Select

              To get more information about Select Case statement, we will try to find an other in the other tutorials, please stay in touch with us. Now after completed all the code the above, please see the result:

                Result of the coding above

                Related post