Browse and Upload in Acess VBA

I needed to import an Excel file (Sheet1)and put the data into an access table. Once imported I need to perform set of updates in the table. This has to be performed daily as a routine. So I needed Browse and Upload provision in my Access form to upload the excel file. Here is the script
Dim f As Office.FileDialog
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True

Set db = CurrentDb()
Dim tbl1 As TableDef
For Each tbl1 In db.TableDefs
If tbl1.Name = "ExcelImport" Then
db.Execute "DROP TABLE ExcelImport"
End If

Next tbl1

If f.Show Then
MsgBox f.SelectedItems.Count & " file(s) were chosen."
Dim i As Integer
For i = 1 To f.SelectedItems.Count
n1 = f.SelectedItems(i)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ExcelImport", n1, True, "Sheet1!"

Next i

'You can perform your steps here

Else
MsgBox "No File is imported"
End If

To import Access table we can use
DoCmd.TransferDatabase acImport, "Microsoft Access", n1, acTable, "SourceTableName",
"DestinationTableName"

No comments:

Post a Comment