Results 1 to 12 of 12

Thread: [RESOLVED] Open a filedialog

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Resolved [RESOLVED] Open a filedialog

    Hello. Can one of these(please refer to attatchment) be opened with vb code? And if so how? I want the user to pick a certain file and when they click open, on the filedialog, it puts the path into a text box. I need to know if there is a command I can use to open windows default filedialog box and obtain a string from it. Thanks.

    And while we are on the subject how do I populate some sort of list with a directory tree of a computers drive? Thats just in case I want to build a filedialog form in the future.


    P.S. I really did do a search for it here first...
    Attached Images Attached Images  
    Last edited by Brian M.; Jun 16th, 2007 at 08:33 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Open a filedialog

    You can do something like this:
    Code:
    Private Sub Command2_Click()
    Dim sText As String
    
    On Error GoTo ErrHandler
    
        With CommonDialog1
            .CancelError = True
                
            .Filter = "General Modules (*.bas)|*.bas"
            .ShowOpen
            
            If Not .FileName = "" Then
                
                'write selected file name into textbox
                Text1.Text = .FileName
                
                'or if you want to load text from file then use this instead
                'NOTE: you will need to set Multiline and ScrollBars properties for your textbox
                Open .FileName For Input As #1
                    sText = Input(LOF(1), #1)
                Close #1
                Text1.Text = sText
                sText = ""
                
            End If
            
        End With
        
        Exit Sub
    
    ErrHandler:
    
        Err.Clear
        Exit Sub
    
    End Sub

  3. #3

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Open a filedialog

    Wow, that was fast.
    Last edited by Brian M.; Jun 16th, 2007 at 10:36 PM.

  4. #4

  5. #5

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Open a filedialog

    You are too fast... Thanks I got it now...

  6. #6

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Open a filedialog / Re: Variable Not Defined

    Ok, when I run this program and click the open path button, I get a Compile Error: variable not defined. It highlights the Private Sub getpathbtn_Click() in yellow and it highlights the CommonDialog1 in blue. I only have a button named getpathbtn and a text field named dbpathtxt. I am running vb 6.0 enterprise w/service pack 6 on 98se.

    Do I have to dim CommonDialog1 as something or maybe declare it? What about libraries? Do I need to go check a dll? And if I do, what do I dim it as, or what library do I click? See code below. Thanks again.

    VB Code:
    1. Option Explicit
    2.     Dim mess As Integer
    3.  
    4. Private Sub getpathbtn_Click()   'highlights this whole line in yellow
    5.  
    6. On Error GoTo ErrHandler
    7.  
    8.     With CommonDialog1             'highlights CommonDialog1 in blue
    9.         .CancelError = True
    10.            
    11.         .Filter = "General Modules (*.mdb)|*.mdb"
    12.         .ShowOpen
    13.        
    14.         If Not .FileName = "" Then
    15.            dbpathtxt.Text = .FileName
    16.         Else
    17.            mess = MsgBox("Not a valid path to a database.", vbOKOnly)
    18.         End If
    19.        
    20.     End With
    21.    
    22. Exit Sub
    23.  
    24. ErrHandler:
    25.     Err.Clear
    26. Exit Sub
    27.  
    28. End Sub

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Open a filedialog

    you need to add a commondialog control to your form

    go to project > components and tick microsoft common dialog control 6.0
    then add a commondialog control to your form from the toolbox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Open a filedialog

    Thank you very much. Its working, for now. I was searching around in references. Well, I was close...

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Re: [RESOLVED] Open a filedialog

    How do you leave the file path away so you would only have text.txt ?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Open a filedialog

    Use the dialog's .FileTitle property
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    31

    Re: [RESOLVED] Open a filedialog

    Smooth LaVolpe.

    It works.

    Thanks.

  12. #12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width