Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Box Event

Status
Not open for further replies.

Jefftopia

Programmer
Jul 30, 2002
104
US
I have a combo box and a text box. Once my users click on a member of the combo box, the text box is then populated with data corresponding to the selection made in that combo box.

The problem I have is that if the user drops-down the combo box and makes a selection by typing and tabbing out (instead of simply clicking to make their selection), my text box will not be populated. I have already tried using both the Change event and the LostFocus event.

With the Change event the code is executed as soon as the user types a single letter.

The LostFocus event causes a problem when tabbing throughout the form (code executes if you tab over the combo box after a selection has already been made).
 
I am using the click event for the ComboBox to update the text box. My problem occurs when the user elects not to click to make their selection.
 

Ok lets try a combination of all 3 events

[tt]
Option Explicit

Dim HasNewValueBeenChecked As Boolean

Private Sub Combo1_Change()
HasNewValueBeenChecked = False
End Sub

Private Sub Combo1_Click()
HasNewValueBeenChecked = True
End Sub

Private Sub Combo1_LostFocus()
If HasNewValueBeenChecked = False Then Call Combo1_Click
End Sub

[/tt]

Good Luck

 
I think I have an easiest solution. Does someone know how to use the KeyAscii KeyCode to disallow user from tabbing out of combo box (KeyCode = 0).

that would solve my problem.
 

Unfortunately you will not be able to intercept the tab key without API calls, even if you use the forms keypreview = true property.

Good Luck
 
Use the Change event to indicate that a value has changed.

Use the LostFocus event to capture a changed entry and handle it.

Use the standard Click event as is.

e.g.:

Dim bChangedCombo1 As Boolean

Sub Combo1_Change()
bChanged = True
End Sub

Sub Combo1_LostFocus
If bChanged Then
bChanged = False
' validate your data here
'if the entered data is valid, then....
Combo1_Click
End If
End Sub

Sub Combo1_Click
' Update the textbox here
End Sub

NOTE: This is slightly different than VB5prgmr's suggestion, in that a LostFocus will not trigger an update unless a Change event has occurred.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top