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

Opening a Windows Explorer Window and capturing the file name 1

Status
Not open for further replies.

Mayhem9

Technical User
Dec 19, 2009
155
AU
Hello,

I am hoping someone might be able to help me with the code posted in this thread: thread702-1289131

There are a couple of comments calling for the ANSI version of the function if NT is not used. I am using XP and Vista machines.

The second issue I have is that I am not sure that I understand how to implement it. I think that it will enable me to do what I want to do, which is to browse for a file and save the file name into the field of a form.

The field name that I wish to populate is ImagePath, which I am guessing I place into the second code to call the file name. I was planning on using a button (AddImage) to open the explorer window.

Any help would be greatly appreciated.
 
How are ya Mayhem9 . . .

You should find my post in thread702-1289131 less complicated ...

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thank you both for your posts.

Me!TextboxName = BrowseFiles() variable = BrowseFiles()

OK - something weird going on here - both this link and the one I posted both point to the same post number, different title but the same code!! (cue creepy music).

AceMan - your code still confuses me and Duane's looks even more scary to a novice like me. I'll have another look at them later, off to work right now.

Cheers,
Darren
 
Edit: I don't know what hapened to my quote! But I was wrong - the title was the same, so we were both pointing to the same post AceMan.

I can see it is going to be one of those days!
 
Anyway, why not simply use thé Application.FileDialog property ?
Note: you have to reference the Microsoft Office x.0 Object Library

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Mayhem9 . . .

Sorry about the confusion. Without checking the link you posted, I simply thought you were referencing a link with code that you couldn't get working. Hence I posted my reference ... not realizing it was the same. The code is an [blue]API[/blue] with credit going to [blue]KPD-Team [/blue] allapi.
Mayhem9 said:
[blue]There are a couple of comments calling for the ANSI version of the function if NT is not used ...[/blue]
The [blue]NT[/blue] section is for 2000 and greater, while [blue]ANSI[/blue] if for legacy 95, 98, Me. Both are included to cover the full span of versions.

The following defines the arguements [blue](top to bottom)[/blue] as you see them in the code. Important to you are the items in [purple]purple[/purple]:

[tt]Arguement definitions
---------------------
hwndOwner
Initialize edit control & return path to file (Buffer)
Size in characters of above buffer
[purple]Initial Dir
Default extension
Filter Strings
Title (Title Bar Text)
[/purple][/tt]
Mayhem9 said:
[blue] I am not sure that I understand how to implement it.[/blue]
[ol][li]In a module in the modules window, copy/paste all the code in the 1st code window (the big one).[/li]
[li]Name the module [blue]modBrowseFiles[/blue].[/li]
[li]In the [blue]Click[/blue] event of a button on the form, the code would be:
Code:
[blue]Me!ImagePath = BrowseFiles()[/blue]
[/li].
[li]Set your arguements. As an example say you desire the following arguements:
[tt]Initial Dir: C:\Graphics\Holidays\
Filter : jpg
Title : Select a Graphic File[/tt]
The code would look like:
Code:
[blue]   If IsWinNT Then
      GetFileNameFromBrowseW Screen.ActiveForm.hwnd, _
                             StrPtr(sSave), _
                             255, _
                             StrPtr("[purple][b]C:\Graphics\Holidays\[/b][/purple]"), _
                             StrPtr("[purple][b]jpg[/b][/purple]"), _
                             StrPtr("purple][b]jpg[/b][/purple] files (*.[purple][b]jpg[/b][/purple])" + Chr$(0) + "*.[purple][b]jpg[/b][/purple]" + Chr$(0) + _
                                    "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), _
                              StrPtr("[purple][b]Select a Graphic File[/b][/purple]")[/blue]
[/li]
[li][blue]Thats it ... perform your testing![/blue][/li][/ol]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks to everyone for their input and help – I really do appreciate it. Pink stars all around!

A couple of little glitches but I have it working and it is going to save me so much time and work. A couple of questions:

1. It appears to ignore the last file in the directory. My images are stored in D:\Tools\Images but the window opens in D:\Tools. I have tried it with and without the end backslash but no change. Not too drastic but would finish it of nicely.
2. Is there any way to get the image to show in the form immediately after it is selected? At the moment it doesn’t show until I move out of that record and back in again. Would this be done On Lost Focus?
3. Is there a standardized way to acknowledge the author of code? I see on a number of sites that the authors specify what they wish to include. Given what you have said, is the following appropriate at the beginning of the code?

Code:
’API code by KPD-Team
‘[URL unfurl="true"]http://allapi.mentalis.org/[/URL]

Thanks again,
Darren
 
Mayhem9 said:
[blue]My images are stored in D:\Tools\Images but the window opens in D:\Tools.[/blue]
If its possible for the character length of Images + the length of any filename in the dir to be greater than 255, then you need to increase the buffer size argument. Try 500 instead of 255. I doubt this is the problem. Other than that, I don't know what to tell you.
Mayhem9 said:
[blue]Is there any way to get the image to show in the form immediately after it is selected?[/blue]
Believing your using a [blue]Picture Object[/blue], all you have to do is update the [blue]Picture[/blue] property. However, the button code needs to be revised incase the user selects [blue]cancel[/blue] from the browse window.
Code:
[blue]   Me!ImagePath = BrowseFiles()
   
   If Trim(Me!ImagePath & "") <> "" Then
      Me![purple][b]YourPictureObjectName[/b][/purple].Picture = Me!ImagePath
   End If[/blue]
Mayhem9 said:
[blue]I see on a number of sites that the authors specify what they wish to include. Given what you have said, is the following appropriate at the beginning of the code?[/blue]
Yes! ... always give credit where credit is due!

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks AceMan,

Believe it or not, it was the 255 character limit. However, I cannot see how something like D:\Tools\Images\YA246.jpg can equal more than 255 characters but it worked!

Your code worked a treat as well - thanks again!

Cheers,
Darren.
 
Hello,

Well I have been using this code without problems for browsing images and recently expanded its use to add hyperlinks to other file types (e.g. scanned manuals or service part lists).

I am curious as to how to get it to default to all files, rather than just .jpg?

I have tried

Code:
StrPtr("All"), _                             
StrPtr("All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), _
And
Code:
StrPtr("*"), _                             
StrPtr("All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), _

Neither make any difference and both default to .jpg in the dialog box.

Any help would be greatly appreciated.

Thanks,
Darren

 
Mayhem9 . . .

In its ordinal position:

[blue]StrPtr("*")[/blue] (the 5th arguement) is used with the 2nd arguement and doesn't apply to the [blue]Filter[/blue] (the 6th arguement). I doubt your using the 2nd arguement so change this to a null string [blue]StrPtr("")[/blue].

That leaves the filter to setup the default. If your filtering on more than one extension, the default is the first extension in the string. Examples:
Code:
[blue]Default to mdb files
--------------------
StrPtr("mdb files (*.mdb)" & Chr$(0) & "*.mdb" & Chr$(0) & _ 
       "mda files (*.mda)" & Chr$(0) & "*.mda" & Chr$(0) & _
       "All files (*.*)" & Chr$(0) & "*.*" & Chr$(0))

Default to All files
--------------------
StrPtr("All files (*.*)" & Chr$(0) & "*.*" & Chr$(0) & _
       "mdb files (*.mdb)" & Chr$(0) & "*.mdb" & Chr$(0) & _ 
       "mda files (*.mda)" & Chr$(0) & "*.mda" & Chr$(0))[/blue]
BTW: be sure to replace all [blue]+[/blue] with [blue]&[/blue] in the code ([blue]+[/blue] can cause problems).

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 


Based upon your code I have made the following changes to mine:
Code:
Public Function BrowseFiles()
    Dim sSave As String

    sSave = Space(350)
    If IsWinNT Then
       GetFileNameFromBrowseW Screen.ActiveForm.Hwnd, _
                StrPtr(sSave), _
                350, _
                StrPtr("c:\Tools\"), _
                StrPtr("All"), _
                StrPtr("All files (*.*)" & Chr$(0) & "*.*" & Chr$(0) & _
                "mdb files (*.mdb)" & Chr$(0) & "*.mdb" & Chr$(0) & _
                "mda files (*.mda)" & Chr$(0) & "*.mda" & Chr$(0))
                StrPtr ("Select File")
                
    End If

    BrowseFiles = Trim(Replace(sSave, Chr$(0), " "))

End Function

I get an argument not optional error when I run compile. It doesn't appear to like "GetFileNameFromBrowseW"

Guess I will have to look at it some more and see if I can figure it out.

Thanks,
Darren
 
Hello,

I am getting a compile error that confuses me. The compile error I get is Argument not optional

The code is as follows (compile error in red):

Code:
Public Function BrowseFiles()
    Dim sSave As String

    sSave = Space(350)
    If IsWinNT Then
       [COLOR=red]GetFileNameFromBrowseW Screen.ActiveForm.Hwnd, _[/color]
                StrPtr(sSave), _
                350, _
                StrPtr("D:\Tools\"), _
                StrPtr(""), _
                StrPtr("All files (*.*)" & Chr$(0) & "*.*" & Chr$(0) & _
                "mdb files (*.mdb)" & Chr$(0) & "*.mdb" & Chr$(0) & _
                "mda files (*.mda)" & Chr$(0) & "*.mda" & Chr$(0))
                StrPtr ("Select File")
                
    End If

    BrowseFiles = Trim(Replace(sSave, Chr$(0), " "))

End Function

If I remove the mdb and mda files from the code it works (defaults to all files):
Code:
Public Function BrowseFiles()
    Dim sSave As String

    sSave = Space(350)
    If IsWinNT Then
       GetFileNameFromBrowseW Screen.ActiveForm.Hwnd, _
                StrPtr(sSave), _
                350, _
                StrPtr("D:\Tools\"), _
                StrPtr(""), _
                StrPtr("All files (*.*)" & Chr$(0) & "*.*" & Chr$(0)), _
                StrPtr("Select File")
                
    End If

    BrowseFiles = Trim(Replace(sSave, Chr$(0), " "))

End Function

It also works if StrPtr(""), _ is changed to StrPtr("All"), _

I am curious as to what the problem is and whether my fix is going to cause me problems in the future?

Cheers,
Darren.
 
Mayhem9 . . .

Sorry to get back so late ... got hung up in other threads.

Arguement and line continuation characters are missing! See them in [red]red[/red] at the end of the mda line:
Code:
[blue]      GetFileNameFromBrowseW Screen.ActiveForm.hWnd, _
                StrPtr(sSave), _
                350, _
                StrPtr("D:\Tools\"), _
                StrPtr(""), _
                StrPtr("All files (*.*)" & Chr$(0) & "*.*" & Chr$(0) & _
                "mdb files (*.mdb)" & Chr$(0) & "*.mdb" & Chr$(0) & _
                "mda files (*.mda)" & Chr$(0) & "*.mda" & Chr$(0)[red][b], _[/b][/red]
                StrPtr ("Select File")[/blue]
Note when you removed the mdb & mda lines you got the arguement & line continuation characters correct!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi AceMan1,

No probs - I am sure you are a busy man on here. Besides, I haven't found enough time to work on my db and it good to try and work things out. After all, my business isn't going to fold if this is not finished today.

I tried placing the underscore before posting, as I did wonder if that was the problem but Access didn't like it (lots of red lines!!). Obviously this is because I forgot the comma! I see you left out the closing parenthesis to test me!. This fixed the problem.

Other than the obvious error, what is the difference between having the mdb and mda files in there?

Cheers,
Darren
 
Mayhem9 said:
[blue]Other than the obvious error, [purple]what is the difference between having the mdb and mda files in there?[/purple][/blue]
They're just an example of setting up [blue]specific extensions to filter on[/blue] (besides All). You should see them in the filter combobox of the browse dialog ... Try a selection! [thumbsup2]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top