Vous êtes sur la page 1sur 4

Selecting Files with the Offce FileDialog

Use the Application.FileDialog (shown in Figure 4-26, followed by sample code) to present users with a
professional pop-up dialog, from which they can select a fle.

You can use FileDialog to give user the ability to specify different flters and select multiple fles.

To use FileDialog objecg, you need to provide a reference to the Microsoft Offce 14.0 Object Library

Adding a reference into your Access project.

Private Sub cmdExportToExcel_Click()


' Requires reference to Microsoft Office 14.0 Object Library.
Dim fDialog As Office.FileDialog
Dim strFilename As String
Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
With fDialog
.AllowMultiSelect = false
.Title = "Save file"
.InitialFileName = "Customers_Suppliers(" & Format(Now(), "dd_mmm_yyyy") & ")"
& ".xlsx"
If .Show = True Then
strFilename = .SelectedItems(1)
DoCmd.TransferSpreadsheet acExport,10,"qryCustomersInCA", strFilename, True
DoCmd.TransferSpreadsheet acExport,10,"Shippers Extended", strFilename, True

MsgBox ("Successfully Export to Excel")


Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub

Export as Excel Spreadsheet from Microsoft Access


 Open the form “frmFileDialog_Export_Before” in design view.
 Select “Export to Excel” button and go to On Click event in the property box.
 Type the following code in Visual Basic for Application.

Private Sub cmdExportToExcel_Click()


' Requires reference to Microsoft Office 14.0 Object Library.
Dim fDialog As Office.FileDialog
Dim strFilename As String
Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
With fDialog
.AllowMultiSelect = False
.Title = "Save file"
.InitialFileName = "Customers_Suppliers_" & Format(Now(), "dd_mmm_yyyy") & ".xlsx"

If .Show = True Then


strFilename = .SelectedItems(1)
DoCmd.TransferSpreadsheet acExport, 10, "qryCustomersInCA", strFilename, True
DoCmd.TransferSpreadsheet acExport, 10, "Shippers Extended", strFilename, True

MsgBox ("Successfully Export to Excel")


Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub

Import data from Excel Spreadsheet into Microsoft Access


 Open the form “frmFileDialog_Import_Before” in design view.
 Select “Import from Excel” button and go to On Click event in the property box.
 Type the following code in Visual Basic for Application

Private Sub cmdImport_Click()


Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)

With fd
.AllowMultiSelect = False
.Title = "Select File"
.Filters.Clear
.Filters.Add "Excel Spreadsheet", "*.xlsx"
.Filters.Add "Excel Spreadsheet", "*.xls"
.Filters.Add "Macro Enabled Workbook", "*.xlsb"

If .Show = True Then


On Error Resume Next
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel5, "tblTemp",
.SelectedItems(1), True

CurrentDb.Execute "INSERT INTO tblStudent_Import SELECT * FROM tblTemp"


CurrentDb.Execute "DELETE * FROM tblTemp"

If Err <> 0 Then


MsgBox Err.Description & Err.Number
Exit Sub
Else
MsgBox "Successfully Imported!", vbInformation, "Data Imported!"
End If
Else
MsgBox "You click cancel button"
End If

End With
End Sub
Import data from Excel Spreadsheet by Using Function
 Open the form “frmFileDialog_Import_Before” in design view.
 Select “Import from Excel(using function)” button and go to On Click event in the property box.
 Type the following code in Visual Basic for Application

Private Sub cmdImportByFunc_Click()


Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)

With fd
If .Show = True Then
Call importCMD(.SelectedItems(1), "tblTemp", "tblStudent_Import")
End If
End With
End Sub

Function importCMD(ByVal fileToImport As String, ByVal tempTBL As String, ByVal masterTBL As String)
On Error Resume Next
'Import data from excel sheet into tempTBL
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel5, tempTBL, fileToImport, True

'Insert temp data to Master


CurrentDb.Execute "INSERT INTO " & masterTBL & " SELECT * FROM " & tempTBL

'Delete temp TABLE


CurrentDb.Execute "DELETE * FROM " & tempTBL

If Err <> 0 Then


MsgBox Err.Number & Err.Description
Else
MsgBox "Successfully imported!", vbInformation, "Data Imported!"
End If
End Function

Vous aimerez peut-être aussi