MS Access:将组合框中的选定条目插入表格

问题描述:

这是我使用MS Access所做的示例。我有一个带有人名的表和两个用于添加电话号码的文本字段。我创建了一个带有名称的列表框。我设法从列表框中插入选定的姓名,电话号码将文本字段(Tel1和Tel2)插入表(​​ContactTable)中。我使用了如下所示的脚本。如何更改此脚本以使用组合框而不是列表框。

This is an example of what I am doing using MS Access. I have a table with people names and two text fields for adding telephone numbers. I created a list box with names. I managed to insert selected names from the list box and telephone numbers form the text fields (Tel1 and Tel2) into a table (ContactTable). I used the script as shown below. How can I change this script to use combo box instead of list box.

Private Sub ListBoxEnter_Click()

    Dim Name As String
    Dim Tel1 As Integer
    Dim Tel2 As Integer

    If ListBox.ItemsSelected.Count = 1 Then
        Name = ListBox.Value
        Tel1 = Tel1field
        Tel2 = Tel2field

        values = "VALUES ("
        values = values & "'" & Person_Id & "','" & Name & "','" & Tel1 & "','" & Tel2 & "')"

        SQL = "INSERT  INTO ContactTable (Person_Id, Name, Tel1, Tel2)"
        SQL = SQL & values
        DoCmd.RunSQL SQL
        Me.Tel1.Value = Null
        Me.Tel2.Value = Null
   End If

End Sub


只需用组合框替换:

Private Sub ListBoxEnter_Click()

    Dim Name As String
    Dim Tel1 As Integer
    Dim Tel2 As Integer

    If Not IsNull(Me!ComboBox.Value) Then
        Name = Me!ComboBox.Value
        Tel1 = Tel1field
        Tel2 = Tel2field

        values = "VALUES ("
        values = values & "'" & Person_Id & "','" & Name & "','" & Tel1 & "','" & Tel2 & "')"

        SQL = "INSERT  INTO ContactTable (Person_Id, Name, Tel1, Tel2)"
        SQL = SQL & values
        DoCmd.RunSQL SQL
        Me.Tel1.Value = Null
        Me.Tel2.Value = Null
   End If

End Sub

如果组合框中有两列:

FirstName = Me!ComboBox.Value     ' The bound column, often column 0.
LastName = Me!ComboBox.Column(1)  ' The second/other column