Link to home
Start Free TrialLog in
Avatar of DennisPost
DennisPostFlag for Netherlands

asked on

How cn I a jump to an item in a vb6 combobox based on the primary key of my table

I need to have my comboboxes filled so that users can start type as soon as it has focus.
I want to set a certain item as the default so that users can skip it if they want.

I have a ado recordset with a clientside cursor that opens when the application first loads.
All the comboboxes are dropdown lists and are sorted.

The users combo boxes are filled lke this:

Public Function UsersCbo(Cbo As ComboBox, Optional ID As Byte = 0, Optional Short As Boolean = False)
On Error GoTo errorhandler
    With Cbo
        If ID = 0 Then
            While rsUser.EOF = False
                If rsUser!Blocked = False Then
                    If Short = True Then
                        .AddItem Trim(rsUser!ShortName)
                    Else: .AddItem Trim(rsUser!UserName)
                    End If
                    .ItemData(.NewIndex) = rsUser!UserId
                    Cbo = .List(0)
                End If
        ElseIf ID > 0 Then
            rsUser.Find "UserID = " & ID & ""
            If Short = False Then
                Cbo.Enabled = True
                .AddItem rsUser!UserName
            Else: .AddItem rsUser!ShortName
            End If
            Cbo = .List(0)
            Cbo.ItemData(Cbo.ListIndex) = ID
        End If
    End With
    Exit Function
    errorM "FieldFunctions, UsersCbo"
End Function

If I want to fill a combobox with all entries :UsersCbo Me.cboUser, 0, False
When I binddata to a combobox ::UsersCbo Me.cboUser, gluserID, False

Problem is that when I want to change the bound data there is only 1 item in the list.
So I fill the list again using : UsersCbo Me.cboUser, 0, False

But then I don't know how to set the combobox to glUserID again.
This field may or may not be changed. I want to prepare the combobox just in case.

This can already be done by the dropdown event but i still want users to be able to type straight away.

I have been playing around with:
cboUser.ItemData(cboUser.ListIndex) = glUserID
cboUser = cboUser.List(glUserID)

And other combinations, but just can't get it to work.
I think the fact that the comboboxes are sorted is interferring.
When i check the itemdata or listindex, then they display the right UserID, just not the right name.
I cannot use an Order By clause in the tsql when opening the recordset as the comboboxes can be filled
by either the full name of the user or their abbrviated username.

Anyone got any suggestions?

Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DennisPost


Perfect !!
That was exactly what I was looking for.