code for import excel files into access

  • Thread starter Shivalee Gupta via AccessMonster.com
  • Start date
S

Shivalee Gupta via AccessMonster.com

i am working on access 2000. i want to write a code which will link my access to excel. first it should show me a browser from which i can select a folder. when i do so, all the files present within that folder should get imported from excel to access as tables. maually i can do with import option of access. but i need to do it dynamically, because i will always have the same named but different content excel files to import into access.
please, please help.
thanks in advance.
 
C

Chris Reveille

Here is the answer to part two of your question. I have
used this for importing not linking. I picked this up from
one of the MVP's from a previous post. Watch for word wrap.

If the Excel files have similar names such as MyFile1,
MyFile2, MyFile3 or MyFileA, MyFileB, MyFileC then you can
put the import routine in a loop that
increments the file name each iteration of the loop.


The following code should get you started
To get the files use something like the following:

Dim foundFnames As New Collection ' collection to store
found filenames
' dirPath & fileFilters are string variables containing
' the path to search
' filefilter should be self explanatory

With Application.FileSearch
..NewSearch
..LookIn = dirPath
..SearchSubFolders = True
..fileName = fileFilter
..MatchTextExactly = True
End With

' apply the search
' vader possibly store the filenames in a collection
With Application.FileSearch
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s)found."
For i = 1 To .FoundFiles.Count
foundFnames.Add .FoundFiles(i)' store the filenames in
a collection
Next i
Else
MsgBox "There were no files found."
End If
End With

' now process each filename found
' need to open the individual spreadsheets listed above
' sheetRange defines name/range of sheet to import
For Each wbFname In foundFnames
DoCmd.TransferSpreadsheet acImport, 8, importTable,
wbFname,hasFldNamesFlg, sheetRange
next wbFname
' there are various error conditions to handle.
' the transfer spreadsheet is ok (see transferspreadsheet
action help topic) but it does have the odd gotcha.
If you are loading the data into an Access table (as
opposed to a linked table) then you should be ok.
Remember that if you get any errors with individual fields
the import is likely to load the rest of the row and just
let you know that there has been a problem in a seperate
ImportErrors table. If you get data conversion errors then
use the tool here to make sure all your cells are of the
same datatype.
http://www.j-walk.com/ss/excel/tips/tip28.htm

Good luck,

Chris
-----Original Message-----
i am working on access 2000. i want to write a code which
will link my access to excel. first it should show me a
browser from which i can select a folder. when i do so, all
the files present within that folder should get imported
from excel to access as tables. maually i can do with
import option of access. but i need to do it dynamically,
because i will always have the same named but different
content excel files to import into access.
 
S

Shivalee Gupta via AccessMonster.com

hi Chris Reveille ,

i have made a command button and in the On_click event of this command button, i have put the code you have given. it gives me an error on this line:

If .Execute(SortBy:=msoSortbyFileName, SortOrder:=msoSortOrderAscending) > 0 Then

Please help .
thanks again,
shivalee
 
C

Chris Reveille

I just tested the below code in Access 2000 and it works.
My directory is "c:\excelfiles"
My file names begin with "test"
My table I am importing to is "ExcelImport"
remember the double quotes

Private Sub Command0_Click()


Dim foundFnames As New Collection ' collection to store
found filenames
' dirPath & fileFilters are string variables containing
' the path to search
' filefilter should be self explanatory

With Application.FileSearch
..NewSearch
..LookIn = "c:\excelfiles"
' ******* The directory the files are in
..SearchSubFolders = True
..filename = "test" & "*"
' ******* my files begin with Test. I used test1.xls,
' ****** test2.xls, test3.xls and test4.xls
..MatchTextExactly = True
End With

' apply the search
' vader possibly store the filenames in a collection
With Application.FileSearch
If .Execute(SortBy:=msoSort' ByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
foundFnames.Add .FoundFiles(i) ' store the filenames in a
Collection
Next i
Else
MsgBox "There were no files found."
End If
End With

For Each wbfname In foundFnames

DoCmd.TransferSpreadsheet acImport, 8, "excelimport",
wbfname, True, ""
' ****** where ExcelImport is existing table name
Next wbfname

End Sub

Chris
-----Original Message-----
hi Chris Reveille ,

i have made a command button and in the On_click event of
this command button, i have put the code you have given. it
gives me an error on this line:
If .Execute(SortBy:=msoSortbyFileName,
SortOrder:=msoSortOrderAscending) > 0 Then
 
S

Shivalee Gupta via AccessMonster.com

i am getting an error at line:

If .Execute(SortBy:=msoSort' ByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then

the error is:
Run-time error -5
Invalid procedure call or argument.

do i have to write anything else also besides the code you have given me?
please help me,
thanks again chris for all the help.

shivalee
 
S

Shivalee Gupta via AccessMonster.com

i have written:

If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then

i am still getting an error :
run time error -5
Invalid procedure call or argument.
do i have to write any other lines of code also besides what u have given me?
please help me...
thanks again for all the help chris.
Shivalee
 
D

Douglas J. Steele

You've trimmed too much from your posting for anyone to make any
suggestions.
 
S

Shivalee Gupta via AccessMonster.com

i have written:

If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then

i am still getting an error :
run time error -5
Invalid procedure call or argument.
do i have to write any other lines of code also besides what u have given me?
please help me...
thanks again for all the help .

and douglas...i have not clipped anything. i dont even know how to clip & what is clipping!
Shivalee
 
S

Shivalee Gupta via AccessMonster.com

my whole code is :
Private Sub Command1_Click()
Dim foundFnames As New Collection ' collection to store found filenames
' dirPath & fileFilters are string variables containing
' the path to search
' filefilter should be self explanatory

With Application.FileSearch
..NewSearch
..LookIn = "c:\excelfiles" ' The directory the files are in
..SearchSubFolders = True
..filename = "test" & "*" ' my files begin with Test. I used test1.xls,
' test2.xls, test3.xls and test4.xls
..MatchTextExactly = True
End With

' apply the search
' vader possibly store the filenames in a collection
With Application.FileSearch
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
foundFnames.Add .FoundFiles(i) ' store the filenames in a Collection
Next i
Else
MsgBox "There were no files found."
End If
End With

For Each wbfname In foundFnames

DoCmd.TransferSpreadsheet acImport, 8, "excelimport", wbfname, True, ""
' where ExcelImport is existing table name
Next wbfname
End Sub

is this what you meant by trimming and the stuff???
regards,
shivalee
 
C

Chris Reveille

It sounds like you are missing one of your references.
Look at you references and see if you have
Microsoft Outlook 9.0 library checked

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top