Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Choosing Excel Spreadsheet for Import Using Code

Status
Not open for further replies.

ScottXJ

Programmer
Aug 14, 2002
51
CA
Hi,

I have a requirement to import Excel spreadsheets into a table on a daily basis and I wanted to know if anyone could provide information on how to do this. I looked at the TransferSpreadsheet option but this requires the path to be hard-coded and doesn't allow for different files to be selected. What I am looking to do is to have a window come up to ask the user to locate the file they would like to import. Any assistance would be appreciated.

Thanks,

Scott.
 
Are these spreadsheets constant? If so, try building a form with a list box and place the paths and names of all your files in the box. Then code it like this:

Dim stFile As String
Dim stTable As String
stTable = "TableName"
stFile = [Forms]![Formname]![ListBoxName]

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, stTable, stFile

Dave
 
Hi Scott!

If you can make sure that the Excel files are all in one folder then the following code will put the files available in a list box:

Dim f, fc
Set fc = CreateObject("Scripting.FileSystemObject").GetFolder(Application.CurrentProject.Path).Files
For Each f In fc
If Right(f.Name, 4) = ".xls" Then
lstFilename.RowSource = lstFilename.RowSource & """" & f.Name & """;"
End If
Next f

Note that this code assumes the Excel files are in the same folder as the database. If you do not want that then you need to replace Application.CurrentProject.Path with the path to the folder where the files reside. You can use the double click event to capture the name selected and then append the path to the name and use that in your transfer spreadsheet method.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Fletch518 and Jebry:

Thanks for your help! I will try these methods out and let you know how they work. I really appreciate your assistance.

Scott.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top