+ Reply to Thread
Results 1 to 2 of 2

MS SQL Connection and Retreival

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2006
    Posts
    3

    MS SQL Connection and Retreival

    Hey all,

    I have recently started dabbling in VBA and have been asked to connect an Excel Workbook to a MSSQL Server. I have not done this before and would like some help please!


    Things i do know:
    1. Server Name
    2. Username and Password will have to be the Windows Logon details
    3. The table i want to access

    Things i don't know:
    1. How to connect to the database (DSN-less)
    2. How to run a query and use the results ( 5 values)



    Basically all i want to do is open a connection to the <SERVER> and retrieve details based on a selection on a field entered on a UserForm

    This document has plans in being distributed so I would prefer to use a non-DSN connection!

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    07-11-2006
    Posts
    3
    hey guys,

    i found the solution by playing around with some stuff i found on Microsoft's site:


    Public Sub return_data()
    
        ' Declare the QueryTable object
        Dim qt As QueryTable
        
        ' Set up the SQL Statement
        sqlstring = "select * from test"
        
        ' Set up the connection string, reference an ODBC connection
        ' There are several ways to do this
        ' Leave the name and password blank for NT authentication
        connstring = _
         "ODBC;Driver={SQL Server};Server=(local);UID=;PWD=;Database=test"
        
        ' Now implement the connection, run the query, and add
        ' the results to the spreadsheet starting at row A1
        With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstring)
         .Refresh
        End With
    End Sub
    works a treat!

    ed

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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