I am a beginner and stumped attempting what sounds like a simple task: Repopulating Access 2007 reference tables (static) from SQL Server 2007.
I am using VBA and ADO connection to create a recordset of the SQL Server table(s) with no problem:
I can't find out how to use this recordset to replace all rows in the local (CurrentDb) table:
I get error that CurrentDb does not recognize the ADO RecordSet "rs". I have tried many variations [commented out below] to bulk load new data into an empty tblASA. Shouldn't this be easy? Does it require looping through the recordset?
Thanks.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String
strConn = "Provider=SQLOLEDB;Data Source='sql2k5.....';Initial Catalog='SQL2005...........';User Id='Int.......';Password='...........';"
Set cn = New ADODB.Connection
cn.Open strConn
' Find out if the attempt to connect worked.
If cn.State = adStateOpen Then
MsgBox "Welcome to IntraOp!"
Else
MsgBox "Sorry, no IntraOp connection."
End If
Set rs = New ADODB.Recordset
rs.Open "SELECT * From dbo.tblASA", cn, adOpenStatic, adLockOptimistic
MsgBox "rs.RecordCount = " & rs.RecordCount
'With rs
'CurrentDb.Execute "INSERT INTO tblASA SELECT * FROM RecordSet("rs");"
'CurrentDb.Execute "INSERT INTO tblASA SELECT * From dbo.tblASA IN rs;"
cn.Execute "INSERT INTO tblASA SELECT * FROM CopyFromRecordSets(rs);"
'cn.Execute " INSERT INTO tblASA SELECT * FROM rs.Recordset", , 129
'cn.Close
'End With
' dbs.Execute " INSERT INTO Customers SELECT * FROM rs;"
If Err.Number = 0 Then
MsgBox "Ran SQL"
End If
Exit Sub
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
I am using VBA and ADO connection to create a recordset of the SQL Server table(s) with no problem:
I can't find out how to use this recordset to replace all rows in the local (CurrentDb) table:
I get error that CurrentDb does not recognize the ADO RecordSet "rs". I have tried many variations [commented out below] to bulk load new data into an empty tblASA. Shouldn't this be easy? Does it require looping through the recordset?
Thanks.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String
strConn = "Provider=SQLOLEDB;Data Source='sql2k5.....';Initial Catalog='SQL2005...........';User Id='Int.......';Password='...........';"
Set cn = New ADODB.Connection
cn.Open strConn
' Find out if the attempt to connect worked.
If cn.State = adStateOpen Then
MsgBox "Welcome to IntraOp!"
Else
MsgBox "Sorry, no IntraOp connection."
End If
Set rs = New ADODB.Recordset
rs.Open "SELECT * From dbo.tblASA", cn, adOpenStatic, adLockOptimistic
MsgBox "rs.RecordCount = " & rs.RecordCount
'With rs
'CurrentDb.Execute "INSERT INTO tblASA SELECT * FROM RecordSet("rs");"
'CurrentDb.Execute "INSERT INTO tblASA SELECT * From dbo.tblASA IN rs;"
cn.Execute "INSERT INTO tblASA SELECT * FROM CopyFromRecordSets(rs);"
'cn.Execute " INSERT INTO tblASA SELECT * FROM rs.Recordset", , 129
'cn.Close
'End With
' dbs.Execute " INSERT INTO Customers SELECT * FROM rs;"
If Err.Number = 0 Then
MsgBox "Ran SQL"
End If
Exit Sub
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub