Reputation: 3
I am trying to assign variables value from recordset and insert values into an Access table. I also need to clear the table and insert new set of data before inserting. The recordset is from a stored procedure in SQL Server. Following does not seem to work:
Dim conn As ADODB.Connection, cmd As ADODB.Command, rst As
ADODB.Recordset
Dim Itm As String, JobNo As Integer, RevNo As Integer, DUStatus As Date, LDUStatus As Date, UTrigger As String
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider='sqloledb';Data Source=SERVER;Initial Catalog='Database';Integrated Security='SSPI';"
conn.Open
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = "rg_ItemsQuerySP"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter("@JobNo", adInteger, adParamInput, , TempJobNo)
.Parameters.Append .CreateParameter("@RevNo", adInteger, adParamInput, , TempRevNo)
End With
Set rst = cmd.Execute
If rst.EOF Then Exit Function
rst.MoveLast
rst.MoveFirst
With rst
Do While Not .EOF
Itm = rst.Fields("Item")
JobNo = rst.Fields("Job No")
RevNo = rst.Fields("Revision No")
DUStatus = rst.Fields("DateUpdatedStatus")
LDUStatus = rst.Fields("LastDateUpdatedStatus")
UTrigger = rst.Fields("UpdateTrigger")
DoCmd.RunSQL ("INSERT INTO ItemsQuerySP_Temp values " & Itm & ", " & JobNo & ", " & RevNo & ", " & DUStatus & ", " & LDUStatus & ", " & UTrigger & ";")
rst.MoveNext
Loop
End With
conn.Close
Set conn = Nothing
Upvotes: 0
Views: 4383
Reputation: 107587
Possibly your issue is the lack of quotes around string variables which would raise SQL error. Since you use ADO parameters, continue to use parameters via QueryDef, avoiding string concatenation (i.e., ampersands) or punctuation (i.e., quotes):
SQL (save below as an MS Access saved query, adjust types as needed: Text
, Long
, Double
, etc.)
PARAMETERS PrmItm Text, PrmJobNo Text, PrmRevNo Text,
PrmDUStatus Text, PrmLDUStatus Text, PrmUTrigger Text;
INSERT INTO ItemsQuerySP_Temp
VALUES(PrmItm, PrmJobNo, PrmRevNo,
PrmDUStatus, PrmLDUStatus, PrmUTrigger)
VBA (relevant section)
Dim qdef AS QueryDef
' ... same as above...
Set qdef = CurrentDb.QueryDefs("mySavedQuery")
With rst
Do While Not .EOF
' BIND PARAMETERS
qdef!PrmItm = rst.Fields("Item")
qdef!PrmJobNo = rst.Fields("Job No")
qdef!PrmRevNo = rst.Fields("Revision No")
qdef!PrmDUStatus= rst.Fields("DateUpdatedStatus")
qdef!PrmLDUStatus = rst.Fields("LastDateUpdatedStatus")
qdef!PrmUTrigger = rst.Fields("UpdateTrigger")
' EXECUTE ACTION
qdef.Execute dbFailOnError
.MoveNext
Loop
End With
Set qdef = Nothing
Upvotes: 1