commeng
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.Connec tion")
conn.CommandTimeout = 9999
conn.Provider = "SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Data Source=tcp:192.168.0.135,1 433"
conn.ConnectionTimeout = 10
conn.Open "server=" & [ServerName] & ";uid=" & [User] & ";pwd=" & [password] & ";database=[Database Name]"
CurrentDb.Execute ("\\[ServerName]\[ShareNam e]\Scripts \SQLSync.s ql")
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.sq l". 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?
Public Sub sqldmo_execute()
Dim conn
Dim db
Set conn = CreateObject("ADODB.Connec
conn.CommandTimeout = 9999
conn.Provider = "SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Data Source=tcp:192.168.0.135,1
conn.ConnectionTimeout = 10
conn.Open "server=" & [ServerName] & ";uid=" & [User] & ";pwd=" & [password] & ";database=[Database Name]"
CurrentDb.Execute ("\\[ServerName]\[ShareNam
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
Is there a better way to call a .sql into Access? Can someone help me figure out what I coded wrong?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?
ASKER
It got the code. Thank you both.
ASKER