我有访问数据库和那里的几个查询。
最近我在表单中添加了一个带有两个单选按钮(值:1和2)的选项组。
我尝试实现的是:当第一个单选按钮被选中时,查询应仅返回国家/地区的数据;当第二个被检查时,查询应该只返回城市的数据。
我创建了一些代码:
Private Sub Command54_Click()
Dim dtps As String
If Frame45.Value = 1 Then
dtps = "101,103,1104,1105"
Else
dtps = "105,125,127,129,131,133,145,147,149,151,153,171,173,175,177,179,181,1105,1125,1127,1129,1131,1133,1141,1145,1147,1149,1151,1153,104,124,126,128,130,132,144,146,148,150,152,170,172,172,176,178,180,1104,1124,1126,1128,1130,1132,1144,1146,1146,1148,1150,1152"
End If
DoCmd.OpenQuery "test1", acViewNormal, acEdit
End Sub
现在查询“test1”非常简单:
"Select * from MyTable"
我的想法是将其改为:
"Select * from MyTable Where CountryCodeID IN ( @dtps )"
有人知道怎么做吗?
我还尝试使用功能:
我的查询代码:
Select * from MyTable Where CountryCodeID IN ( getcountrycode() )
功能代码是:
Private Sub Command54_Click()
'MsgBox Frame45.Value
DoCmd.OpenQuery "test1", acViewNormal, acEdit
End Sub
Public Function getcountrycode()
Dim dtps As String
If Frame45.Value = 1 Then
dtps = "101,103,1104,1105"
Else
dtps = "101,103,105,125,127,129,131,133,145,147,149,151,153,171,173,175,177,179,181,1105,1125,1127,1129,1131,1133,1141,1145,1147,1149,1151,1153,104,124,126,128,130,132,144,146,148,150,152,170,172,172,176,178,180,1104,1124,1126,1128,1130,1132,1144,1146,1146,1148,1150,1152"
End If
getcountrycode = dtps
End Function
它返回错误:“表达式
中的未定义函数'getcountrycode'答案 0 :(得分:0)
简单地构建查询可能更容易:
sSQL = "SELECT * FROM MyTable Where CountryCodeID IN ( " & dtps & " )"
If Not IsNull(DLookup( _
"Test1", "MSysObjects", "[Name]='Test1' AND Type= 5")) Then
''Query exists, overwrite the sql permanently
CurrentDb.QueryDefs("Test1").SQL = sSQL
Else
''Note that you cannot have a query with the same name as a table,
''so you might need to check that, too.
CurrentDb.CreateQueryDef "Test1", sSQL
End If
DoCmd.OpenQuery "test1", acViewNormal, acEdit
我强烈建议您将查询绑定到表单,而不是简单地打开查询。