使用组合框中的值来确定在Access中搜索的字段

时间:2015-11-30 17:34:55

标签: vba access-vba

我正在尝试使用组合框中的值来选择更新文本框时将搜索哪个字段。

此代码有效,但只允许搜索PatientID:

Private Sub txtGoTo_AfterUpdate()

If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[PatientID] =" & txtGoTo
If rs.NoMatch Then
    MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
           vbOKOnly + vbInformation
Else
    Me.Recordset.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null

End Sub

此代码不起作用,但应传达我想要做的事情(更改粗体):

Private Sub txtGoTo_AfterUpdate()

**GCriteria = cboSearchField.Value & " LIKE '*" & txtSearchString & "*'"**

If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[**Gcriteria**] =" & txtGoTo
If rs.NoMatch Then
    MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
           vbOKOnly + vbInformation
Else
    Me.Recordset.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null

End Sub

1 个答案:

答案 0 :(得分:0)

我认为应该只是改变:

 rs.FindFirst "[**Gcriteria**] =" & txtGoTo

要:

 rs.FindFirst Gcriteria

因为您之前已经设定了条件:

 *GCriteria = cboSearchField.Value & " LIKE '*" & txtSearchString & "*'"**