2

I have a problem with this instruction in VBA that tries to import data from an external SQL Server DB into a local MS Access DB:

Conectar = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\VENTAS\VENTAS.mdb;" _
            & "Persist Security Info=False;"

cn.ConnectionString = Conectar    
cn.Open

SQL= "insert into PRO (COD_PRO,DESC_PRO) " & _
     "select COD_ARTICU,DESCRIPCIO  " & _
     "FROM OPENDATASOURCE ('SQLNCLI', 'Data Source=VAIO\SQLEXPRESS;User" & _ 
     "ID=XXX;Password=XXX;').ACPE.dbo.STA11 where perfil<>'N';"

cn.Execute Sql

The error

"Syntax error in FROM clases"

It seems that I can't use OPENDATASOUCE to an SQL Server from Access?

2 Answers 2

2

You need to reverse the query reference. OPENDATASOURCE is a SQL Server TSQL command and is not an MS Access SQL command. Consider running an ODBC inline of a stored query:

SQL (save as stored query or script as VBA string)

INSERT INTO PRO (COD_PRO, DESC_PRO) 
SELECT COD_ARTICU, DESCRIPCIO
FROM [ODBC;DRIVER={SQL Server};server=VAIO\SQLEXPRESS;database=ACPE;UID=XX;PWD=XXXX].STA11;

VBA

DoCmd.OpenQuery "queryName"
' OR CurrentDb.Execute strSQL

Alternatively, create a linked table, then run the append query as linked tables can interact with local tables:

DoCmd.TransferDatabase acLink, "ODBC Database", _
      "ODBC;DRIVER={SQL Server};server=VAIO\SQLEXPRESS;database=ACPE;UID=XX;PWD=XXXX;", _
       acTable, "STA11", "STA11"

strSQL = "INSERT INTO PRO (COD_PRO, DESC_PRO) 
          SELECT COD_ARTICU, DESCRIPCIO
          FROM STA11;"

CurrentDb.Execute strSQL
Sign up to request clarification or add additional context in comments.

Comments

0

OPENDATASOURCE is T-Sql statement and is used from within SQL Server not from Access, so you get this error.

To insert data from PRO table which is located in sql server you can:

Create a linked table in VENTAS.mdb to Sql server table "PRO"

Or

create pass through query to Sql Server Pro table in VENTAS.mdb.

7 Comments

Would a pass-through query recognize the tables in the local Access database?
@Zev, yes, pass-through query can access any ODBC connection, including odbc to acess database. In the OP you create pass through query in VENTAS.mdb to SQL server.
Would a pass-through query recognize the tables in the local Access database at the same time as it is querying tables in the remote SQL Server?
@zev, Have a look to: support.microsoft.com/en-us/kb/304323 , for pass-through query to sql server.
@ZevSpitz - No, pass-through queries only run in the scope of connection and cannot see the local database from which it was called. You can make-table from it: SELECT * INTO [localtable] FROM [passthruQ]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.