CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jun 2005
    Posts
    51

    Making A "BROWSE" button

    hey all,

    I'm trying to make a browse button that will allow a user to search through their pc to locate a file... and then use the pathway and filename for later use in my program...

    I know it will probably involve making another form but it's the actualy browsing that i really have no clue how to make

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Making A "BROWSE" button

    Silly suggestion.

    Try using the FileListBox, DriveListBox and the DirectoryListbox found in your Toolbox.

    You could also consider using the BrowseForFolder Dialog:

    http://www.codeguru.com/forum/showth...rowseForFolder

    Also have a look here:
    http://www.codeguru.com/forum/showth...ht=filelistbox

    Hope it helps!

  3. #3
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    Microsoft Common Dialog Control... how exactly do i use it?

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Making A "BROWSE" button

    Quote Originally Posted by clownfish0326
    Microsoft Common Dialog Control... how exactly do i use it?
    Take a look at this link..
    http://msdn.microsoft.com/library/de...ogControl).asp

    Sample code from MSDN
    Code:
    Private Sub Command1_Click() 
    ' Set CancelError is True 
    CommonDialog1.CancelError = True 
    On Error GoTo ErrHandler 
    ' Set flags 
    CommonDialog1.Flags = cdlOFNHideReadOnly 
    ' Set filters 
    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt) *.txt|Batch Files (*.bat)|*.bat" 
    ' Specify default filter 
    CommonDialog1.FilterIndex = 2 
    ' Display the Open dialog box 
    CommonDialog1.ShowOpen 
    ' Display name of selected file 
    MsgBox CommonDialog1.filename 
    Exit Sub 
     
    ErrHandler: 
    'User pressed the Cancel button 
    Exit Sub
    End Sub
    HTH

    Edit -- Corrected the link and Syntax of the Code
    Last edited by vb_the_best; July 5th, 2005 at 09:44 AM.

  5. #5
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    I keep getting an error 424 "Object Required" and it highlights this line:

    CommonDialog1.CancelError = True

  6. #6
    Join Date
    Dec 2002
    Posts
    55

    Re: Making A "BROWSE" button

    be sure to add the Microsoft Common Dialog Control to your form

  7. #7
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    And how do i do that?

  8. #8
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Making A "BROWSE" button

    Inside your VB6 IDE,

    Goto Project > References,

    scroll down the list until you find 'Microsoft common dialog control,' tick the checkbox, click on the new button that has appeared on your toolbar and draw a box on your form.

    Phew!
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  9. #9
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    I'm sorry i should have said this earlier but I am using Visual Basic with Microsoft Access, so some directions for the commond dialog with VBA would really help

  10. #10
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Making A "BROWSE" button

    Ahh, you need to use an api call in vba....

    Think this was taken from planetsourcecode originally:

    Code:
    Option Explicit
    'Commondialog API - more efficient than using MS Common Dialog Control
    (comdlg32.ocx)
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias
    "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type
    Private Const OFN_OVERWRITEPROMPT = &H2
    Private Const OFN_ALLOWMULTISELECT = &H200
    Private Const OFN_EXPLORER = &H80000
    'UDT that makes calling the commondialog easier
    Public Type CMDialog
    Ownerform As Long
    Filter As String
    Filetitle As String
    FilterIndex As Long
    FileName As String
    DefaultExtension As String
    OverwritePrompt As Boolean
    AllowMultiSelect As Boolean
    Initdir As String
    Dialogtitle As String
    Flags As Long
    End Type
    Public cmndlg As CMDialog
    '****************COMMONDIALOG CODE*********************
    Public Sub ShowOpen()
    Dim OFName As OPENFILENAME
    Dim temp As String
    With cmndlg
    OFName.lStructSize = Len(OFName)
    OFName.hwndOwner = .Ownerform
    OFName.hInstance = App.hInstance
    OFName.lpstrFilter = Replace(.Filter, "|", Chr(0))
    OFName.lpstrFile = Space$(254)
    OFName.nMaxFile = 255
    OFName.lpstrFileTitle = Space$(254)
    OFName.nMaxFileTitle = 255
    OFName.lpstrInitialDir = .Initdir
    OFName.lpstrTitle = .Dialogtitle
    OFName.nFilterIndex = .FilterIndex
    OFName.Flags = .Flags Or OFN_EXPLORER Or IIf(.AllowMultiSelect,
    OFN_ALLOWMULTISELECT, 0)
    If GetOpenFileName(OFName) Then
    .FilterIndex = OFName.nFilterIndex
    If .AllowMultiSelect Then
    temp = Replace(Trim$(OFName.lpstrFile), Chr(0), ";")
    If Right(temp, 2) = ";;" Then temp = Left(temp, Len(temp) -
    2)
    .FileName = temp
    Else
    .FileName = StripTerminator(Trim$(OFName.lpstrFile))
    .Filetitle = StripTerminator(Trim$(OFName.lpstrFileTitle))
    End If
    Else
    .FileName = ""
    End If
    End With
    End Sub
    Public Sub ShowSave()
    Dim OFName As OPENFILENAME
    With cmndlg
    OFName.lStructSize = Len(OFName)
    OFName.hwndOwner = .Ownerform
    OFName.hInstance = App.hInstance
    OFName.lpstrFilter = Replace(.Filter, "|", Chr(0))
    OFName.nMaxFile = 255
    OFName.lpstrFileTitle = Space$(254)
    OFName.nMaxFileTitle = 255
    OFName.lpstrInitialDir = .Initdir
    OFName.lpstrTitle = .Dialogtitle
    OFName.nFilterIndex = .FilterIndex
    OFName.lpstrDefExt = .DefaultExtension
    OFName.lpstrFile = .FileName & Space$(254 - Len(.FileName))
    OFName.Flags = .Flags Or IIf(.OverwritePrompt, OFN_OVERWRITEPROMPT,
    0)
    If GetSaveFileName(OFName) Then
    .FileName = StripTerminator(Trim$(OFName.lpstrFile))
    .Filetitle = StripTerminator(Trim$(OFName.lpstrFileTitle))
    .FilterIndex = OFName.nFilterIndex
    Else
    .FileName = ""
    End If
    End With
    End Sub
    
    '****************STRING FUNCTIONS*********************
    Public Function StripTerminator(ByVal strString As String) As String
    'Removes chr(0)'s from the end of a string
    'API tends to do this
    Dim intZeroPos As Integer
    intZeroPos = InStr(strString, Chr$(0))
    If intZeroPos > 0 Then
    StripTerminator = Left$(strString, intZeroPos - 1)
    Else
    StripTerminator = strString
    End If
    End Function
    
    'End Module Code
    
    'Form Code
    
    Private Sub mnuFileOpen_Click()
    With cmndlg
    .Filter = "All files (*.*)|*.*"
    .Flags = 5
    .Initdir = OpenDir
    .Ownerform = hwnd
    ShowOpen
    If Len(.FileName) = 0 Then Exit Sub
    OpenDir = PathOnly(.FileName)
    Tag = .FileName
    Caption = App.Title & " - " & .Filetitle
    AddMRU .FileName
    'Load file code
    End With
    End Sub
    
    Private Sub mnuFileSaveAs_Click()
    With cmndlg
    .Filter = "All files (*.*)|*.*"
    .Flags = 5
    .Initdir = SaveDir
    .Ownerform = hwnd
    'prefill the dialog box with the files' name
    .FileName = IIf(Len(Tag) = 0, "Untitled.txt", FileOnly(Tag))
    .OverwritePrompt = True
    ShowSave
    If Len(.FileName) = 0 Then Exit Sub
    SaveDir = PathOnly(.FileName)
    Caption = App.Title & " - " & .Filetitle
    AddMRU .FileName
    'save file code
    End With
    End Sub
    Hope this helps
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  11. #11
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    Okay,

    To debug that code would take a lot of time, shall we go back to the drawing board?

    This is what I need:

    I'm using VBA and I need to make a "browse" button to select a file... just like any other browse button you would see in windows...

  12. #12
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Making A "BROWSE" button

    I would suggest you use the Common Dialog Control, like previously said. First, you need to add it to your project. Go into the menu Project->Components... A new window will appear with an huge list. Scroll down and find the "Microsoft Common Dialog Control", check the checkbox and click OK. Now, there is a new icon in your toolbars. Add it on your form (like you add any other control) and you will be able to use the code provided

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  13. #13
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    As I have previously stated I am using VBA so thus the directions to Project->Components to Microsoft Common Dialog Control are non existent

  14. #14
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Making A "BROWSE" button

    Then use the menu Insert->ActiveX Control...

    JeffB]
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  15. #15
    Join Date
    Jun 2005
    Posts
    51

    Re: Making A "BROWSE" button

    under "insert" there is no activex control option... only form, table, query, etc... there is an activex control under a different drop down menu but it requires that i register it
    Last edited by clownfish0326; July 5th, 2005 at 04:28 PM.

Page 1 of 2 12 LastLast

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

Featured