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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save File Name to a field using the file dialog box 1

Status
Not open for further replies.

BakerUSMC

Technical User
May 24, 2003
96
US
Hello to all,

I have been searching this forum for the past 2 hours and can't seem to find a post that will enable me to have a command button that when clicked, opens a file dialog box and select a file. Then save JUST the file name to a text box.

I need to have the file dialog box open to a specified folder that is entered on form_frmlocation!location and the file name to be saved in Me!Photo.

The user enters the file path on one form but on another form the name of the file needs to be entered. I am hoping to do this via VBA.

Can anyone point me in the right direction?

Thanks!!

BakerUSMC
 
Try the InStrRev and Right functions.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Check out this thread...I think it will help you. It gives some code for the common dialog...File/Open/Save

thread705-879446

Hope this is what you were looking for.
 
jtseltmann,

Thanks for the link... I've tested it but 2 things are preventing it from being perfect...

1. It only looks for .mdb files
2. It return the full file path...not just the file name.

Could you show me what to change in the module?

Thanks
 
BakerUSMC,
I created a sub to call that code to make it easier and passed the 'Dialog Title' and the 'Field' I want updated with the file name selected. The focus is then returned to that control. Use the strFilter to add or remove entries for file types you want to limit to.

Public Sub BrowseButtonFilename(DialogTitle As Variant, FieldName As Object)

Dim strFilter As String
Dim lngFlags As Long
Dim DefaultExt As Variant
Dim Filename As Variant
Dim z As Integer
Dim check As Variant
StartDir = CurDir
strFilter = ahtAddFilterItem(strFilter, "Csv Files (*.csv)", "*.csv")
strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.txt")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
Filename = ahtCommonFileOpenSave(InitialDir:=StartDir, Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, DefaultExt:="", Filename:="", DialogTitle:=DialogTitle)
z = 0
For z = 1 To Len(Filename)
check = Right(Filename, z)
If Left(check, 1) = "\" Then
Filename = Right(Filename, z - 1)
End If
Next z

If Nz(Filename, "") = "" Then
FieldName = Filename
FieldName.SetFocus
End If

End Sub
 
NOTE!!
Change the last if/then to read:

If Nz(Filename,"") <> "" Then...

had to modify mine to make it generic and typed that wrong!
 
Thanks for the posting. I will try it first thing in the morning.

Thanks again
 
jtseltmann,

I am not sure where to place your coding... Do I put it in the click event of a button or a new module?

Thanks...
 
B,
You can add the BrowseButtonFilename sub to the same module if you like. Then all the code is in the one place. Then on your form set up a button next to your text field that says 'Browse' or whatever you like. Then on the _Click event you would call the Sub. Pass it the title for the dialog box and the object from the form that you want populated with the filename selected. This sub basically takes the place of the GetOpenFile Function supplied with the code.

Code:
Private Sub cmdBrowse_Click()

Call BrowseButton("Please select the input file", Me.txtFilename)

End Sub
 
jtseltmann,

That works perfectly!! You deserve a STAR for this... with your last piece of code I had to change Call BrowseButton to Call BrowseButtonFileName to match the sub you wrote before...

But thanks again!!!!!

BakerUSMC
 
Yes, my bad! I have two subs set up. One returns the full path and one returns just the file name. They are called BrowseButton and BrowseButtonFilename. I cut/paste the wrong one for the sample! (yea, i meant to do that!)

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top