创建表单以根据多个条件搜索记录

时间:2016-02-16 17:28:20

标签: ms-access ms-access-2016

我正在尝试创建一个表单,允许您根据多个条件返回结果。

我有FirstName字段,LastName字段和State字段。

我还有一个名为searchFirstsearchLastsearchState的文本框,用户可以在其中输入条件。

点击后,搜索按钮将执行以下代码。

Private Sub mySearchQuery_Click()

Dim filter As String

Dim rtFirstName As String
Dim rtLastName As String
Dim rtState As String

rtFirstName = Me.searchFirst.Value
rtLastName = Me.searchLast.Value
rtState = Me.searchState.Value

If Not IsNull(rtFirstName) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(FirstName like""*" & rtFirstName & "*"")"
End If


If Not IsNull(rtLastName) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(LastName like""*" & rtLastName & "*"")"
End If

If Not IsNull(rtState) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(State LIKE""*" & rtState & "*"")"
End If

' Now re-construct the SQL query '
Dim sql As String
sql = "SELECT * FROM MainData"

If Not IsNull(filter) Then
        sql = sql & " WHERE " & filter
End If

Me.RecordSource = sql
'SubForm.Form.RecordSource = sql

End Sub

我在下面收到以下错误。

  

运行时错误'3075':查询中出现语法错误(缺少运算符)   表达式'AND(FirstName like“* tracy *”)AND(lastName like“* Smith *”)   AND(状态如“* ga *”)'。

我不确定为什么AND包含在搜索查询的开头?

1 个答案:

答案 0 :(得分:0)

  

我不确定为什么在搜索开始时包含了AND   查询?

由于您有Dim filter As Stringfilter永远不能包含Null。这意味着这些If条件...... If Not IsNull(filter) ...将永远为真。

同样,Not IsNull(rtFirstName)Not IsNull(rtLastName)Not IsNull(rtState)将始终为True。

最终结果是代码为您的filter字符串添加了另一个条件,无论相应的搜索文本框是否包含任何内容,并且每个部分都以" AND "为前缀。

考虑到这些要点,只有当您在相应的搜索文本框中有内容并决定何时加入filter时,您才可以重构代码以添加" AND "段。但是我发现在每个" AND "中添加" AND "之后,最好先包含filter,然后再将WHERE添加到Private Sub mySearchQuery_Click() Dim strSelect As String Dim strWhere As String If Len(Trim(Me!searchFirst.Value) & vbNullString) > 0 Then strWhere = strWhere & " AND FirstName Like ""*" & Me!searchFirst.Value & "*""" End If If Len(Trim(Me!searchLast.Value) & vbNullString) > 0 Then strWhere = strWhere & " AND LastName Like ""*" & Me!searchLast.Value & "*""" End If If Len(Trim(Me!searchState.Value) & vbNullString) > 0 Then strWhere = strWhere & " AND State Like ""*" & Me!searchState.Value & "*""" End If ' Now re-construct the SQL query strSelect = "SELECT * FROM MainData" ' only add WHERE clause if we have something in strWhere If Len(strWhere) > 0 Then ' use Mid() to ignore leading " AND " strSelect = strSelect & " WHERE " & Mid(strWhere, 6) End If Debug.Print strSelect ' <- inspect this in Immediate window; Ctrl+g will take you there ' enable one of these RecordSource lines after confirming Debug.Print shows you what you need 'Me.RecordSource = sql 'SubForm.Form.RecordSource = sql End Sub 子句中。

var im = require('imagemagick');

var resize_options = {
    srcPath: path,
    dstPath: path,
    width: 100,
    height: 50
};
im.resize(resize_options, function (err) {
    if (err) {
        console.log(err);
        res.end('Error!');
    }
    else {
        res.end('Success!');
    }
});
相关问题