schmir1
asked on
Copy a SQL Server Table using VBA
How do I copy a SQL Server Table using VBA?
Below is the code that I used when the table was in an Access back-end
Below is the code that I used when the table was in an Access back-end
Public Sub CheckCopyTable()
On Error Resume Next
Dim acc As Access.Application
Dim tbl As DAO.TableDef
Dim strBackend_Path As String
Dim db As Database
Dim tdf As TableDef
strBackend_Path = DB_Param("DB_ServerPath") & DB_Param("DB_BackendName")
Set acc = New Access.Application
acc.OpenCurrentDatabase (strBackend_Path)
Set tbl = acc.CurrentDb.TableDefs("Commitment_Outlook_bak")
If Err.Number <> 0 Then
If Err.Number = 3265 Then
Set db = OpenDatabase(strBackend_Path)
db.TableDefs("Commitment_Outlook_new").Name = "Commitment_Outlook_bak" 'new to bak
Else
LogEvt "Error Number " & Err.Number & vbNewLine & Err.Description, vbExclamation, "CheckCopyTable Error (SB-123)", NOMSG
End If
End If
acc.CloseCurrentDatabase
Set acc = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I would have to "CREATE TABLE MyNewTable" then do the "SELECT * INTO MyNewTable FROM MyOldTable".
You can do a straight Select * Into, but you lose the primary keys, identity fields, etc.
It depends on what you are trying to do.
It depends on what you are trying to do.
ASKER
I'm still not sure I'm following. I would like to maintain the primary keys, idendtity fields, etc. Can you give me an example of how to copy the table and all the data in it to another table and have them both have identical data structures?
You would need a tool for that. Access has an "upgrade" wizard for moving tables from Access to SQL Server but not the other way.
Try DeZign:
http://www.datanamic.com/dezign/index.html
<quote>
Supported Databases
Oracle, MS SQL Server, MySQL, IBM DB2, Firebird, InterBase, MS Access, PostgreSQL, Paradox, dBase, Pervasive, Informix, Clipper, Foxpro, Sybase, SQLite, ElevateDB, NexusDB, DBISAM More.
</quote>
/gustav
Try DeZign:
http://www.datanamic.com/dezign/index.html
<quote>
Supported Databases
Oracle, MS SQL Server, MySQL, IBM DB2, Firebird, InterBase, MS Access, PostgreSQL, Paradox, dBase, Pervasive, Informix, Clipper, Foxpro, Sybase, SQLite, ElevateDB, NexusDB, DBISAM More.
</quote>
/gustav
ASKER
This is something that I need to run in my code.
is it linked into your Access Database?
You could give the CopyObject method a go
DoCmd.CopyObject, "Employees Copy", acTable, "Employees"
J
You could give the CopyObject method a go
DoCmd.CopyObject, "Employees Copy", acTable, "Employees"
J
>> You could give the CopyObject method a go
The issue is he wants a copy of the original table -- including structure in the BE. The CopyObject just refers to a newly named front end link.
The issue is he wants a copy of the original table -- including structure in the BE. The CopyObject just refers to a newly named front end link.
ASKER
Jimpen is exactly right. I need something that runs on the SQL Server but I kick off with my VBA. Here is basically what the code needs to do:
1) I already have code that "INSERTS" the new data into "Commitment_Outlook_bak"
2) If successful then I need code to copy Table "Commitment_Outlook" to Table "Commitment_Outlook_sav"
3) If this is successful then copy Table "Commitment_Outlook_bak" to "Comitment_Outlook"
Would it be hard to make a stored procedure and run it from my code. Could I do a stored procedure that copies directly into "Commitment_Outlook" and roll back if there is a failure?
1) I already have code that "INSERTS" the new data into "Commitment_Outlook_bak"
2) If successful then I need code to copy Table "Commitment_Outlook" to Table "Commitment_Outlook_sav"
3) If this is successful then copy Table "Commitment_Outlook_bak" to "Comitment_Outlook"
Would it be hard to make a stored procedure and run it from my code. Could I do a stored procedure that copies directly into "Commitment_Outlook" and roll back if there is a failure?
ASKER
I don't think my Stored Procedure idea is going to work. I just remembered that the table that I'm getting the data from is a Outlook Address list that is resident on my C drive.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I don't think the DoCmd.TransferDatabase is going to work?
I think I've found a way. See code below.
If there is no other input then I'll close this question.
I think I've found a way. See code below.
If there is no other input then I'll close this question.
Function CreateSPT(SPTQueryName As String, strSQL As String)
'Generate a Pass-Through query then run it
'Params: SPTQueryName - name of the query (will be deleted and recreated if exists),
' strSQL - query to run on SQL Server include Stored Procedures (Using EXEC)
'Example 1: CreateSPT ("Test", "Select * from Users") 'creates a Pass-Through Query called test with the "Select..." statement
'Example 2: CreateSPT("Generic_SPT", "EXEC RenameTBL ""Commitment_Outlook_bak"",""Commitment_Outlook_bak1""")
'Example 2: CreateSPT ("Generic_SPT", "EXEC sp_rename ""Commitment_Outlook_bak1"",""Commitment_Outlook_bak""") 'using system SP
Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
DoCmd.Close acQuery, SPTQueryName
On Error Resume Next
DoCmd.DeleteObject acQuery, SPTQueryName
On Error GoTo 0
Set cat = New ADOX.Catalog
Set cmd = New ADODB.Command
cat.ActiveConnection = CurrentProject.Connection
Set cmd.ActiveConnection = cat.ActiveConnection
cmd.CommandText = strSQL
cmd.Properties("Jet OLEDB:ODBC Pass-Through Statement") = True
' cmd.Properties("Return Records") = bolReturnRecords 'no worky but On Error Resume Next stopped the error
cmd.Properties("Jet OLEDB:Pass Through Query Connect String") = _
"ODBC;DSN=DTLData_Dev;database=DTLData;UID=sa;PWD=;"
cat.Procedures.Append SPTQueryName, cmd
DoCmd.OpenQuery SPTQueryName
Set cat = Nothing
Set cmd = Nothing
End Function
That looks to be the best solution.
Sorry we couldn't give you a better answer.
Sorry we couldn't give you a better answer.
ASKER
All good info although I found what I think is a good solution. Thanks for your help.
Glad to be of assistance. May all your days get brighter and brighter.
ASKER