The subform control I'm working on imports a delimited text file and populates a table with the imported information. It works *almost* great. It imports fine, but if a user were to accidentally hit the "Import Packing List" button again and the "Please select Packing List text file to import." box opens to the same directory where they found the file the first time, it doesn't matter if they hit "Cancel": the import goes through again, adding the same entries over again.
Here is the module with the import code (modified from an Excel Import script I found elsewhere on this site)
And here is the code for the control on the subform:
Any ideas as to what is going on?
Here is the module with the import code (modified from an Excel Import script I found elsewhere on this site)
Code:
Public ProsDataFile As String, fileerror As Boolean, FileName As String, FileGrab As String
Public varFile As Variant
Public Function GetTxtFile()
Dim fDialog As Office.FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Do not allow user to make multiple selections
.AllowMultiSelect = False
'Set the title of the dialog box
.Title = "Please select Packing List text file to import."
'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Text File", "*.txt"
'show the dialog box. If the .show method returns "true", the user picked
'at least one file. If the .show method is "false", the user clicked Cancel.
If .Show = True Then
For Each varFile In .SelectedItems
'This pulls out the file name from the path string
Dim ReversedString As String, FirstFind As Integer
ReversedString = StrReverse(varFile)
FirstFind = InStr(ReversedString, "\") - 1
FileName = StrReverse(Mid(ReversedString, 1, FirstFind))
Next
Else
MsgBox ("You either hit Cancel or did not select a file. Please try again.")
End If
End With
End Function
Code:
Private Sub conImpTxt_Click()
On Error GoTo Err_conImpTxt_Click
Call GetTxtFile
DoCmd.TransferText acImportDelim, "PL Import", "tblLI", FileName, False
DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
Exit_conImpTxt_Click:
Exit Sub
Err_conImpTxt_Click:
Resume Exit_conImpTxt_Click
End Sub