Solved

Query ACCESS 2003 database table

Posted on 2013-01-10
3
346 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 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

12 Experts available now in Live!

Get 1:1 Help Now