Results 1 to 2 of 2
  1. #1
    selvakumar.arc is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Posts
    49

    ActiveSheet.QueryTables.Add - General ODBC Error message

    Hi all,

    I am trying to update the excel sheet using the following method and I am getting error as "General ODBC error".

    varConn = "ODBC;DBQ=C:\xyz.mdb;Driver={Driver do Microsoft Access (*.mdb)}"
    varSQL = "SELECT * from XYZ"

    With ActiveSheet.QueryTables.Add(Connection:=varConn, Destination:=ActiveSheet.Range("A1"))
    .CommandText = varSQL
    .Name = "Data"
    .Refresh BackgroundQuery:=False
    End With

    Can anyone help me to solve this issue?

    Thanks


    Selvakumar R

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    This is how I use VBA in Excel to connect to Access database:

    'declare public constant in a general module
    Public Const gconConnection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='\\server name\path\databasename.accdb'"
    ______________

    Private Sub btnStart_Click()
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    cn.Open gconConnection
    'get project records matching state project number
    rs.Open "SELECT DISTINCT ProjectName FROM GeoDataAll WHERE StateNum='" & gstrStateNum & "';", cn, adOpenStatic, adLockReadOnly
    ...
    End Sub
    ______________

    But I just open recordsets and then have more code that writes data to cells. You appear to be doing something else. So I used macro recorder to try and produce code like yours. This is what generated:

    Code:
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
            "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=R:\Lab\Database\Data\LabData.accdb;Mode=ReadWrite;Extend" _
            , _
            "ed Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;" _
            , _
            "Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Databas" _
            , _
            "e Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=Fal" _
            , _
            "se;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass User" _
            , "Info Validation=False"), Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdTable
            .CommandText = Array("Projects")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .SourceDataFile = "R:\Lab\Database\Data\LabData.accdb"
            .ListObject.DisplayName = "Table_LabData.accdb"
            .Refresh BackgroundQuery:=False
        End With
    Some of the line breaks look odd but the data imported. No idea why it broke on the words Extended, Database, False and threw commas in between - weird. I suppose just need to edit those lines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-02-2015, 09:44 AM
  2. Replies: 15
    Last Post: 11-01-2013, 03:24 PM
  3. Replies: 14
    Last Post: 07-29-2013, 02:33 PM
  4. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  5. ODBC timeout message on query
    By thart21 in forum Queries
    Replies: 3
    Last Post: 08-05-2011, 08:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums