Solved

Query ACCESS 2003 database table

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

728 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