Solved

Query ACCESS 2003 database table

Posted on 2013-01-10
3
348 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
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This collection of functions covers all the normal rounding methods of just about any numeric value.
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now