eyes59
asked on
VBA code error
Trying to let user select a file to import. The code goes to the predetermined area but when a file is selected, nothing happens. The selection is not recognized.
Private Sub NewImportSelected_Click()
Dim strfilter As String
Dim strSaveFilename As String
strfilter = ahtAddFilterItem("Excel Files (*.xls)", "*.xls")
strSaveFilename = ahtCommonFileOpenSave(Open File:=True , Filter:=strfilter, _
Flags:=ahtOFN_OVERWRITEPRO MPT Or ahtOFN_READONLY) 'set OpenFile to true if selecting a file to open
DoCmd.SetWarnings False
If strSaveFilename <> "" Then
DoCmd.TransferSpreadsheet acImport, 8, "Open Items", strSaveFilename, True, "Detail!A12:T40000"
'DoCmd.DeleteObject acTable, "_ImportErrors"
MsgBox " Import Complete "
End If
DoCmd.SetWarnings True
End Sub
Private Sub NewImportSelected_Click()
Dim strfilter As String
Dim strSaveFilename As String
strfilter = ahtAddFilterItem("Excel Files (*.xls)", "*.xls")
strSaveFilename = ahtCommonFileOpenSave(Open
Flags:=ahtOFN_OVERWRITEPRO
DoCmd.SetWarnings False
If strSaveFilename <> "" Then
DoCmd.TransferSpreadsheet acImport, 8, "Open Items", strSaveFilename, True, "Detail!A12:T40000"
'DoCmd.DeleteObject acTable, "_ImportErrors"
MsgBox " Import Complete "
End If
DoCmd.SetWarnings True
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It might be helpful to put in a Debug.Print line to display the value stored in strSaveFilename, to see if it is correct. Also, step through the code to see which line triggers the error. It might be with the TransferSpreadsheet statement.
I like to use named arguments, and save all values to variables before using them to set the arguments, for greater clarity, as in this sample from my new Working with Excel ebook:
DoCmd.TransferSpreadsheet transfertype:=acImport, _
spreadsheettype:=acSpreadsheetTypeExcel9, _
tablename:=strTable, _
FileName:=strXLFile, _
hasfieldnames:=True, _
Range:=strRange
This method lets you examine each variable in the Immediate window, using Debug.Print statements, or the Watch window, in case there is a problem.
ASKER
The link to this was extreemly helpful:
http://www.mvps.org/access/api/api0001.htm
http://www.mvps.org/access/api/api0001.htm
FileBrowser-Import.mdb