I have two excel files I need to append to an access database. These two files will be pulled from a folder on a continual basis. Once I append these two files I plan on deleting them from the folder. Then the process starts again where the folder will buildup with new files that I will need to append to the database once again.
Example: Student-TXCB01 needs to be appended to Student_Table_Import. Course-TXCB01 needs to be appended to Course_Table_Import. Student and Course will always be part of the filename. What appears after the hypen will change.
I want to build a button with code to do this process automatically.
Subscribe to get a FREE chapter from Windows 7 The Missing Manual
This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
Here's one way to do what you want. I hope this works for you.
Code:
Sub ImportExcelData()
Dim zXLFPath As String
Dim zXLFName As String
Do
zXLFName = InputBox("Enter the Excel Filename ONLY!" & vbCrLf & _
"DO NOT enter the path or extension", "Excel File Name Entry")
Loop Until Trim(zXLFName) <> ""
If Trim(UCase(zXLFName)) = "EXIT" Then Exit Sub
zXLFPath = "G:\Excel\Archives\" & zXLFName & ".xls"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Student_Table_Import", zXLFPath, True
End Sub
Notes:
You'll need to change the zXLFPath directory path to point to the directory holding your files.
You can add variables to prompt for the table to be used if you want to use the same code for both imports just follow the example used for prompting for the file name.
You many need to change this constant {acSpreadsheetTypeExcel12} if you are using a version of excel earlier than 2010.
Good points both! Thanks for cleaning up my code once again.
FYI: If you add the x to .xls to get .xlsx for 2007 & 2010 Excel you also need to change the acSpreadsheetTypeExcel12 to acSpreadsheetTypeExcel12xml as appropriate for your version.
Instead of a input box and typing in a filename can I get a browse folder and then pickup the file I want to import? Typing in the filename each time is too time consuming.
Give this a try. It also addresses some of John's points as it allows selecting .xls or .xlsx files and auto adjusts the file type in the Transfer command and also only allows you to pick existing files.
Code:
Sub ImportXLS()
Dim zXLFPath As String
Dim zXLFName As String
Dim iFileType As Integer
zXLFPath = PickFileDialog("G:\BEKDocs\Excel\Archives")
If Trim(UCase(zXLFName)) = "EXIT" Then Exit Sub
If UCase(Right(zXLFPath, 1)) = "X" Then
iFileType = acSpreadsheetTypeExcel12Xml
Else
iFileType = acSpreadsheetTypeExcel12
End If
DoCmd.TransferSpreadsheet acImport, iFileType, "BridesDB", zXLFPath, True
End Sub 'ImportXLS
Private Function PickFileDialog(zTargetDir As String) As String
'Requires reference to Microsoft Office 14.0 Object Library Office 2010.
'Note: Returns a fully qualified filename, e.g. d:\path\filename.ext
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim zCurDir As String
'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box
.AllowMultiSelect = False
.Title = "Please select the file to import"
'Clear out the current filters, and add your own.
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "Access 2007-10", "*.xlsx"
'Set the initial directory using passed argument string
.InitialFileName = zTargetDir
'Show the dialog box. If the .Show method returns True, the
'user picked a file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
cmdFileDialog = .SelectedItems(1)
Else
cmdFileDialog = "EXIT"
End If
End With
End Function 'PickFileDialog
Of course, remember to change my testing parameters in the code.
I separate the file selection from the import using a Browse button, and have a Check Data in Excel button so the user can double check the file before importing.
I find importing from Excel much more problematic than you would expect. The column headings need to be just right, and people seem to always mess with the templates you give them etc.
I never got this part of the code to work:
DoCmd.TransferSpreadsheet acImport, iFileType, "BridesDB", zXLFPath, True
It bombs on this line of code. Please help?
I never got the code to work correctly. It bombs on this line of code:
DoCmd.TransferSpreadsheet acImport, iFileType, "BridesDB", zXLFPath, True
I think I replied to RetiredGeek but I am not sure? I was wondering if the code could also append directly to a table in my database when it gets imported? Hope I am clear!--I have been on vacation and just got back to this issue.
I just tried this using RG's code with the FileDialog and I needed to make a change to the PickFileDialog function as below:
Code:
PickFileDialog = .SelectedItems(1)
PickFileDialog = "EXIT"
instead of cmdFileDialog in both cases
And this line needed to be changed, to a folder I have.
zXLFPath = PickFileDialog("G:\BEKDocs\Excel\Archives")
This line worked as is, because if the table does not exist it just creates it.
DoCmd.TransferSpreadsheet acImport, iFileType, "BridesDB", zXLFPath, True
When you say it "bombs" can you tell us more about what happens? Do you get any errors or messages?
The error message is: This action or method requires a File name argument.
This is what I have:
Private Sub Command0_Click()
Dim zXLFPath As String
Dim zXLFName As String
Dim iFileType As Integer
zXLFPath = PickFileDialog("c:\temp\txc\")
If Trim(UCase(zXLFName)) = "EXIT" Then Exit Sub
If UCase(Right(zXLFPath, 1)) = "X" Then
iFileType = acSpreadsheetTypeExcel12Xml
Else
iFileType = acSpreadsheetTypeExcel12
End If
DoCmd.TransferSpreadsheet acImport, iFileType, "Students", zXLFPath, True
End Sub 'ImportXLS
Private Function PickFileDialog(zTargetDir As String) As String
'Requires reference to Microsoft Office 14.0 Object Library Office 2010.
'Note: Returns a fully qualified filename, e.g. d:\path\filename.ext
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim zCurDir As String
'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box
.AllowMultiSelect = True
.Title = "Please select the file to import"
'Clear out the current filters, and add your own.
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls"
.Filters.Add "Access 2007-10", "*.xlsx"
'Set the initial directory using passed argument string
.InitialFileName = zTargetDir
'Show the dialog box. If the .Show method returns True, the
'user picked a file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
cmdFileDialog = .SelectedItems(1)
Else
cmdFileDialog = "EXIT"
End If
End With