Hi everybody, hope you are all good. I am having an issue with the TransferSpreadsheet method. Currently I have an access database with a table named "PymtElc" and would like to import data to this table from a spreadsheet named the same way. I am using a code I found on the internet and made some changes, but is not working.It browses the file but does not add the information on the spreadsheet on the table of the database. What I would like to achieve here is to browse the file, choose it, and after that import it to my access database. Since the code needed a form named just like the table I had created it on a spreadsheet format, but after the code runs it is set as a normal form and not datasheet. I believe it is important that the Excel file and the database are located in a shared folder of a shared drive. Can you please help me with this?
Code below:
Code below:
Code:
Private Sub Command0_Click()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim CustomerFile As String
' Set up the File Dialog.
On Error GoTo Import_Posting_Dates_Error
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow only one file to be selected
.AllowMultiSelect = False
' Set the title of the dialog box.
.title = "Please select your customer file."
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel Spreadsheets", "*.xls"
.Filters.Add "Excel Spreadsheets", "*.xlsx"
' Set the default location to the current user's desktop
.InitialFileName = "P:\USBA"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file selected and add it to the list box.
For Each varFile In .SelectedItems
CustomerFile = varFile
Next
End If
End With
' See error handler, what to do if operation is cancelled (2522)
DoCmd.TransferSpreadsheet acImport, 10, _
"PymtElc", CustomerFile, True
DoCmd.OpenForm "PymtElc"
On Error GoTo 0
Exit Sub
Import_Posting_Dates_Error:
If Err.Number = 2522 Then ' 2522 missing argument (File Name) - i.e no file selected
MsgBox "No file selected, operation cancelled " & "( " & Err.Number & " ).", vbInformation, "APP NAME"
Else
MsgBox Err.Number & " (" & Err.Description & ")", vbInformation, "APP NAME"
End If
End Sub