Solved

Replace tables in MS SQL via MSaccess 2003

Posted on 2011-09-28
12
252 Views
Last Modified: 2012-05-12
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
Comment
Question by:dory550
12 Comments
 
LVL 11

Expert Comment

by:datAdrenaline
Comment Utility
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
 

Author Comment

by:dory550
Comment Utility
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
 

Expert Comment

by:Ashrafedes
Comment Utility
That is mean you need you import Table from SQL server Right ?
0
 

Author Comment

by:dory550
Comment Utility
Wrong!
I am exporting tables from Access to MS SQL NOT IMPORTING
Dory
0
 
LVL 5

Expert Comment

by:AlokJain0412
Comment Utility
What would you like to do Tables Structures or Tables data with same table structures (Sql server and MS Access)
0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:datAdrenaline
Comment Utility
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
 

Author Comment

by:dory550
Comment Utility
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
 
LVL 11

Accepted Solution

by:
datAdrenaline earned 500 total points
Comment Utility
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
 

Author Comment

by:dory550
Comment Utility

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
 

Author Closing Comment

by:dory550
Comment Utility
thanks
Dory
0
 
LVL 11

Expert Comment

by:datAdrenaline
Comment Utility
You are most welcome Dory!  I wish you success with your project!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now