Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a "Browse" button on a form 2

Status
Not open for further replies.

chris1603

Technical User
Feb 24, 2003
17
0
0
GB
I have a field on a form which holds a filename (Complete path as a hyperlink) and I am trying to add a button next to the field so the user does not have to type (or mistype) the name but can choose by browsing. This seems to be common to many applications but I cannot find the control.

Any ideas?
Chris
 
It's the CommonDialog.

Tranman

"Adam was not alone in the Garden of Eden, however,...much is due to Eve, the first woman, and Satan, the first consultant." Mark Twain
 
Chris1603, the following will give you exactly what you need. I tested the code on a test access 2000 database and it worked great.

1. Open Your database
2. Add a command button to Your form. Set both the Name property and the Caption property to "Command1." or any name you want for the button
3. Add a text box to Form1. Set the Name property to "Text1." or any name you want to use for the text box.
4. Right-click Command1, click Properties, and then click the Event tab.
5. In the On Click event procedure, click [Event Procedure] from the drop-down list, and then click the ellipsis to start the Visual Basic Editor.
6. Modify the Command1_Click procedure to the following:
Private Sub Command1_Click()
Me!Text1 = LaunchCD(Me)
End Sub


7. On the Insert menu,of your Code View click Module.
8. Copy and paste the following code in the new module.

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (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

Function LaunchCD(strform As Form) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.hwnd
sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
"JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select a file using the Common Dialog DLL"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "A file was not selected!", vbInformation, _
"Select a file using the Common Dialog DLL"
Else
LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
End If
End Function


9. On the Debug menu, in the Code view, click Compile and Save All Modules if you are using Access 97. If you are using Access 2000 or Access 2002, click Compile Your Database Name, and then close the Code window.
10. Save Your form
11. Click New botton you created. Click a file from the browse window. The file path to the file will appear on your Text1 box.
 
Thank you both. I have got it working.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top