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

时间:2017-07-03 13:39:48

标签: ms-access access-vba

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

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

1 个答案:

答案 0 :(得分:1)

只需用组合框代替:

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