D
Deleted member 73419
Guest
I have the following code which I cannot get to work using VBA:
Query "z_TestQuery"
VBA code:
If I run the query directly and fill in the appropriate parameters when prompted, I get results returned so this query acts as you would expect.
But, if I run the VBA function, I do not get any results. At this point I am not concerned with the returned data itself, as I'll do something with the RecordSet in due course, but I am not able to populate the RecordSet.
I initially wondered whether I was not checking the RecordSet for the record count correctly, hence I tried two ways to get the record count in order to see if I was actually returning results.
Does anyone have any ideas as to why this doesn't work?
Thanks
Query "z_TestQuery"
Code:
SELECT PN, DE
FROM tblPN
WHERE (PN like [@Param1] AND MN like [@Param2]);
Code:
Function TestQuery2()
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim PN As String
Dim MN As String
Dim RecordsAffected As Integer
Set cn = CurrentProject.Connection
cn.CursorLocation = adUseClient
PN = "GE*"
MN = "Al*"
cm.ActiveConnection = cn
cm.NamedParameters = True
cm.CommandType = adCmdStoredProc
cm.CommandText = "z_testQuery"
cm.Parameters.Append cm.CreateParameter("@Param2", adVarChar, adParamInput, Len(MN), MN)
cm.Parameters.Append cm.CreateParameter("@Param1", adVarChar, adParamInput, Len(PN), PN)
Set rs = cm.Execute(RecordsAffected)
Debug.Print "Record count (RecordCount): " & rs.RecordCount
Debug.Print "Record count (RecordsAffected): " & RecordsAffected
Debug.Print
End Function
If I run the query directly and fill in the appropriate parameters when prompted, I get results returned so this query acts as you would expect.
But, if I run the VBA function, I do not get any results. At this point I am not concerned with the returned data itself, as I'll do something with the RecordSet in due course, but I am not able to populate the RecordSet.
I initially wondered whether I was not checking the RecordSet for the record count correctly, hence I tried two ways to get the record count in order to see if I was actually returning results.
Does anyone have any ideas as to why this doesn't work?
Thanks