MS Access:将组合框中的选定条目插入表格 [英] MS Access: Insert a selected entry from Combo Box into table

查看:104
本文介绍了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

这篇关于MS Access:将组合框中的选定条目插入表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆