+ Reply to Thread
Results 1 to 3 of 3

Need Help Creating SQL Connection String from Excel Worksheet

  1. #1
    Registered User
    Join Date
    01-29-2015
    Location
    Royal Oak, MI
    MS-Off Ver
    7
    Posts
    3

    Need Help Creating SQL Connection String from Excel Worksheet

    I'm trying to create a text box on my 2013 Excel worksheet that the user can enter the connection string info such as Server Name, Database Name etc., and have it connect to a SQL Server database 2008r2 via clicking on a button with the text box. I'm not sure of what VB coding to use to accept the connection string info from worksheet and have it connect to SQL Server.

    On the same worksheet, there are other buttons to click and run queries against the database, but the connection string is hard coded. Being somewhat new to VB code, I'm thinking there's probably an easy way to do it that I'm missing.

    Can anyone help? Thank you!

  2. #2
    Registered User
    Join Date
    01-29-2015
    Location
    Royal Oak, MI
    MS-Off Ver
    7
    Posts
    3

    Re: Need Help Creating SQL Connection String from Excel Worksheet

    Anyone have a possible solution? Any other ideas to accomplish having a user enter their connection string into an Excel worksheet and have the underlying VB code perform the connection to SQL?

  3. #3
    Registered User
    Join Date
    01-29-2015
    Location
    Royal Oak, MI
    MS-Off Ver
    7
    Posts
    3

    Re: Need Help Creating SQL Connection String from Excel Worksheet

    After a few hours in the Bat Cave I've got the answer.

    Here is how the Excel Spreadsheet is set up. The user enters their connection string in Column D.

    Enter Your Server Name in Column D ----- xxxxxx\SQL2008R2
    Enter Your Database Name in Column D ----- DBName
    Enter Your User Name in Column D ----- UserID
    Enter Your Password in Column D ----- xxxxxxxxx


    VB Code

    ' Set up for SQL connection
    Dim gsServerName As String
    Set ServerNameCell = Sheet6.Range("D10")
    gsServerName = ServerNameCell

    Dim gsDatabase As String
    Set DBNameCell = Sheet6.Range("D11")
    gsDatabase = DBNameCell

    Dim gsUsername As String
    Set UserNameCell = Sheet6.Range("D12")
    gsUsername = UserNameCell

    Dim gsPassword As String
    Set PassNameCell = Sheet6.Range("D13")
    gsPassword = PassNameCell


    ' Connection String Input
    con.ConnectionString = "Provider=sqloledb;" & _
    "Data Source=" & gsServerName & ";" & _
    "Initial Catalog=" & gsDatabase & ";" & _
    "User Id=" & gsUsername & ";" & _
    "Password=" & gsPassword
    con.Open
    cmd.ActiveConnection = con


    ' Run the following Stored Procedure to build the Account table
    cmd.CommandText = "TableBuild"
    Set rs = cmd.Execute(, , adCmdStoredProc)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel External Connection Dropping Password from Connection String?
    By mar0isa in forum Access Tables & Databases
    Replies: 1
    Last Post: 03-06-2015, 10:35 AM
  2. Change a connection string using vba within excel
    By vineshanc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2014, 04:56 AM
  3. Excel Dynamic Connection string
    By 9976422121 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2012, 07:06 AM
  4. Connection String In Excel
    By mfosterla in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2008, 04:19 AM
  5. Vesion of Excel in Connection string
    By Prasun in forum Excel General
    Replies: 0
    Last Post: 05-02-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1