Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Text Happens Despite Clicking "Cancel" 1

Status
Not open for further replies.

TravisT42

Technical User
Jan 7, 2008
7
US
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)

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
And here is the code for the control on the subform:

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
Any ideas as to what is going on?
 
...
Else
MsgBox ("You either hit Cancel or did not select a file. Please try again.")
[!]FileName = ""[/!]
End If
...
Call GetTxtFile
[!]If FileName = "" Then Exit Sub[/!]
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Now I know the answer to the question "Who rocks the house?"

Thanks PHV!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top