Insert file path in text box on double click of the file

ajetrumpet

Banned
Local time
Today, 17:25
Joined
Jun 22, 2007
Messages
5,638
All,

I am almost sure this can't be done, because by default, a file opens on a double click of it's name. However, what I want a do is open a directory on double click of my "fake" label, and then have the user choose the file (I wish I could get them to double click on it) they want, and populate the text box on the form with the file's path. Any suggestions on there on how this "double click" wish of mine could be accomplished? I have the directory open already, but I do not know the code, nor the windows manipulation technique to accomplish the other part of this. I am thinking it's not possible. But...I am also thinking that an API setting in windows (whichever setting relates to the double clicking of a file name) can be changed upon double click of the label when it opens the directory window. Does this sound right to anybody?

Thank you all for any help...
 
Last edited:
I take it you don't want to use a file dialog object? (Application.FileDialog) That's the standard way of doing stuff like this.

If not, it'll take me a little while to figure out. Maybe somebody else can kick in while you're waiting.
 
I knew the code for this once George, but I'm not good at it either. I know this is the standard way of doing it, and I've don't think I have that code stored anymore either. I will try and figure it out that way. thanks!
 
Try this:
Code:
Function GetFileName() As String
Dim x As FileDialog ' You MUST add a reference to Microsoft Office X Object Library.
    Set x = Application.FileDialog(msoFileDialogFilePicker)
    With x
        .AllowMultiSelect = False
        .Filters.Clear
        'You can change the filter or add new filters in this manner.
        .Filters.Add "All", "*.*"
        If .Show Then
            GetFileName = .SelectedItems(1)
        Else
            GetFileName = ""
        End If
    End With
End Function

Use like this:
Code:
?getfilename
C:\Users\Dor\Documents\Downloads\World of Warcraft - The Burning Crusade\WoW-1.12.0-enGB-Installer\Installer Tome 3.mpq

or

Code:
Private Sub txtAdamsField_Click() 'Or whatever event is appropriate for you.
    Me.txtAdamsField = GetFileName
End Sub
 
I just got done figuring it out George. Sorry! :) Here is the article I read:

http://support.microsoft.com/kb/288543

Here is the code that I used:
Code:
Private Sub lblresume_DblClick(Cancel As Integer)

Dim fd As FileDialog
Dim vrtSelectedItem As Variant

  Set fd = Application.FileDialog(msoFileDialogFilePicker)
      
    With fd

      If .Show = -1 Then

        For Each vrtSelectedItem In .SelectedItems
          Me.resume = vrtSelectedItem
          Me.lblresume.Visible = False
        Next

      End If

    End With

  Set fd = Nothing

End Sub
It works fine, and inserts the path in the textbox. Only thing I wonder though George, is how to replace this:
Code:
        For Each vrtSelectedItem In .SelectedItems
          Me.resume = vrtSelectedItem
          Me.lblresume.Visible = False
        Next
without a loop! I don't want one in there. There is no use for it. And, I'd like to (if I could), prevent the user from selecting more than one file. An "IF" statement perhaps? That is simple. I don't need help with that. But, I don't understand why this:
Code:
      If .Show = -1 Then
        Me.resume = fd.SelectedItems(vrtSelectedItem)
does not work for me as a substitute for the loop! Apparently, that collection doesn't act the same as say, a tabledef collection? You can refer to names in "tabledefs", but this collection requires the indexed integer representation of the object when using the above substitute. Any thoughts on this?
 
Adam, in that thread I started about boolean and Yes/No data type being not same, Brent bought attention to the fact that this version is more 'buggy':

Code:
If Something = -1 Then

Compared to this:

Code:
If Something Then

This is because in any programming language, we define false as 0 (zero) then define True as True=Not False. Now, in VBA (and VB6?), the True value for a boolean is -1, while the rest usually display True as 1 but they are actually identical! This is what we see in bits:

Code:
11111111 11111111

(Note: Some programming language may use more or less bytes than 2, but in VBA's case 2 bytes is used here.)

Now why does VBA return a -1 while rests return 1? Well, it's because VBA uses a signed integer data type, so this bit:

Code:
[color=red]1[/color]1111111 11111111

acts as the sign and thus makes the whole number a negative number (and we start counting backward, too a 1 followed by 15 0s is actually bigger than the all 1s). In most other programming languages, 1 is return because the data type is assumed to be unsigned (and thus can hold a bigger number)


Now the problem is that if you force a comparison to -1, how does that works?

Code:
?1=-1
False

?-2=-1
False

0=-1
False

So therefore, unless you explicitly want a -1 and not just generic "True" value, you should only evaluate that it is *NOT* a zero, rather than *IS* -1.


I'm not sure why Microsoft use .Show = -1, but if using If .Show Then is wrong, then I don't wanna be right!
 
Agreed, you should never use -1, or 1 when checking boolean fields or properties. It's usually just best to not bother comparing a boolean field to something else because the value of the field or property already tells you everything you need to know, i.e. True or False.
 
It works fine, and inserts the path in the textbox. Only thing I wonder though George, is how to replace this:
Code:
        For Each vrtSelectedItem In .SelectedItems
          Me.resume = vrtSelectedItem
          Me.lblresume.Visible = False
        Next
without a loop!

That's what the code I wrote in post 5 does...no loop. You don't need one to fill a text box.

Just put my code in a module and replace your code with this:
Code:
Private Sub lblresume_DblClick(Cancel As Integer)
     Me.resume = GetFileName
     Me.lblresume.Visible = False
End Sub
 

Users who are viewing this thread

Back
Top Bottom