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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-Selecting a value in a combo box

Status
Not open for further replies.
Nov 15, 2000
322
US
I am attempting to provide users with a combo box to select a time in 30 minute intervals between 6:00a and 7:00p. I then store that time (along with a date captured in a 3rd party date control) in a SQL table in a datetime field.

This works fine. When I retrieve the recordset, I can get the date control to set from the datetime field, but not the combo box for the time.

This is where I set the valid values for the time combo
Code:
    cCombo.AddItem ("6:00 AM")
    cCombo.AddItem ("6:30 AM")
    cCombo.AddItem ("7:00 AM")
    cCombo.AddItem ("7:30 AM")
    cCombo.AddItem ("8:00 AM")
    cCombo.AddItem ("8:30 AM")
    cCombo.AddItem ("9:00 AM")
    cCombo.AddItem ("9:30 AM")
    cCombo.AddItem ("10:00 AM")
    cCombo.AddItem ("10:30 AM")
    cCombo.AddItem ("11:00 AM")
    cCombo.AddItem ("11:30 AM")
    cCombo.AddItem ("12:00 PM")
    cCombo.AddItem ("12:30 PM")
    cCombo.AddItem ("1:00 PM")
    cCombo.AddItem ("1:30 PM")
    cCombo.AddItem ("2:00 PM")
    cCombo.AddItem ("2:30 PM")
    cCombo.AddItem ("3:00 PM")
    cCombo.AddItem ("3:30 PM")
    cCombo.AddItem ("4:00 PM")
    cCombo.AddItem ("4:30 PM")
    cCombo.AddItem ("5:00 PM")
    cCombo.AddItem ("5:30 PM")
    cCombo.AddItem ("6:00 PM")
    cCombo.AddItem ("6:30 PM")
    cCombo.AddItem ("7:00 PM")


This is where I'm attempting to retrieve the time value from the recordset. strHold is set correctly, but the second line always leaves cCombo.Text = ""

Code:
strHold = Trim(Format(cCase.P_ADO_Recordset("INSPECT_DT"), "H:MM AMPM"))

cCombo.Text = strHold

strHold is equal to one of the values in the .AddItem routine above, but the combo box has a blank as its retrieved value.

Combo Style is 2-Dropdown List because I don't want the users to be able to free-form data. If I change the style to 0-Dropdown Combo, it works. But this allows the user to enter their own values.

Any ideas on how I can get this time value from the datetime field in the recordset to set the combo box to the right selection and keep the users locked out of free-form?


Monkeylizard
Sometimes just a few hours of trial and error debugging can save minutes of reading manuals.
 
You need to send CB_FINDSTRINGEXACT message to the combo box to find the string (strHold).

Add following declarations to your code.
___
[tt]
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const CB_FINDSTRINGEXACT = &H158[/tt]
___

Now insert the following code.
___
[tt]
Dim strHold As String
strHold = Trim(Format(cCase.P_ADO_Recordset("INSPECT_DT"), "H:MM AMPM"))
cCombo.ListIndex = SendMessage(cCombo.hwnd, CB_FINDSTRINGEXACT, 0, ByVal strHold)[/tt]
___

SendMessage function will search the combo box for the string and return its index. This will be assigned to the ListIndex of the combo box.
 
Thanks Hypetia.

I was being an idiot. I was loading the values to the combo box AFTER doing
Code:
strHold = Trim(Format(cCase.P_ADO_Recordset("INSPECT_DT"), "H:MM AMPM"))

cCombo.Text = strHold

Now that I fixed that, my original code works just fine. I'll file your tip away as that seems pretty useful for quickly identifying the index values in a combo.

Monkeylizard
Sometimes just a few hours of trial and error debugging can save minutes of reading manuals.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top