+ Reply to Thread
Results 1 to 2 of 2

O365 64bit Excel VBA - need to query data in Access DB (64bit) - how do I set this up?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2022
    Location
    winnipeg mb
    MS-Off Ver
    o365
    Posts
    1

    O365 64bit Excel VBA - need to query data in Access DB (64bit) - how do I set this up?

    Hi I would like to see example VBA code to connect to Access from Excel - to return queried data - all 64bit. all Office 365.

    Thanks,
    Rick

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: O365 64bit Excel VBA - need to query data in Access DB (64bit) - how do I set this up?

    Here you go:

    Sub GetAccessData()
    ' Sample demonstrating how to return a recordset from an Access db
    
       Const adOpenStatic As Long = 3
       Const adLockPessimistic As Long = 2
       Const adCmdText As Long = 1
    
        '    sheet for output
       Dim wks As Excel.Worksheet
       Set wks = ActiveSheet
    
        '    Path to database
       Dim PathToDB As String
       PathToDB = "C:\test\database1.accdb"
    
       Dim cn As Object
       Set cn = CreateObject("ADODB.Connection")
        '    open connection to database
        With cn
            .ConnectionTimeout = 500
            .Provider = "Microsoft.ACE.OLEDB.12.0"
            .ConnectionString = "Data Source=" & PathToDB & ";"
            .Open
            .CommandTimeout = 500
        End With
    
        '    SQL query string - change to suit
       Dim commandText As String
       commandText = "SELECT tblGSD.Day, tblGSD.[Due Calls] "
        commandText = commandText & "FROM tblGSD "
        commandText = commandText & "WHERE (((tblGSD.Day)=#2/21/2019#)) "
        commandText = commandText & "GROUP BY tblGSD.Day, tblGSD.[Due Calls] "
        '    Create New Recordset
       Dim rs As Object
       Set rs = CreateObject("ADODB.Recordset")
    
        ' open recordset using query string and connection
        With rs
            .Open commandText, cn, adOpenStatic, adLockPessimistic, adCmdText
            ' check for records returned
            If Not .EOF Then
                'Populate field names
                Dim i As Long
                For i = 1 To .Fields.Count
                    wks.Cells(1, i).Value = .Fields(i - 1).Name
                Next i
    
                ' Copy data to sheet starting from A2
                wks.Cells(2, 1).CopyFromRecordset rs
    
            End If
            .Close
        End With
    
        ' clean up
        cn.Close
    End Sub
    Everyone who confuses correlation and causation ends up dead.

+ 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. MonthView Excel 365 64bit
    By d365b in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2021, 06:08 PM
  2. [SOLVED] How to resize the Userform based on screen resolution in Office 64bit and Windows 64bit
    By king05 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-21-2018, 04:30 PM
  3. 32 V. 64BIT excel
    By Cheecochongo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-20-2016, 09:07 PM
  4. How to read custom props on closed file (64bit Win Server, 64bit Excel)
    By soundneedle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2015, 01:25 AM
  5. Change registry settings for IE 64bit in 64bit environment
    By dgdgdg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2014, 07:56 AM
  6. Reporting from Excel 64bit using VBA
    By Bezzie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2013, 02:45 AM
  7. Replies: 3
    Last Post: 11-23-2012, 01:57 AM

Tags for this Thread

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