Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2006, 09:07 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel Tab And Access

Does anyone know how to write a macro which opens up Access and retrieves information into an Excel Spreadsheet.

I would like to run a macro from a Button which retrieves information and places it in a new Tab (with Date&Time)

Is this possible
 
Old November 15th, 2006, 12:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Kenneth,

Your best bet is to use QueryTables. The following code is an example of how to use them in VBA. You will need to populate a sensible Access file path and SQL query string.

Code:
Sub QueryTableMacro()

Dim wks As Worksheet
Dim qt As QueryTable

Dim i As Integer

Dim strAccessFilePath As String
Dim strSQL As String
Dim strDestinationRange As String
Dim strDir As String
Dim strConnection As String


    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' Set up master information - could be passed as arguments
    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ' Access file path
    strAccessFilePath = "C:\My database.mdb"

    ' SQL query
    strSQL = "SELECT `Information`.`Building Name`, "
    strSQL = strSQL & "`Information`.`Street Address`, "
    strSQL = strSQL & "`Information`.Town, "
    strSQL = strSQL & "`Information`.County, "
    strSQL = strSQL & "`Information`.Postcode"
    strSQL = strSQL & Chr(13) & "" & Chr(10)
    strSQL = strSQL & "FROM `Information` `Information`"

    ' Destination range
    strDestinationRange = "A1"

    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' Add a new sheet
    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Set wks = ThisWorkbook.Sheets.Add
    wks.Name = Format(Now, "dmmmyy hhmm")

    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' Determine the Connection variable
    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ' Find the Path
    i = 1
    strDir = strAccessFilePath
    Do Until InStr(i, strDir, "\", vbBinaryCompare) = 0
        i = InStr(i, strDir, "\", vbBinaryCompare) + 1
    Loop
    strDir = Left(strDir, i - 1)

    ' Set the Connection string
    strConnection = "ODBC;" & _
                    "DSN=MS Access Database;" & _
                    "DBQ=" & strAccessFilePath & ";" & _
                    "DefaultDir=" & strDir & ";" & _
                    "FIL=MS Access;" & _
                    "MaxBufferSize=2048;" & _
                    "PageTimeout=5;"

    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ' Add the QueryTable
    ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Set qt = wks.QueryTables.Add( _
        Connection:=strConnection, _
        Destination:=wks.Range(strDestinationRange), _
        Sql:=strSQL)

    ' Set the remaining query table properties
    ' NB you must run the Refresh method if you want any data
    With qt
        .Name = "Query from MS Access Database"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to disable a tab on a tab control. dbradley VB.NET 6 April 14th, 2011 10:04 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
DataGrid on Tab Control of MS Access 2000 MickeyMauz Access 1 April 17th, 2006 02:25 AM
Covert a tab dillemted file to excel bbradyva Excel VBA 0 October 21st, 2005 10:00 AM
Upload tab delimited into Access? mariakovacs Classic ASP Databases 0 October 10th, 2003 10:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.