访问VBA代码以导入excel文件

时间:2014-04-06 21:58:24

标签: access-vba

我需要访问vba代码来导入excel文件,但我需要用户选择哪个excel文件。我非常擅长访问vba。

4 个答案:

答案 0 :(得分:0)

您必须打开 FileOpen通用对话框。这将对您有所帮助:

How to show "Open File" Dialog in Access 2007 VBA?

以下是要在Excel文件中读取的代码:

ms-access vba - read from excel and also update that excel

答案 1 :(得分: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

我希望这会有所帮助。

答案 2 :(得分: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数据的表的名称。

答案 3 :(得分:0)

尝试一下。

Sub ExcelImport()
Dim intRet_B As Integer
Dim GetTableName As String
TableName = "table name" 'Put an access table name between "".

SetFile01:
With Application.FileDialog(msoFileDialogOpen)
    .Title = "Select the file" 'Title of dialog box
    .Filters.Clear
    .Filters.Add "Custom Excel Files", "*.xlsx, *.csv, *.xls"
    .AllowMultiSelect = False
    .InitialFileName = CurrentProject.Path
    intRet_B = .Show
    If intRet_B <> 0 Then
        GetTableName = Trim(.SelectedItems.Item(1))
    Else
        GetTableName = ""
        Answ = MsgBox("The file is not selected. If you want to select it, press Yes, No otherwise.", vbQuestion + vbYesNo + vbSystemModal, "File selection")
        If Answ = vbYes Then
            GoTo SetFile01
        Else
            MsgBox "Abort the process.", vbOKOnly + vbInformation + vbSystemModal, "Process Termination"
            DoCmd.Close
            DoCmd.Quit
            End
        End If
    End If
End With


End Sub