Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using VBA to run .SQL file in Access 2016

Posted on 2016-09-29
8
Medium Priority
?
271 Views
Last Modified: 2016-09-29
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?
0
Comment
Question by:commeng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
ste5an earned 1400 total points
ID: 41821741
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.

That is only possible when you either run the script against your Access database and use linked tables to SQL Server or when you run the script against your SQL Server and use a linked server to your Access database.

So the question is: What are you trying to do?

For both scenarios you need to read the file first into a string variable.

Further more CurrentDb.Execute runs only SQL statements against your Access database. You cannot provide a path to your script. Also your usage of CreateObject() in your procedure makes absolutely no sense.
0
 

Author Comment

by:commeng
ID: 41821762
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.
0
 

Author Comment

by:commeng
ID: 41821773
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.
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 600 total points
ID: 41821835
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, if Access is the front end and SQL Server the back end why do you need to update data in Access? Like ste5an said, a link table in Access should solve your problem so you'll only need to update SQL Server database and who connects to Access will see immediately any change made in SQL Server database.
0
 

Author Comment

by:commeng
ID: 41821850
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.
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41821875
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?
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41821879
Can you explain the connection between AutoCAD, Access and SQL Server? What kind of process?
0
 

Author Comment

by:commeng
ID: 41821881
It got the code.  Thank you both.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

704 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