PC Review


Reply
Thread Tools Rate Thread

How to combine several excel files into one access table?

 
 
Dawn
Guest
Posts: n/a
 
      22nd Mar 2010
Dear all,
The directory for saving files is determined while the number of files
in the directory every month is not certain, and the format of each file is
the same, the difference lies in the file names and the records in each files.
I want to write a Sub to read all records in each file in the directory
into an access table. How to achieve this? Thx for your support in advance.




 
Reply With Quote
 
 
 
 
Tom van Stiphout
Guest
Posts: n/a
 
      22nd Mar 2010
On Sun, 21 Mar 2010 20:13:01 -0700, Dawn
<(E-Mail Removed)> wrote:

You can iterate over the files in a folder using the Dir function.
Then I would attach the file, run an append query to copy the data
from Excel to Access, remove the attachment, and loop.

-Tom.
Microsoft Access MVP


>Dear all,
> The directory for saving files is determined while the number of files
>in the directory every month is not certain, and the format of each file is
>the same, the difference lies in the file names and the records in each files.
> I want to write a Sub to read all records in each file in the directory
>into an access table. How to achieve this? Thx for your support in advance.
>
>
>

 
Reply With Quote
 
Dawn
Guest
Posts: n/a
 
      23rd Mar 2010
Thx,Tom,it's a new way out of my box. But since I'm a green hand, can you put
out some coding for reference? Very thx

"Tom van Stiphout" wrote:

> On Sun, 21 Mar 2010 20:13:01 -0700, Dawn
> <(E-Mail Removed)> wrote:
>
> You can iterate over the files in a folder using the Dir function.
> Then I would attach the file, run an append query to copy the data
> from Excel to Access, remove the attachment, and loop.
>
> -Tom.
> Microsoft Access MVP
>
>
> >Dear all,
> > The directory for saving files is determined while the number of files
> >in the directory every month is not certain, and the format of each file is
> >the same, the difference lies in the file names and the records in each files.
> > I want to write a Sub to read all records in each file in the directory
> >into an access table. How to achieve this? Thx for your support in advance.
> >
> >
> >

> .
>

 
Reply With Quote
 
pietlinden@hotmail.com
Guest
Posts: n/a
 
      23rd Mar 2010
On Mar 22, 8:36*pm, Dawn <D...@discussions.microsoft.com> wrote:
> Thx,Tom,it's a new way out of my box. But since I'm a green hand, can youput
> out some coding for reference? Very thx
>
> "Tom van Stiphout" wrote:
> > On Sun, 21 Mar 2010 20:13:01 -0700, Dawn
> > <D...@discussions.microsoft.com> wrote:

>
> > You can iterate over the files in a folder using the Dir function.
> > Then I would attach the file, run an append query to copy the data
> > from Excel to Access, remove the attachment, and loop.

>
> > -Tom.
> > Microsoft Access MVP

>
> > >Dear all,
> > > * *The directory for saving files is determined while the number of files
> > >in the directory every month is not certain, and the format of each file is
> > >the same, the difference lies in the file names and the records in each files.
> > > * *I want to write a Sub to read all records in each file in the directory
> > >into an access table. How to achieve this? Thx for your support in advance.

>
> > .


Private Sub cmdPopulateCombo_Click()
Dim strDirectory As String
Dim strFile As String

' Requires the BrowseFolder API from Access web... Here:
strDirectory = BrowseFolder("Which folder contains the files you
want?")
strFile = Dir(strDirectory & "\*.XLS")

Do While strFile <> ""
'NOTE: xlsEmpInfo is the name of my linked Excel spreadsheet

'---Set link to the strFile (the filename returned by the
Dir() function
currentdb.TableDefs("xlsEmpInfo").Connect = "Excel
5.0;HDR=YES;IMEX=2;DATABASE=" & strFile

'---run the append query to write the contents of the excel
file to your table. (build the query first... manually attach an excel
file)
CurrentDb.Execute "qappMyAppendQuery", dbFailOnError

'--- get the next file
strFile = Dir
Loop

End Sub


You will need the top two API calls, most likely... at least the
BrowseFolder API (so you can choose the folder the Excel files are in)


Private Sub ImportXLFiles()
Dim strDirectory As String
Dim strFile As String
Dim i As Integer

strDirectory = BrowseFolder("Which folder contains the files you
want?")
strFile = Dir(strDirectory & "\*.XLS")

Do While strFile <> ""
DBEngine(0)(0).TableDefs("MyAttachedExcelFile").Connect =
"Excel 5.0;HDR=YES;IMEX=2;DATABASE=" & strFile
Loop
End Sub

This code is probably buggy as all get out... I haven't tested it...
Will fix it further later today if you need it.

Pieter
 
Reply With Quote
 
Dawn
Guest
Posts: n/a
 
      24th Mar 2010
Pieter,Tom,
Thany you very much for your selfless help. I 'll try first, may come
here again when encounter problems. thx.



"(E-Mail Removed)" wrote:

> On Mar 22, 8:36 pm, Dawn <D...@discussions.microsoft.com> wrote:
> > Thx,Tom,it's a new way out of my box. But since I'm a green hand, can you put
> > out some coding for reference? Very thx
> >
> > "Tom van Stiphout" wrote:
> > > On Sun, 21 Mar 2010 20:13:01 -0700, Dawn
> > > <D...@discussions.microsoft.com> wrote:

> >
> > > You can iterate over the files in a folder using the Dir function.
> > > Then I would attach the file, run an append query to copy the data
> > > from Excel to Access, remove the attachment, and loop.

> >
> > > -Tom.
> > > Microsoft Access MVP

> >
> > > >Dear all,
> > > > The directory for saving files is determined while the number of files
> > > >in the directory every month is not certain, and the format of each file is
> > > >the same, the difference lies in the file names and the records in each files.
> > > > I want to write a Sub to read all records in each file in the directory
> > > >into an access table. How to achieve this? Thx for your support in advance.

> >
> > > .

>
> Private Sub cmdPopulateCombo_Click()
> Dim strDirectory As String
> Dim strFile As String
>
> ' Requires the BrowseFolder API from Access web... Here:
> strDirectory = BrowseFolder("Which folder contains the files you
> want?")
> strFile = Dir(strDirectory & "\*.XLS")
>
> Do While strFile <> ""
> 'NOTE: xlsEmpInfo is the name of my linked Excel spreadsheet
>
> '---Set link to the strFile (the filename returned by the
> Dir() function
> currentdb.TableDefs("xlsEmpInfo").Connect = "Excel
> 5.0;HDR=YES;IMEX=2;DATABASE=" & strFile
>
> '---run the append query to write the contents of the excel
> file to your table. (build the query first... manually attach an excel
> file)
> CurrentDb.Execute "qappMyAppendQuery", dbFailOnError
>
> '--- get the next file
> strFile = Dir
> Loop
>
> End Sub
>
>
> You will need the top two API calls, most likely... at least the
> BrowseFolder API (so you can choose the folder the Excel files are in)
>
>
> Private Sub ImportXLFiles()
> Dim strDirectory As String
> Dim strFile As String
> Dim i As Integer
>
> strDirectory = BrowseFolder("Which folder contains the files you
> want?")
> strFile = Dir(strDirectory & "\*.XLS")
>
> Do While strFile <> ""
> DBEngine(0)(0).TableDefs("MyAttachedExcelFile").Connect =
> "Excel 5.0;HDR=YES;IMEX=2;DATABASE=" & strFile
> Loop
> End Sub
>
> This code is probably buggy as all get out... I haven't tested it...
> Will fix it further later today if you need it.
>
> Pieter
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: many excel files into one access table Jeff Boyce Microsoft Access External Data 3 12th Mar 2009 06:19 PM
importing excel files into access table =?Utf-8?B?TWlrZQ==?= Microsoft Access External Data 0 5th Jul 2007 03:32 PM
How do you combine several tables into one table in Access? =?Utf-8?B?R3dlbmRvbHlu?= Microsoft Access 6 19th Apr 2005 10:45 PM
combine access tables into one table =?Utf-8?B?dnVsY2FuODg=?= Microsoft Access Queries 7 30th Mar 2005 10:16 PM
append multiple excel files to access table Ron R Microsoft Access External Data 0 21st Jul 2003 02:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 PM.