Solved

Query ACCESS 2003 database table

Posted on 2013-01-10
3
352 Views
Last Modified: 2013-01-15
I have a simple need to query the database and have the enter contents of a table, tblHolidays, end up on an Excel worksheet.  It is a small table and I have the below, but it does not link the Commandtype.  Can I pass the table name as an array?

sandra

Sub GetHolidays()
Dim cnn As New ADODB.Connection
Dim strPath As String

    ThisWorkbook.Worksheets("Holidays").Activate
    Range("A1").Activate
    strPath = "\\Sf1\user1\shared\DepositServices\CIA\SOS_APPLICATIONS\ADMIN\PRD\Admin.mdb"

    With cnn
        .Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strPath & ";"  'Create DB connection
        .CommandType = xlCmdTable
        .CommandText = Array("tblHolidays")
        .SourceDataFile = strPath
        .ListObject.DisplayName = "Holidays"
        .Refresh BackgroundQuery:=False
    End With
   
End Sub
0
Comment
Question by:ssmith94015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38766063
If your ultimate goal here is to export an Access table to Excel, then you can do it like so:

Dim strTableName As String
strTableName = "tblHolidays"
DoCmd.TransferSpreadsheet acExport, , strTableName, "C:\YourFolder\" & strTableName & ".xls", True
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 38766247
Personally I would use..

Sub ImportAccess()
' assumes data is to be imported into A1 onwards
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim stDB As String
Dim ws As Worksheet
Dim NumFld As Long, x As Long

Set ws = ThisWorkbook.Worksheets("Holidays")

stDB = ThisWorkbook.Path & "\" & "Northwind.mdb"  ' SET stDB TO YOUR DATABASE

ws.Range("A1").CurrentRegion.Clear  ' or something similar to make sure no data remains from previous import

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & stDB & ";"

rst.Open "SELECT * FROM tblHolidays", cnn

' next 4 lines put field names in row 1 - omit if not required
NumFld = rst.Fields.Count
For x = 0 To NumFld - 1
ws.Cells(1, x + 1).Value = rst.Fields(x).Name
Next x
' get the data
ws.Cells(2, 1).CopyFromRecordset rst

rst.close
cnn.close
Set rst = Nothing
Set cnn = Nothing
End Sub
0
 

Author Closing Comment

by:ssmith94015
ID: 38780274
I do not want to export from Access to Excel, but bring in each month the holiday table into a particular worksheet in a workbook with other tabs that use this data.  Thus, simply doing a transfer would not work.  but the importAccess did the trick.

Thank you both.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question