Open a SQL script with vba

Posted on 2013-10-22
Medium Priority
Last Modified: 2016-09-16
Hello --

I am trying to create a button with a vba macro assigned to it that will open a SQL script in SQL server management studio when I click it. I want to use windows authentication in the code. I am able to execute ssms.exe, but I do not know how to write the vba that will have the button open a SQL script and login to SQL server with a chosen server and database.  The script does not need to be executed and I am using SQL server 2008 and office 2003.

Any help will be appreciated.


Question by:Wandererer
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
LVL 79

Expert Comment

ID: 39595033
Why not use the VBA sql connection method?

Excel has an example of connecting to sql for queries.
LVL 15

Expert Comment

ID: 39595353
A few clarifications please.

You state, "...will open a SQL script in SQL server management studio..." Does that mean you actually want the button to open SSMS with the script loaded?

You say you have a script. Is it SQL code in a file (which is a script), or is it a smithering of code you want to open.

I suspect, from your question, you want to open SSMS and have it load a file, with the proper database connection. If so, try:

ssms.exe -S server_name -d database -U user -P password -E [use windows authentication] [filename[, file_name]*] name(s) of file(s) to load

ssms.exe /? will bring up the list of parameters to use.

-noSplash will suppress the splash screen while SSMS is loading.

Author Comment

ID: 39595662
Thank you for the responses.  As you know, I am very new to VBA, as this is the first time that I have tried to implement it with SQL Server.  

dbbishop --  I tried the code you suggested, perhaps incorrectly written (the variables are dummy variables, but have similarities with my actual variables):

Public Sub Open_ModScript()
    ssms.exe -S WRTSA41276,15001 -d wandersden -E W:\Test\Scripts\Test.sql

End Sub

I get a 'Compile error: Expected: end of statement' at the Server name which is highlighted.  I have tried with and without the port: 15001.  This has usually been the issue when I have tried passing the server name.

Any suggestions?


Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 20

Expert Comment

by:Marten Rune
ID: 39595758
try with a : instead of the ,
And write it Together with the -S switch (applies to all switches), and inclose the scriptpath with " then it will understand spaces in the path, as i e
ssms.exe -SWRTSA41276:15001 -dwandersden -E "W:\Test\Scripts\Test.sql"

Regards Marten
LVL 15

Expert Comment

ID: 39597799
Public Sub Open_ModScript()
    Dim rc
    rc = Shell("ssms.exe -S WRTSA41276,15001 -d wandersden -E W:\Test\Scripts\Test.sql", 1)
End Sub

Open in new window

LVL 15

Accepted Solution

dbbishop earned 2000 total points
ID: 39597821
If the path/filename contains spaces, you should do something like:
Public Sub Open_ModScript()
    Dim rc
    Dim myFile

    myFile = Chr(34) & "W:\My Test Folder\Test.sql" & chr(34)
    rc = Shell("ssms.exe -S WRTSA41276,15001 -d wandersden -E " & myFile, 1)
End Sub

Open in new window


Author Closing Comment

ID: 39604129
Thank you sir, works perfectly.

Expert Comment

by:venkatesh ks
ID: 41801073
how to open sql server management studio by using sql server authentication by using vba??

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

762 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