0

我在 EXCEL 中有一个带有一个文本框和一个组合框的用户表单。此用户窗体连接到一个小型数据库(一个有 2 列的表) 组合框填充了我喜欢的数据库表第一列中的值,当组合框将文本框更改为自动填充第二列中的对应值时. 我有以下代码,但它不工作:拜托,有人可以帮助我吗?

Sub PopulateTB()

    Dim rs As Recordset
    Dim db As database
    Dim SQL As String

    Set db = OpenDatabase(ThisWorkbook.Path & "\materiale.mdb")

    SQL = "SELECT values_col2 FROM table_db WHERE values_col1 = " & UserForm1.ComboBox1.Value & ";"
    Set rs = db.OpenRecordset(sql)


 Do Until rs.EOF = True
    UserForm1.TextBox1.Value = rs.Fields(SQL)
    rs.MoveNext
 Loop

   rs.Close
    Set db = Nothing
    Set rs = Nothing

End Sub

谢谢!

4

1 回答 1

0

我这样推,没关系

Sub PopulateTB(ByRef ctl As Control, ByVal strTable As String, ByVal strField As String, Optional ByVal strCriteria As String)

    Dim strSQL As String
    Dim strSQLcount As String
    Dim rs As Recordset
    Dim db As Database
    Dim rsCount As Recordset, totalCol As Long
    Dim varRecords As Variant

    Set db = OpenDatabase(ThisWorkbook.Path & "\materiale.mdb")

    strSQLcount = ""
    strSQLcount = strSQLcount & " " & "SELECT COUNT(*) AS Total FROM " & "[" & strTable & "]"
    Set rsCount = db.OpenRecordset(strSQLcount)
    totalCol = rsCount!Total
    rsCount.Close
    Set rsCount = Nothing



    strSQL = ""
    strSQL = strSQL & " " & "SELECT" & "[" & strField & "]"
    strSQL = strSQL & " " & "FROM " & "[" & strTable & "]"

    Set rs = db.OpenRecordset(strSQL)



    varRecords = rs.GetRows(totalCol)
    ctl.Value = varRecords(0, Me.ComboBox1.ListIndex)

    rs.Close
    db.Close
    Set db = Nothing
    Set rs = Nothing


End Sub
于 2013-10-02T12:29:01.203 回答