我对adodb参数查询存在以下问题,该查询因错误而失败或返回空记录集。但是,类似的DAO查询也会成功。
这一切都在MS Access中
首先,SQL-Find_Post_Code-设计为采用一个或两个参数:
PARAMETERS [Local] Text ( 255 ), FState Text ( 255 );
SELECT PCodes.postcode, PCodes.locality, PCodes.state, PCodes.long, PCodes.lat
FROM PCodes
WHERE (((PCodes.locality) Like "*" & [Local] & "*") AND ((PCodes.state)=[FState])) OR (((PCodes.locality) Like "*" & [Local] & "*") AND ((([PCodes].[state]) Like [FState]) Is Null))
ORDER BY PCodes.locality;
现在有了ADODB功能:
Public Function GetPostCode(varLocality As Variant, varState As Variant) As String
'//Purpose: Return Post Code for a varLocality and/or varState
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
On Error GoTo Handle_err
With cmd
.CommandText = "Find_Post_Code"
.CommandType = adCmdStoredProc
Set .ActiveConnection = CurrentProject.Connection
.Parameters.Append .CreateParameter("Local", adVarChar, adParamInput, , CStr(Nz(varLocality, "")))
.Parameters.Append .CreateParameter("FState", adVarChar, adParamInput, , CStr(Nz(varState, "")))
rst.CursorType = adOpenStatic
Set rst = .Execute
End With
If Not rst.EOF Then
GetPostCode = rst!postcode
End If
rst.Close
Handle_err:
If Err Then
MsgBox "Error " & Format(Err.Number) & " " & Err.Description, vbCritical, AppTitle
Err.Clear
End If
Set rst = Nothing
Set cmd = Nothing
End Function
这失败,并显示错误消息: 错误3708参数对象定义不正确。提供的信息不一致或不完整。
如果我将参数强制转换为adBSTR,则没有错误,但是结果记录集没有数据
但是,如果我使用以下DAO函数,一切都很好
Public Function GetPostCode2(Locality As String, State As String) As String
'//Purpose: Return Post Code for a varLocality and/or varState
Dim db As Database
Dim qd As DAO.QueryDef
Dim prmLocality As DAO.Parameter
Dim prmFSTate As DAO.Parameter
Dim rst As DAO.Recordset
On Error GoTo Handle_err
Set db = CurrentDb
Set qd = db.QueryDefs("Find_Post_Code")
Set prmLocality = qd.Parameters!Local
prmLocality.Value = Locality
Set prmFSTate = qd.Parameters!FState
prmFSTate.Value = State
Set rst = qd.OpenRecordset
If Not rst.EOF Then
GetPostCode2 = rst!postcode
End If
rst.Close
Handle_err:
If Err Then
MsgBox "Error " & Format(Err.Number) & " " & Err.Description, vbCritical, AppTitle
Err.Clear
End If
Set rst = Nothing
Set prmLocality = Nothing
Set prmFSTate = Nothing
Set qd = Nothing
Set db = Nothing
End Function
我不希望在ADODB和DAO之间交换和更改以实现我的目标。非常感谢您的帮助
答案 0 :(得分:0)
感谢您的输入。解决方法如下: 1.按照HansUp的建议修改SQL语句。
PARAMETERS [Local] Text ( 255 ), FState Text ( 255 );
SELECT PCodes.postcode, PCodes.locality, PCodes.state, PCodes.long, PCodes.lat
FROM PCodes WHERE (((PCodes.locality) Like "%" & [Local] & "%") AND ((PCodes.state)=
[FState])) OR (((PCodes.locality) Like "%" & [Local] & "%") AND ((([PCodes].[state])
Like [FState]) Is Null)) ORDER BY PCodes.locality;
有趣的是,如果在Access UI中使用此查询,则不会返回任何值