我有一个运行查询的访问表单。但是它使用vba代码运行查询,有时它会在表中搜索文本字段,有时会搜索数字字段,具体取决于他们在表单中的组合框中选择的字段。
我留下了一条说明,如果他们希望搜索文本字段,则必须输入双引号或代码不起作用。但是,如果用户没有遵循这些指示,他们将获得一个弹出窗口,通过选项debug和end来解决编码问题。我不希望他们看到这条消息。
有没有办法抑制此错误消息并编写自己的错误消息?
修改 昏暗的dbsCurrent作为数据库 Dim qryTest作为QueryDef
varWhere = "WHERE InclusiveParent." & Combo0.Value & "=" & Text2
varWhere = "select Location, IcmService, IcmScript, ThresholdVariable, PbxVdn, Domestic, FirstSecondLook, DNIS, Tollfree, Outdial, Description, Carrier, DefaultTollfree, BlockedRoute, Variable3, Variable4, Variable5, Variable9, ValueScrVdn, Cvp from InclusiveParent " & varWhere
'Filter frmCustomers based on search criteria
'Dim dbsCurrent As Database
'Dim qryTest As QueryDef
Set dbsCurrent = CurrentDb
Set qryTest = dbsCurrent.QueryDefs("Broaden")
qryTest.SQL = varWhere
'Close frmSearch
DoCmd.Close acForm, "SearchDependents"
InclusiveParent是我正在查询的查询,而Broaden是requery。 SearchDependents是表单的名称。 Combo0是一个组合框,可让他们选择要筛选的字段。 Text2是他们输入过滤条件的文本字段。但是,并非所有字段都是数字,因此当他们选择按文本字段过滤时,必须输入双引号或代码失败。
答案 0 :(得分:1)
尝试此操作并删除需要引号的注释:
varWhere = "WHERE InclusiveParent." & Combo0.Value & "="
If IsNumeric(Text2.Value) Then
varWhere = varWhere & Text2.Value
Else
varWhere = varWhere & """" & Text2.Value & """"
End If
答案 1 :(得分:1)
由于Combo0.Value是InclusiveParent查询中字段的名称,因此请检查该字段的数据类型。您可以使用该信息来确定是否需要将Text2.Value包装在引号中。通过了解字段的数据类型,您还可以验证Text2.Value ...当Combo0.Value是数字字段时,请确保它是有效数字。这还可以确保当用户仅输入Text2.Value的数字但Combo0.Value是文本字段时,确保引用该值。
Select Case dbsCurrent.QueryDefs("InclusiveParent").Fields(Me.Combo0.Value).Type
Case dbBigInt, dbByte, dbCurrency, dbDecimal, dbDouble, _
dbFloat, dbInteger, dbLong, dbSingle
If Not IsNumeric(Me.Text2.Value) Then
'* warn user and request a valid number *'
Else
'* build WHERE clause without quotes around Text2.Value *'
End If
Case dbChar, dbMemo, dbText
'* build WHERE clause with quotes around Text2.Value *'
Case Else
'* decide what you want for field which is neither text nor numeric *'
End Select