Re: Choosing an access table with VBA code

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: shockley (shocklee_at_frontiernet.net)
Date: 03/11/04


Date: Thu, 11 Mar 2004 06:33:17 GMT

You can iterate through the tables in the database, add and name the
worksheets with code like this:

    For Each T In db2.TableDefs
        If Not T.Name Like "MSys*" Then
            Worksheets.Add After:=Sheets(Sheets.Count)
            ActiveSheet.Name = T.Name
        End If
    Next T

Access uses hidden tables with names starting with "MSys..." that you won't
want to include in your workbook.

The most foolproof way to get the user to select a table name is using a
listbox or combobox in a user form. You can populate it like this:

    For Each T In db2.TableDefs
        If Not T.Name Like "MSys*" Then
            frm01.cbxTableNames.AddItem T.Name
        End If
    Next T

Then use the click event for the combobox to select a table-name and
continue executing the macro.

    frm01.Show
    Worksheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = frm01.cbxTableNames.Value

HTH,
Shockley

"me" <chrisv@texas.net> wrote in message
news:404fbd54$0$26496$ec3e2dad@news.usenetmonster.com...
> Hello all.
>
> I use the following code to copy and Access data table and export it into
an
> Excel worksheet. I have an Access database that has 50 tables. What I
need
> to know is after making the DAO connection, how can I make the the
procedure
> loop through the entire database and copy/paste the tables into an excel
> workbook, or how can I incorporate an input box that allows the user to
> select a table from the database so it can be copied over one at a time???
>
> Any suggestions? All help is appreciated.
>
> Code:
>
> Public Sub CopyAccessTable()
>
> 'Open the database that will be copied
> Set dbs = OpenDatabase _
> ("C:\Documents and Settings\chrisv\Desktop\db2.mdb")
>
> 'Select the initial table to be copied
> Set rst = dbs.OpenRecordset("Customer", dbOpenDynaset, _
> dbReadOnly)
>
> 'Select the starting range to begin copy.
> [A2].CopyFromRecordset rst
>
> 'These lines iterate fields for copy
> For Each fld In rst.Fields
> i = i + 1
> Cells(1, i).Value = fld.Name
> Next fld
>
> 'Now we need to get our virtual data on a worksheet
> ActiveSheet.Columns.AutoFit
> ActiveSheet.Name = rst.Name
> 'Let's check for other tables in the database
>
> 'Let's copy that info into a new worksheet
>
> 'Keep this going until we are out of tables
>
>
> 'Clean up shop
> dbs.Close
>
> 'Verify completion
> MsgBox "Your table was successfully imported to Excel"
> End Sub
>
>



Relevant Pages

  • Re: Importing Excel spreadsheets
    ... Thanks John. ... in the spread sheet don't necessarily have the same companies every month ... my database and combining the data from pages 1 and 2 where the company name ... > in each worksheet each month. ...
    (microsoft.public.access.externaldata)
  • RE: Access or Excel - which is the way to go for my project?
    ... to develop the database vs. the amount of effort it takes right now. ... there is only one contractor that holds a multi-year contract. ... The contractor responds with a work order estimate. ... The second worksheet is for change orders to the ...
    (microsoft.public.access.tablesdbdesign)
  • RE: The best way to handle excel data imported to Access
    ... may help the date issue if you are proficient in writing Excel formulas. ... template to be imported later after they copy pasted to this worksheet. ... I am the developer of the database by the help of this forum. ... rows with member data must begin in row 2. ...
    (microsoft.public.access.externaldata)
  • Re: Determining last row/column using ADO with closed workbook
    ... Jamie, thank you so much for your support ... worksheet belonging to a spefic workbook....using the structure ... If I do all of this in an Access database can I manintain the same ... >>Can this be done within Excel using ADO. ...
    (microsoft.public.excel.programming)
  • RE: I need help writing a macro to replace cell contents. (new to
    ... done any sort of database work and as one of my co-workers ... Begin Loop ... Cell in Column B ... For Worksheet B, C, D, E, F ...
    (microsoft.public.excel.programming)