2014-04-06 71 views

回答

0

我假設你需要給你更詳細一點你迄今爲止已經嘗試過。但是,從以下代碼開始可以提供幫助。

Function File_dailog() As String 

On Error GoTo catchError 
txtPath = "" 
Set fso = CreateObject("Scripting.FileSystemObject") 
Dim directory As String, fileName As String, total As Integer 
Dim fd As Object 
Set fd = Application.FileDialog(3) ' this will open the file dailog 

With fd       ' following are the properties of the file dailog 
.AllowMultiSelect = False 
.Title = "Please select the file." 
.Filters.Clear 
.Filters.Add "Excel 2010", "*.xlsx?" ' you can add more filters of the file for the users below 

If .Show = True Then 
txtPath = Dir(.SelectedItems(1)) 
End If 
txtPath = fso.GetFileName(.SelectedItems(1)) ' fetching the file name 
End With 
File_dailog = txtPath       'return value 
exit_catchError: 
    Exit Function 
catchError: 
    If Err.Number = 5 Then      ' error handling if nothing selected by the user 
    Exit Function 
    End If 

End Function 

要導入Excel您可以使用:

strTable = "temp"  ' name of table you want in your database 
strFile = File_Dailog ' you will get the file name from user selection 
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strFile, True 

我希望這有助於。

0

這是我喜歡用的代碼:

Dim SelectedFile As String 
Dim FilePicker  As FileDialog 
Dim SQLdelete  As String 

Set FilePicker = Application.FileDialog(msoFileDialogFilePicker) 
FilePicker.AllowMultiSelect = False 
FilePicker.Filters.Add "Excel", "*.xls*", 1 
FilePicker.InitialFileName = "C:\Users\" 
FilePicker.Title = "Please Select the Excel Data..." 
FilePicker.Show 

If FilePicker.SelectedItems.Count <> 0 Then 
    SelectedFile = FilePicker.SelectedItems(1) 

    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_Name", SelectedFile, True 

    MsgBox ("The data has been successfully loaded") 
End If 

注:tbl_Name將被保存在您希望的Excel數據表的名稱