在 MS Access 中,创建动态查询后,如何使用记录集中的相应值更新表单上的文本框?

in MS Access, after creating a dynamic query, how does one update the textboxes on the form with the corresponding values from the recordset?

按照此处的示例:https://support.microsoft.com/en-us/help/304302/how-to-build-a-dynamic-query-with-values-from-a-search-form-in-access 我创建了一个搜索按钮来搜索 table,并且似乎提取了正确的 SQL 语句。但是,在这个表单上我有几个文本框(用户 ID、名字、姓氏、部门),它们与数据库中各自的列相关联。如何更新 frmSearchUsers 表单中的这些文本框以反映 filtered/queried table?

的结果

我原以为 Me.Requery 就足够了,但是文本框仍然空白(除了 txtSQL)

Private Sub cmdSearch_Click()
On Error Resume Next

Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String

'Initialize the Where Clause variable.
sWhereClause = " Where "

'Start the first part of the select statement.
sSQL = "select * from customers "

'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
    With ctl
        'The only Control you are using is the text box.
        'However, you can add as many types of controls as you want.
        Select Case .ControlType
            Case acTextBox
                .SetFocus
                'This is the function that actually builds
                'the clause.
                If sWhereClause = " Where " Then
                    sWhereClause = sWhereClause & BuildCriteria(.Name, dbtext, .Text)
                Else
                    sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbtext, .Text)
                End If
        End Select
    End With
Next ctl

'Set the forms recordsource equal to the new
'select statement.
Me.txtSQL = sSQL & sWhereClause
Me.RecordSource = sSQL & sWhereClause
Me.Requery

结束子

有 4 个文本框,UserID、FirstName、LastName、Department。

假设我知道 table 客户中有一个 Jane Doe,但不知道她的 ID 或部门。

在 FirstName 文本框中键入 Jane,在 LastName 文本框中键入 Doe,点击搜索似乎会产生适当的 SQL 查询(并已确认这在 SQL 视图中正确过滤 table): SELECT * 来自名字="Jane" 和姓氏="Doe"

的客户

但是附加字段不会更新 - 我在这里做错了什么?是因为我如何获得绑定到 table 列的文本框的控制源?

找到答案了。我必须定义记录集和数据库,然后在按钮更新后引用记录集结果以显示到文本框。代码大致相同:

Private Sub btnSearch_Click()

On Error Resume Next

Dim strSQL As String
Dim ctl As Control
Dim strWhereClause As String
Dim db As Database
Dim rs As DAO.Recordset
Dim textBox As Control
Dim finalSQL As String
Set db = CurrentDb

'Init beginning of SQL select statement
sWhereClause = " Where "
sSQL = "SELECT * FROM Customers "

'Loop through filled in Controls on form to get value
For Each ctl In Me.Controls
    With ctl
        Select Case .ControlType
            Case acTextBox
            .SetFocus
            If sWhereClause = " Where " Then
                sWhereClause = sWhereClause & BuildCriteria(.Name, dbText, .Text)
            Else
                sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbText, .Text)
            End If
        End Select
    End With
Next ctl

Me.txtSQL = sSQL & sWhereClause
finalSQL = sSQL & sWhereClause

Set rs = db.OpenRecordset(finalSQL)
If rs.RecordCount > 0 Then
'If Matching Record(s) are found, pull result into appropriate fields
    Me.UserID = rs!UserID
    Me.FirstName = rs!FirstName
    Me.LastName = rs!LastName
    Me.Department = rs!Department

MsgBox "No Users Found Matching Specified Search Criteria.", vbOKCancel, "No Results Found"
End If