Use the code here to bring up a file dialog:
http://www.mvps.org/access
Paste it into a new module, and then you can get a file like so, let's say in a command button:
Private Sub cmdImport_Click()
Dim strFile As String
Dim strFilter As String
On Error Goto cmdImport_Error
strFilter=ahtAddFilterItem
strFile=ahtCommonFileOpenS
If strFile="" Then Exit Sub
DoCmd.TransferSpreadsheet acImport,,"MyTable",strFil
MsgBox "Everything ok!"
Exit Sub
cmdImport_Error:
MsgBox "Something went wrong! Error was: " & Err.Number & " " & Err.Description
End Sub
This line does the import:
DoCmd.TransferSpreadsheet acImport,,"MyTable",strFil
The last argument determines if the spreadsheet has field names (true) or not (false). You can specify the spreadsheet type in the second argument if you need to.
Main Topics
Browse All Topics





by: capricorn1Posted on 2004-08-28 at 10:13:07ID: 11921963
you can use this codes to import the excel file
FileName, _ cending) > 0 Then
you need to add Microsoft Office xx Object Library to your refernces
Tools>References select the Microsoft Office xx Object Library xx is the version number
Private Sub cmdGetExcelFile_Click()
Dim strFileName As String, strFileName1 as String, sTableName As String, strPath As String
Dim i As Integer
Dim fs As Object
Set fs = Application.FileSearch
With fs
.LookIn = "C:\ExcelFiles"
.fileName = "*.xls"
If .Execute(SortBy:=msoSortby
SortOrder:=msoSortOrderDes
For i = 1 To .FoundFiles.Count
strPath = .FoundFiles(i)
strFileName = Dir(strPath)
strFileName1 = Left$([strFileName], InStr(1, [strFileName], ".") - 1)
sTableName = Mid(Replace(strFileName1, " ", ""), InStr(1, (Replace(strFileName1, " ", "")), "-") + 1)
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found. And you want to Import " & strFileName
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"" & sTableName & "", strPath, True
Next i
Else
MsgBox "There were no files found."
End If
End With
End Sub