I have this form which so far displays data if its there or lets you enter new data in the form. I have a combo box which at present inserts whatever is selected into my table.

How can I make it so the id of the item in the combobox is inserted instead of the actual text displayed.

The table where the ComboBox data comes from is like this:
PackageID: An Autonumber
Package: Description of selected package.

How can I also make it, so that when I go back to the form to view the saved data, the combobox is preselected with corresponding id.

Heres my code so far.

Private Sub Form_Activate()
    'On Error GoTo errHandler
    Dim sSql As String
    Dim adoRSA As ADODB.Recordset
    Dim adoField As ADODB.Field
    Dim lCol As Long
    Dim lRow As Long
    Dim lWidth As Long
    Dim labEntry As Label
    Dim txtEntry As TextBox
    Dim vArr As Variant
    Dim lX As Long
    Dim sHide As String
    If Me.Tag = "loaded" Then Exit Sub
    Me.Tag = "loaded"
    Me.Icon = frmAccount.Icon
    'txtEdit.Visible = False
    'load IP details
        sSql = "SELECT IPdetails.* " & _
            "FROM IPdetails WHERE Clis = " & glCli
        Set adoRS1 = gadoConnDB.Execute(sSql)
        If Not adoRS1.EOF Then
            'If adoRS1(tickProgrammed.DataField) Then
                'tickProgrammed.Value = 1
                'tickProgrammed.Value = 0
            'End If
            For Each txtEntry In txtIPDetails
                txtEntry.Text = adoRS1(txtEntry.DataField)
                'If tickProgrammed.Value Then
                    'txtEntry.BackColor = &H8080FF
                    'txtEntry.BackColor = &H80000005
                'End If
            'framePBX.Tag = .TextMatrix(.Row, 2)     'save lCli
            For Each txtEntry In txtIPDetails
                txtEntry.Text = ""
                txtEntry.BackColor = &H80000005
            'tickProgrammed.Value = 0
            'framePBX.Tag = .TextMatrix(.Row, 2)     'save lCli
        End If

    Dim strSQL As String 'Declare the variables we need
    'Load the data
    '** change this SQL to load the data you want.
    strSQL = "SELECT * FROM PackageType"
    '** change oConn to the name of your Connection object
    Set adoRScombo1 = gadoConnDB.Execute(strSQL)
    'Fill the combo box (or ListBox)
    '** change the name of the combo to the one you want to fill
    With CboPackageType
    Do While Not adoRScombo1.EOF
    '** change the name of the field here to the one you want to show
    .AddItem adoRScombo1.Fields("Package").Value
    End With
    'Tidy up
    Set adoRScombo1 = Nothing

End Sub

Private Sub cmdUpdateContact_Click()
    Dim sSql As String
    Dim adoRS1 As New ADODB.Recordset
    Dim txtEntry As TextBox
    'update ip data
    sSql = "SELECT IPdetails.* " & _
            "FROM IPdetails WHERE Clis = " & glCli
    adoRS1.Open sSql, gadoConnDB, adOpenStatic, adLockOptimistic

    If adoRS1.EOF Then
    End If
    adoRS1("Clis") = glCli
    adoRS1("dtype") = CboPackageType
    'If tickProgrammed.Value = 0 Then
        'adoRS1(tickProgrammed.DataField) = False
        'adoRS1(tickProgrammed.DataField) = True
    'End If
    For Each txtEntry In txtIPDetails
        adoRS1(txtEntry.DataField) = Trimmer(txtEntry.Text)

    cmdUpdateContact.Enabled = False

End Sub
10 Years
Discussion Span
Last Post by nth
'strSQL is the query string
'dName is the Fieldname to be displayed
'dID is the ID field to be inserted on the Itemdata
'cbo is the name of the combobox that will contain the datas

Sub populateComboBox(ByVal strSql As String, ByVal dName As String, ByVal dID As String, ByVal cbo As ComboBox)
Dim rs As New ADODB.Recordset
Dim n As Integer
n = 0
If rs.State = 1 Then rs.Close
rs.Open strSql, myDe.Conn, 1, 3
    If rs.RecordCount >= 1 Then
           Do While Not rs.EOF
            If IsNull(rs.Fields(dName)) Then
            cbo.AddItem ("")
            cbo.AddItem (rs.Fields(dName))
            End If
            cbo.ItemData(n) = rs.Fields(dID)
        n = n + 1
    End If
End Sub

hope this would help you.



Use .additem feature of the combo box to add the ids into the combo box. The help file guides u to do that.

Hope it helps...




i tried commetburn's example
cbo.ItemData(n) = rs.Fields(dID)
but it returns a type mismatch error.
My dID field is String...what do i do??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.