• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Replace tables in MS SQL via MSaccess 2003

Hi guys
I am using VBA to replace tables in sql 2005 and having a problem.
This action consists of 3 steps:
1 Delete the tables to be replaced.
2. Refresh the Database.
3. Transfer replacement tables.
I using VBA code for steps 1 & 3  which works fine.
Is there any  code I can use for step 2?
Thanks
Dory
0
dory550
Asked:
dory550
1 Solution
 
datAdrenalineCommented:
Which database are you refreshing?  The Access file, or the SQL Server database?  Also, what exactly are you trying to refresh? --- a display of information?
0
 
dory550Author Commented:
datAdrenaline
I am trying to replace SQL tables with Access tables
I delete the SQL table with vba code but  keep getting "Over Write" error
Even though  tables were deleted,
No error occurs if I physically refresh database after table deletions.
I need to clear display of information
Dory



0
 
AshrafedesCommented:
That is mean you need you import Table from SQL server Right ?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
dory550Author Commented:
Wrong!
I am exporting tables from Access to MS SQL NOT IMPORTING
Dory
0
 
AlokJain0412Commented:
What would you like to do Tables Structures or Tables data with same table structures (Sql server and MS Access)
0
 
LowfatspreadCommented:
please be more specific in your requirements...

delete is not the same as drop when talking about databases and tables...

what are you doing ?

    deleting all the data on a table
              - the table structure remains (note for sql server you may wish to use TRUNCATE instead of DELETE)
   or dropping the table
              - the physical table (including its data) is removed from the database.

why would you attempt this via VBA the standard approach would be to generate script files for the DBA to apply...
alternatively a SSIS package would be developed...
0
 
datAdrenalineCommented:
Since you are getting the Overwrite error and you indicate you are EXPORTING your Access tables to SQL Server, it is my guess that you are not DROPing your SQL Server tables and you are simply DELETEing the records within them.  So with that being the case you can move your Acces (Jet/ACE) data by issuing a SQL statement that INSERTs your Access data IN an existing SQL Server table with a single SQL statement.  The VBA to do such a task looks like this:
Public Sub AccessToSQLServer(strAccessTableName As String, strSQLServerTableName As String)
    
    Dim strSQL As String
    Dim strODBCConnect As String
    
    'Define the connect string (same as what a linked table connect property would be)
    strODBCConnect = "[ODBC" & _
                     ";DRIVER=SQL Server;SERVER=someServernameAndInstance" & _
                     ";DATABASE=someSQLDatabase;Network=DBMSSOCN" & _
                     ";Trusted_Connection=Yes]"
    
    
    'Build the INSERT INTO SQL Statement that will export the data to SQL Server
    'through the "portal" query we just modified
    strSQL = "INSERT INTO " & strODBCConnect & ".[" & strSQLServerTableName & "]" & _
             " SELECT * FROM [" & strAccessTableName & "]"
             
    'Execute the statment, which will dump the Access data to SQL Server
    CurrentDb.Execute strSQL, dbFailOnError + dbSeeChanges
    
End Sub

Open in new window


The assumption is that the source and destination tables have the same field names and the field names are defined in the same order.  If that is not the case, then you will have to add the fields to the IINSERT INTO clause and SELECT clauses respectively.  Alos, you will have to substitute the "some*" references in the strODBCConnect variable to reflect your configuration.

0
 
dory550Author Commented:
datAdrenaline
Thanks for your input
Sorry I took so long to response
Anyway
I AM DROPPING THE TABLES!
Here is a sample of the code I am using to remove and replace a sql table:


Function Movetable2Sql()
deletetSQLables 'see function below
'I need stop after I drop table, refresh the database and resume
'If not I get an error
stop
    DoCmd.TransferDatabase acExport, "ODBC", "ODBC;DRIVER={SQL Server};SERVER=TECRA\SQLEXPRESS;Database=tking;Trusted_Connection=Yes", acTable, "picture", "picture", False
End Function

Function deletetSqlTables()
On Error Resume Next
Dim sSQL As String
Dim ObjConn As New ADODB.Connection
ObjConn.Open "DSN=pkpkc"
    sSQL = "drop table picture"
    ObjConn.Execute sSQL, dbSeeChanges
        Set Ogjconn = Nothing
    deleteSqlTables = True
    On Error GoTo 0
End Function


0
 
datAdrenalineCommented:
I personally don't like to DROP things I am going to just recreate.  I consider it a greater risk to mess with the schema than to work with existing schema and just delete / re-insert data.  With that in mind, maybe the following code would be an option for you:
Public Sub AccessToSQLServer(strAccessTableName As String, strSQLServerTableName As String)
    
    Dim strSQL As String
    Dim strODBCConnect As String
    Dim db As DAO.Database
    
    Set db = CurrentDb
    
    'Define the connect string (same as what a linked table connect property would be)
    strODBCConnect = "[ODBC" & _
                     ";DRIVER=SQL Server;SERVER=TECRA\SQLEXPRESS" & _
                     ";DATABASE=tking;Network=DBMSSOCN" & _
                     ";Trusted_Connection=Yes]"
    
    'Build the SQL to delete all records in the destination table and execute it.
    strSQL = "DELETE FROM " & strODBCConnect & ".[" & strSQLServerTableName & "]"
    db.Execute strSQL, dbFailOnError + dbSeeChanges
    
    'Build the INSERT INTO SQL Statement that will export the data to SQL Server
    strSQL = "INSERT INTO " & strODBCConnect & ".[" & strSQLServerTableName & "]" & _
             " SELECT * FROM [" & strAccessTableName & "]"
             
    'Execute the statment, which will dump the Access data to SQL Server
    db.Execute strSQL, dbFailOnError + dbSeeChanges
    
End Sub

Open in new window


Back to your original question of "Refresh the database" ... we have expressed that we really don't know what you are meaning here --- do you want to refresh the list of tables from somewhere or ??? What needs to be refreshed after your code runs -- or rather what does not reflect what you expect see, and when do you expect to see it?
0
 
dory550Author Commented:

datAdrenaline
After checking again I discovered my error
my code works ok now
no need to refresh database,
However
I read your comment and agree with it
Sooo
I am replacing my code to match your reccomendations.
Thank you for your help
Dory

0
 
dory550Author Commented:
thanks
Dory
0
 
datAdrenalineCommented:
You are most welcome Dory!  I wish you success with your project!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now