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!
Re: Making A "BROWSE" button
Microsoft Common Dialog Control... how exactly do i use it?
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
Re: Making A "BROWSE" button
I keep getting an error 424 "Object Required" and it highlights this line:
CommonDialog1.CancelError = True
Re: Making A "BROWSE" button
be sure to add the Microsoft Common Dialog Control to your form
Re: Making A "BROWSE" button
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!
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
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
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...
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
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
Re: Making A "BROWSE" button
Then use the menu Insert->ActiveX Control...
JeffB]
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
Re: Making A "BROWSE" button
here you go:
Code:
Private Const VER_PLATFORM_WIN32_NT = 2
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function GetFileNameFromBrowseW Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As Long, ByVal nMaxFile As Long, ByVal lpstrInitialDir As Long, ByVal lpstrDefExt As Long, ByVal lpstrFilter As Long, ByVal lpstrTitle As Long) As Long
Private Declare Function GetFileNameFromBrowseA Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As String, ByVal nMaxFile As Long, ByVal lpstrInitialDir As String, ByVal lpstrDefExt As String, ByVal lpstrFilter As String, ByVal lpstrTitle As String) As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim sSave As String
sSave = Space(255)
'If we're on WinNT, call the unicode version of the function
If IsWinNT Then
GetFileNameFromBrowseW Me.hWnd, StrPtr(sSave), 255, StrPtr("c:\"), StrPtr("txt"), StrPtr("Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)), StrPtr("The Title")
'If we're not on WinNT, call the ANSI version of the function
Else
GetFileNameFromBrowseA Me.hWnd, sSave, 255, "c:\", "txt", "Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0), "The Title"
End If
'Show the result
MsgBox sSave
End Sub
Public Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
oh, all this from allapi.net by the way. Download it - very well worth it if you're developing on a crap version of access
cheers