简体   繁体   English

MS Access搜索表单查询

[英]MS Access search form query

I am new to Access and am need of help with multi-text box search form in Access 2016. My form has 6 fields in which users can input data to get search result in the subform. 我是Access的新手,并且需要Access 2016中的多文本框搜索表单的帮助。我的表单有6个字段,用户可以在其中输入数据以在子表单中获取搜索结果。 Users would need to have the option to enter search parameters in one or more fields to get results. 用户将需要选择在一个或多个字段中输入搜索参数以获得结果。 We have a similar form in an Access 2003 database that people love. 人们喜欢的Access 2003数据库具有类似的形式。 I have tried copying and updating the code from the Access 2003 database, but I can't seem to get it to work in the 2016 database. 我曾尝试从Access 2003数据库复制和更新代码,但似乎无法在2016数据库中使用它。 I have spent weeks searching for answers and am at a total loss. 我花了数周时间寻找答案,但全然不知所措。

The below code is what I copied from the Access 2003 database and updated for the current database: 下面的代码是我从Access 2003数据库复制并为当前数据库更新的代码:

Private Sub cmdWCSearch_Click()
Dim strsql As String

strsql = "SELECT * FROM qryWCSearch WHERE ID > 0"

If Not IsNull(Me.WCLastName) Then
strsql = strsql & "And [WCLastName] Like '*" & Me.WCLastName & "*'"
End If

If Not IsNull(Me.WCDOI) Then
strsql = strsql & "And [WCDOI] Like '*" & Me.WCDOI & "*'"
End If

If Not IsNull(Me.WCWorkStatus) Then
strsql = strsql & "And [WCWorkStatus] Like '*" & Me.WCWorkStatus & "*'"
End If

If Not IsNull(Me.WCClaimNumber) Then
strsql = strsql & "And [WCClaimNumber] Like '*" & Me.WCClaimNumber & "*'"
End If

If Not IsNull(Me.WCBodyPart) Then
strsql = strsql & "And [WCBodyPart] Like '*" & Me.WCBodyPart & "*'"
End If

If Not IsNull(Me.WCClaimStatus) Then
strsql = strsql & "And [WCClaimStatus] Like '*" & Me.WCClaimStatus & "*'"
End If

End Sub

In the query I have Like "*" & [Forms]![WelcomePage]![WCLastName] & "*" Or ([Forms]![WelcomePage]![WCLastName] Is Null) under the criteria. 在查询中,条件下有Like "*" & [Forms]![WelcomePage]![WCLastName] & "*" Or ([Forms]![WelcomePage]![WCLastName] Is Null)

Whenever I run the search I get an error message saying "The expression On Click you entered as the event property setting produced the following error: A problem occurred while Microsoft Access was communicating with the OLE server or ActiveX Control." 每当我运行搜索时,我都会收到一条错误消息,提示“事件属性设置中输入的单击时表达式产生了以下错误:Microsoft Access与OLE服务器或ActiveX控件通信时发生了问题。”

Any help in getting this to work would be greatly appreciated! 任何帮助使此工作正常进行,将不胜感激!

You need a space in front of all your "And"s: 您需要在所有“与”前加一个空格:

If Not IsNull(Me.WCLastName) Then
    strsql = strsql & " And [WCLastName] Like '*" & Me.WCLastName & "*'"
End If

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

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