如何将ORDER BY语句添加到VBA搜索功能

时间:2017-06-28 21:22:18

标签: access-vba

我有一个Access 2010表单,其中有一个“搜索”按钮。搜索工作完美,但我希望它返回按ID,LastName,FirstName排序的记录,我无法弄清楚如何做到这一点。以下是我目前正在使用的工作VBA。非常感谢任何帮助>

Private Sub SearchCrystals_Click()
If Len(cboSearchField) = 0 Or IsNull(cboSearchField) = True Then
        MsgBox "You must select a field to search."

    ElseIf Len(txtSearchString) = 0 Or IsNull(txtSearchString) = True Then
        MsgBox "You must enter a search string."

    Else

        'Generate search criteria
        GCriteria = cboSearchField.Value & " LIKE '*" & txtSearchString & "*'"

        'Filter qry_crystals_specimens_formsearchDNC based on search criteria
        Form_frm_crystals_specimens_master.RecordSource = "select * from qry_crystals_specimens_formsearchDNC where " & GCriteria
        Form_frm_crystals_specimens_master.Caption = "qry_crystals_specimens_formsearchDNC (" & cboSearchField.Value & " contains '*" & txtSearchString & "*')"

        'Close frmSearch
        DoCmd.Close acForm, "frm_search_crystals_specimens"

        MsgBox "Results have been filtered."

    End If
End Sub

1 个答案:

答案 0 :(得分:0)

这应该做:

Dim Sql As String

Sql = "select * from qry_crystals_specimens_formsearchDNC where " & GCriteria & " order by LastName, FirstName, ID"

Debug.Print Sql

Form_frm_crystals_specimens_master.RecordSource = Sql