Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Connecting to SQL (Office 2003)

    Using VBA within Excel or Word 2003 I need to connect to a SQL Server database to retrieve and send data. However, the code also needs to be compatible with Office 2000.
    Should I be using ADO to do this? What is the syntax for connecting to the database? Thanks, Andy.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Connecting to SQL (Office 2003)

    Set a reference to the Microsoft ActiveX Data Objects 2.1 Library. This is the version used in Office 2000 (and still available in later versions).

    Sample code:

    Dim oConn As New ADODB.Connection
    Dim oRst As New ADODB.Recordset
    oConn.Open "Provider=sqloledb;" & _
    "Server=myServerName;" & _
    "Database=myDatabaseName;" & _
    "User Id=myUsername;" & _
    "Password=myPassword"
    oRst.Open "MyTable", oConn, adOpenKeyset

    Don't forget to close the recordset and connection at the end.

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Connecting to SQL (Office 2003)

    Brill. Thanks, Andy.

Posting Permissions

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