如何使excel combobox.value从访问数据库

时间:2016-12-02 03:59:30

标签: excel vba excel-vba ms-access

我有一个组合框,其中包含来自访问数据库的值,如何将组合框作为触发器。选择一个值后,其他文本框会自动填充来自访问数据库的相应值吗?感谢

On Error GoTo UserForm_Initialize_Err
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
         "Data Source=E:\Database.accdb"
rs.Open "specification", cn, adOpenStatic
rs.MoveFirst
With Me.ComboBox1
    .Clear
    Do
        .AddItem rs![SerialNoCubicle]
        rs.MoveNext
    Loop Until rs.EOF
End With


rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
Exit Sub

此代码用于调用组合框中显示的数据库中的值

更新

我找到了解决方案,但我的代码很漂亮,如果有可能,需要有人帮我简化代码

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String

Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ace.OLEDB.12.0; " & _
"Data Source=E:\Database.accdb"
Set rs = New ADODB.Recordset

sql1 = "Select * FROM specification Where SerialNoCubicle = '8'"
sql2 = "Select * FROM specification Where SerialNoCubicle = '17'"
sql3 = "Select * FROM specification Where SerialNoCubicle = '18'"

    If TextBox8.Value = 8 Then
        rs.Open sql1, cn
    With rs
        TextBox1.Value = rs.Fields("Project").Value
        TextBox2.Value = rs.Fields("ProjectNo").Value
        TextBox3.Value = rs.Fields("No&DateofDrw").Value
        TextBox4.Value = rs.Fields("DrawingNumber").Value
        TextBox5.Value = rs.Fields("NameofCubicle").Value
        TextBox6.Value = rs.Fields("SingleLineLayout").Value
        TextBox7.Value = rs.Fields("PlantofTest").Value
        TextBox9.Value = rs.Fields("TypeofProduct").Value
        TextBox10.Value = rs.Fields("IPofProduct").Value
        TextBox11.Value = rs.Fields("Substation").Value
    End With
    End If

    If TextBox8.Value = 17 Then
        rs.Open sql2, cn
    With rs
        TextBox1.Value = rs.Fields("Project").Value
        TextBox2.Value = rs.Fields("ProjectNo").Value
        TextBox3.Value = rs.Fields("No&DateofDrw").Value
        TextBox4.Value = rs.Fields("DrawingNumber").Value
        TextBox5.Value = rs.Fields("NameofCubicle").Value
        TextBox6.Value = rs.Fields("SingleLineLayout").Value
        TextBox7.Value = rs.Fields("PlantofTest").Value
        TextBox9.Value = rs.Fields("TypeofProduct").Value
        TextBox10.Value = rs.Fields("IPofProduct").Value
        TextBox11.Value = rs.Fields("Substation").Value
    End With
    End If

    If TextBox8.Value = 18 Then
        rs.Open sql3, cn
    With rs
        TextBox1.Value = rs.Fields("Project").Value
        TextBox2.Value = rs.Fields("ProjectNo").Value
        TextBox3.Value = rs.Fields("No&DateofDrw").Value
        TextBox4.Value = rs.Fields("DrawingNumber").Value
        TextBox5.Value = rs.Fields("NameofCubicle").Value
        TextBox6.Value = rs.Fields("SingleLineLayout").Value
        TextBox7.Value = rs.Fields("PlantofTest").Value
        TextBox9.Value = rs.Fields("TypeofProduct").Value
        TextBox10.Value = rs.Fields("IPofProduct").Value
        TextBox11.Value = rs.Fields("Substation").Value
    End With
    End If

rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing

1 个答案:

答案 0 :(得分:0)

使用_change事件

Private Sub ComboBox1_Change()

    'or create your own query
    qry = "SELECT [FIELD1] FROM [TABLE] WHERE [FIELD2] = " & Me.ComboBox1.value & ";"

    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
     "Data Source=E:\Database.accdb"

    rs.open qry, cn
    Me.Textbox1.Value = rs![FIELD1]

    'rinse and repeat for other text boxes

    rs.close
    cn.close

End Sub