Results 1 to 5 of 5

Automation error when opening Access database

Threaded View

  1. #1
    Registered User
    Join Date
    04-23-2010
    Location
    Oregon
    MS-Off Ver
    Excel 2003
    Posts
    20

    Automation error when opening Access database

    This runs but returns an "run time" and "automation error" I dont know why.

    Thanks in advance for help

    Private Sub CommandButton1_Click()
       
       Dim cnn As Object, strQuery As String, rst As Object
       Dim strPathToDB As String, strFormula As String, i As Long
       Dim wks As Worksheet
       Dim lngNextNum As Long, lngRow As Long, lngCol As Long
       Dim varData
       
       
       ' output to activesheet
       Set wks = ActiveSheet
       
       ' Path to database
       strPathToDB = "Z:\spreadchecks stand alone.mdb"
       
       Set cnn = CreateObject("ADODB.Connection")
       ' open connection to database
       With cnn
          .ConnectionTimeout = 500
          .Provider = "Microsoft.Jet.OLEDB.4.0"
          .ConnectionString = "Data Source=" & strPathToDB & ";"
          .Open
          .CommandTimeout = 500
       End With
       ' SQL query string - change to suit
       strQuery = "SELECT Data.Date, Data.ValueRight,"
       
    
    
       
       ' create new recordset
       Set rst = CreateObject("ADODB.Recordset")
       
       ' open recordset using query string and connection
       With rst
         .Open strQuery, cnn, 3, 2, 1
          ' check for records returned
          If Not (.EOF And .BOF) Then
             'Populate field names starting at col AA
             For i = 1 To .Fields.Count
                wks.Cells(1, 27 + i) = .Fields(i - 1).Name
             Next i
             ' Copy data from AA2
             wks.Cells(2, "AA").CopyFromRecordset rst
          End If
          .Close
       End With
    
       ' clean up
       Set rst = Nothing
       cnn.Close
       Set cnn = Nothing
    End Sub
    Last edited by GWB; 04-26-2010 at 05:06 PM.

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