|
-
July 5th, 2005, 08:50 AM
#1
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
-
July 5th, 2005, 08:55 AM
#2
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!
-
July 5th, 2005, 09:29 AM
#3
Re: Making A "BROWSE" button
Microsoft Common Dialog Control... how exactly do i use it?
-
July 5th, 2005, 09:40 AM
#4
Re: Making A "BROWSE" button
 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.
-
July 5th, 2005, 10:05 AM
#5
Re: Making A "BROWSE" button
I keep getting an error 424 "Object Required" and it highlights this line:
CommonDialog1.CancelError = True
-
July 5th, 2005, 10:25 AM
#6
Re: Making A "BROWSE" button
be sure to add the Microsoft Common Dialog Control to your form
-
July 5th, 2005, 10:34 AM
#7
Re: Making A "BROWSE" button
-
July 5th, 2005, 10:48 AM
#8
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
-
July 5th, 2005, 10:53 AM
#9
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
-
July 5th, 2005, 11:06 AM
#10
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
-
July 5th, 2005, 11:22 AM
#11
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...
-
July 5th, 2005, 12:18 PM
#12
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
-
July 5th, 2005, 12:47 PM
#13
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
-
July 5th, 2005, 02:30 PM
#14
Re: Making A "BROWSE" button
Then use the menu Insert->ActiveX Control...
JeffB]
-
July 5th, 2005, 03:26 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|