[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Open a SQL script with vba

Posted on 2013-10-22
8
Medium Priority
?
1,373 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 80

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

649 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