Solved

Open a SQL script with vba

Posted on 2013-10-22
8
1,298 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
[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
8 Comments
 
LVL 78

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

691 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