copying data from a SQL server to a MDB table

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an elegant way I can insert data into a table in my mdb file from a
SQL table using a single ADO call? Or do I just need to open two recordsets
one to the SQL table and one to the local project and step through each
record?

I'm trying to come up with an elegant way to keep one of the tables in my
mdb file synced to a table on my SQL server. Any ideas would be greatly
appreciated.
 
Why not link to the SQL Server table, rather than having a copy of it in
your MDB?
 
Hi, Siggi.

I imagine that you aren't linking directly to the SQL Server table because
you don't want the Access users to be fiddling with the production version of
the data. If that's not the case, then just linking to SQL Server via a DSN
would be the easiest way for the Access users to "see" the SQL Server data.

To keep the records in Access in sync with the records in SQL Server, just
delete the records first from the Access table, then append the records from
the SQL table with a query. It would be easiest to create a temporary link
to the SQL Server table and append the records to the Access table with a
simple append query, then delete the link. For example, these two queries:

DELETE *
FROM tblAccess;


INSERT INTO tblAccess
SELECT *
FROM tblSQLServer;

.. . . where tblAccess is the name of the local table in Access,
tblSQLServer is the name of the linked table.

Please see the following Web page for VBA code to relink the tables:

http://www.mvps.org/access/tables/tbl0009.htm

If you can't link to SQL Server, then you may still use queries to get the
data from SQL Server, either through a query or via VBA code. For example,
if the data were in another Access database, then the following VBA would
work:

sqlStmt = "DELETE * " & _
"FROM tblAccess;"
CurrentDb().Execute sqlStmt, dbFailOnError

sqlStmt = "INSERT INTO tblAccess " & _
"SELECT *
"FROM [;DATABASE=C:\Work\OtherDB.mdb;].tblInOtherDB;" & _
CurrentDb().Execute sqlStmt, dbFailOnError

.. . . where tblAccess is the name of the local table in Access,
tblInOtherDB is the name of the table in the other database, and
C:\Work\OtherDB.mdb is the path to the other Access database.

However, since you're using SQL Server, you'll need a SQL Server connection
string inside the brackets of the FROM clause. If you need help with the
connection string, then please see the following Web page for examples:

For a DSN-less connection:
http://www.carlprothman.net/Default.aspx?tabid=90#ODBCDriverForSQLServer

For a DSN connection:
http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Two reasons. The MDB needs to be functional off-line and I only need a subset
of the SQL Server table in each MDB. The main thing is to be able to maintain
an off-line capability.
 
That being the case I would link to the table on Sql, run a query to update
the local table, and finaly drop that linked table. Here is some "Aur
Code".

strCon = "ODBC;" _
& "Description=DSNLessConnection;" _
& "DRIVER=SQL Server;" _
& "SERVER=YourServerName;" _
& "UID=YourUserName;" _
& "PWD=YourPassword;" _
& "DATABASE=YourDatabase"
DoCmd.TransferDatabase acLink, "ODBC", strCon, acTable, _
SourceTableName, DestTableName, , True
db.Execute "UPDATE DestTableName INNER JOIN AccessTable " _
& "ON DestTableName.Part = AccessTable.Part " _
& "SET AccessTable.aaa= DestTableName.aaa, ...;"
db.Execute "Drop Table DestTableName"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top