Solved

Open a SQL script with vba

Posted on 2013-10-22
8
1,202 Views
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.

Thanks,

Wanderer(er)
0
Comment
Question by:Wandererer
8 Comments
 
LVL 77

Expert Comment

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

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

Expert Comment

by:dbbishop
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.
0
 

Author Comment

by:Wandererer
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?

Thanks,

wander
0
 
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
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 15

Expert Comment

by:dbbishop
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

0
 
LVL 15

Accepted Solution

by:
dbbishop earned 500 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

0
 

Author Closing Comment

by:Wandererer
ID: 39604129
Thank you sir, works perfectly.
0
 

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now