简体   繁体   English

在 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?

Following the example here: https://support.microsoft.com/en-us/help/304302/how-to-build-a-dynamic-query-with-values-from-a-search-form-in-access I have created a search button that searches a table and seem to pull in the correct SQL statement.按照此处的示例: https : //support.microsoft.com/en-us/help/304302/how-to-build-a-dynamic-query-with-values-from-a-search-form-in-access我创建了一个搜索按钮,用于搜索一个表,并且似乎引入了正确的 SQL 语句。 However, on this form I have several textboxes (UserID, FirstName, LastName, Department) which are tied to their respective columns in the database.但是,在这个表单上,我有几个文本框(UserID、FirstName、LastName、Department),它们与数据库中各自的列相关联。 How to I update these textboxes inside the form frmSearchUsers to reflect the results of the filtered/queried table?如何更新表单 frmSearchUsers 中的这些文本框以反映过滤/查询表的结果?

I would have thought a Me.Requery would be sufficient, however the textboxes remain blank (save for txtSQL)我原以为 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

End Sub结束子

there are 4 textboxes, UserID, FirstName, LastName, Department.有 4 个文本框,UserID、FirstName、LastName、Department。

Let's say I knew that in table Customers there was a Jane Doe, but did not know her ID or department.假设我知道在客户表中有一个 Jane Doe,但不知道她的 ID 或部门。

Typing Jane into the FirstName textbox and Doe into the LastName textbox, and hitting search seems to yield the appropriate SQL query (and have confirmed that this correctly filters in SQL view on the table): SELECT * FROM Customers Where FirstName="Jane" and LastName="Doe"将 Jane 输入 FirstName 文本框并将 Doe 输入 LastName 文本框,然后点击搜索似乎会产生适当的 SQL 查询(并确认这在表的 SQL 视图中正确过滤):SELECT * FROM Customers Where FirstName="Jane" 和姓氏=“母鹿”

However the additional fields will not update - what am I doing wrong here?但是,附加字段不会更新 - 我在这里做错了什么? Is it because of how I have the control source for the textboxes I have tied to the table columns?是因为我如何拥有绑定到表格列的文本框的控件源吗?

Figured out the answer.想通了答案。 I had to define the recordset and db, and then reference the recordset results to display to the textbox after button update.我必须定义记录集和数据库,然后在按钮更新后引用记录集结果以显示到文本框。 The Code was largely the same:代码大致相同:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM