Link to home
Create AccountLog in
Avatar of commeng
commengFlag for United States of America

asked on

Using VBA to run .SQL file in Access 2016

I am trying to write a script that will run a .sql file directly from Access to update the Access database and SQL database at the same time.  My script works great in SQL Management Studio,  but I am having problems calling it from VBA:  My script reads:

Public Sub sqldmo_execute()

Dim conn
Dim db
Set conn = CreateObject("ADODB.Connection")
conn.CommandTimeout = 9999
conn.Provider = "SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Data Source=tcp:192.168.0.135,1433"
conn.ConnectionTimeout = 10
conn.Open "server=" & [ServerName] & ";uid=" & [User] & ";pwd=" & [password] & ";database=[Database Name]"

CurrentDb.Execute ("\\[ServerName]\[ShareName]\Scripts\SQLSync.sql")

MsgBox ("Complete")

End Sub

I am getting a runtime error 3078:  The Microsoft Access database engine cannot find the input table or query "\\[ServerName]\[ShareName]\Scripts\SQLSync.sql".  Make sure it exists and that its name is spelled correctly.

Is there a better way to call a .sql into Access?  Can someone help me figure out what I coded wrong?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of commeng

ASKER

The Access database is link to the SQL tables.  OK so I need to read my .sql file into a string variable not call the file itself.
Avatar of commeng

ASKER

Basically I have a script on the SQL server that calls information out of AutoCad and populates a table in SQL.  I want the end user to use Access as the front end, so I need the data to always be current. When they are in Access.  So my script updates the SQL table and then the Access database.  But the don't want the end user to have to use SQL Management Studio to get the most current data.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of commeng

ASKER

Yes, I do have the tables linked.  But it he user makes a change in AutoCAD, then they need to update the SQL table so it will reflect in Access.  So I need to work on automating the SQL update not Access.
Yes, I do have the tables linked.
But it he user makes a change in AutoCAD, then they need to update the SQL table so it will reflect in Access.
What I'm missing here? Is there tables in Access that aren't linked to SQL Server and you're updating those? If affirmative, are the same tables existing in SQL Server database? Again, if affirmative, why not working only with SQL Server tables and get rid of the Access databases?
Can you explain the connection between AutoCAD, Access and SQL Server? What kind of process?
Avatar of commeng

ASKER

It got the code.  Thank you both.