Function Not Working as Expected

S

Sash

Time to call in the experts!! I'm calling the function Frame50_File (code
below) to select a file to be imported into my database. What happens is it
opens explorer and I select the file. The explorer screen appears again and
when I select the file a second time it works. I tried putting break points
in the function and I can't figure out why it works the second time, but not
the first.

The section of code used to call the function:

Call Frame50_File
Filename = Frame50_File()
If Filename = "" Then Exit Sub


Function:

Public Function Frame50_File() As String

With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Import File"
.Filters.Clear
.Filters.Add "Text", "*.txt"
.Filters.Add "All Files", "*.*"
.FilterIndex = 1
.AllowMultiSelect = False
.InitialFileName = "\u:interfaces/import/"
result = .Show
If (result <> 0) Then
Returnfilename = Trim(.SelectedItems.Item(1))
Else
MsgBox "No file selected"
Returnfilename = ""
Forms!MainForm.[Frame50] = Null
End If

If Returnfilename = "" Then
Exit Function
Else
Frame50_File = Returnfilename
End If

End With
End Function
 
D

Dennis

You are calling the function twice.
Call Frame50_File will call the function but does nothing on its return.
Your next line Filename = Frame50_File()
calls it again but gets the filename returned, hence it works the 2nd time.
Remove your line Call Frame50_File
 
S

Sash

Perfect!!! I'm self taught and stumble through this stuff most of the time.
Learn so much from this group!!!

Dennis said:
You are calling the function twice.
Call Frame50_File will call the function but does nothing on its return.
Your next line Filename = Frame50_File()
calls it again but gets the filename returned, hence it works the 2nd time.
Remove your line Call Frame50_File

Sash said:
Time to call in the experts!! I'm calling the function Frame50_File (code
below) to select a file to be imported into my database. What happens is it
opens explorer and I select the file. The explorer screen appears again and
when I select the file a second time it works. I tried putting break points
in the function and I can't figure out why it works the second time, but not
the first.

The section of code used to call the function:

Call Frame50_File
Filename = Frame50_File()
If Filename = "" Then Exit Sub


Function:

Public Function Frame50_File() As String

With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Import File"
.Filters.Clear
.Filters.Add "Text", "*.txt"
.Filters.Add "All Files", "*.*"
.FilterIndex = 1
.AllowMultiSelect = False
.InitialFileName = "\u:interfaces/import/"
result = .Show
If (result <> 0) Then
Returnfilename = Trim(.SelectedItems.Item(1))
Else
MsgBox "No file selected"
Returnfilename = ""
Forms!MainForm.[Frame50] = Null
End If

If Returnfilename = "" Then
Exit Function
Else
Frame50_File = Returnfilename
End If

End With
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top