How to Display a Default Text on Combobox




How to Display a Default Text on Combobox

Normally, a combobox will be blank when a form is loaded. We will see a list of items when we click on the combo box drop-down list. In this How To, I will demonstrate how to display a default text on the combobox when form is loaded step by step below:

Step 1. Add Combo Box

  • Add Combo box
  • Name it as Combo9

We need a combobox of all customers from the Customer table. If you don’t know how to create the combobox, please see more details at: How to Create a Combo box. The customer combobox will be blank when the form is opened as shown in the picture below.

When you click on the combobox drop-down list then you will see the list of customer name so you can select the customer name you want.

In this example, I want to display the default text “Select Customer” in Gray color on the combobox when form is opened. The default text will guide the users what to do with the combobox.

Step 2. Enter VBA code under On Form Load Event Procedure

  • Under the form design view, open the Form Property Sheet
  • Select Event tab of Property Sheet
  • Select Event Procedure under the On Load
  • Click on three dots (…) next to Event Procedure
  • Enter VBA code below

VBA Code:
Private Sub Form_Load()
Me.Combo9 = "Select Customer"
Me.Combo9.ForeColor = 11250603  
End Sub

 

When the form is opened the Customer combobox will display the default text “Select Customer” in gray. When you select the customer name from the combo box, it will still display in gray color as shown below.

However, I want the color of the customer name to be black as normal again when I click on the drop-down box as shown below. The VBA code is needed for the customer combobox.

Step 3. Enter VBA code under On Got Focus Event Procedure of Combobox

  • Double click customer name combobox to open a Property Sheet
  • Select Event tab of Property Sheet
  • Select Event Procedure under the On Got Focus
  • Click on three dots (…) next to Event Procedure

VBA Code:
Private Sub Combo9_GotFocus()
Me.Combo9.ForeColor = vbBlack
End Sub

Related Video:

<iframe width=”560″ height=”315″ src=”https://www.youtube.com/embed/fcnSUR_zv6M” frameborder=”0″ allowfullscreen></iframe>




Result:

  • The Select Customer default text with gray color is displaying for the Customer name combobox

  • The customer name will display in black color when clicking on combobox

  • The selected customer name is displaying in black color.

 

Note:

you can use this method with a text like below:

Private Sub Form_Load()
Me.txtCustomer = "Enter Customer Name"
Me. txtCustomer.ForeColor = 11250603
End Sub
Private Sub txtCustomer _GotFocus()
   Me. txtCustomer.ForeColor = vbBlack
End Sub
Related Video:





Related posts

Leave a Comment