 |
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

July 10th, 2004, 01:42 AM
|
Registered User
|
|
Join Date: Jul 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
File Field - How to browse for a file?
Hi,
I have a field to store the path and name of a file, and I want to design a form (access 2000) to browse for the file, How can I do this? Maybe I should use an Activex component, but I do not know which, how to configure and use it? Do you have form examples?
Thanks,
Salvador Hernandez
|

July 10th, 2004, 02:35 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Hello Salvador,
No ActiveX controls necessary. You can just use the Windows Open File dialog with an API call. The following will load the full path and file name of a file into a textbox for you.
Just place a textbox and command button on your form and place the following in the command buttons OnClick event:
**Code********
Private Sub cmdFindFile_Click()
On Error GoTo Errorhandler
Me.txtFile = OpenFile(Me.txtFile)
Exit Sub
Errorhandler:
MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"
End Sub
**End Code********
Then place the API call in a standard module, along with a little utility function to handle null characters...
**Code********
Option Compare Database
Option Explicit
Type FileSelInfo
hwndOwner As Long
strApp As String * 255
strTitle As String * 255
strButton As String * 255
strFile As String * 4096
strDir As String * 255
strFilter As String * 255
lngIndex As Long
lngView As Long
lngFlags As Long
End Type
Declare Function GetFileInfo Lib "msaccess.exe" Alias "#56" (FSI As FileSelInfo, ByVal fOpen As Integer) As Long
Function OpenFile(Optional strFile As Variant = Null, Optional strFilter As String = "All Files (*.*)", Optional strDir As String = "", Optional strTitle As String = "", Optional strButton As String = "")
'Use to choose a file to open
Dim FSI As FileSelInfo
On Error GoTo Errorhandler
With FSI
.lngFlags = 0
.strFilter = RTrim(strFilter) & vbNullChar
.lngIndex = CInt("0")
If Not IsNull(strFile) Then
.strFile = RTrim(strFile) & vbNullChar
End If
.strTitle = RTrim(strTitle) & vbNullChar
.strButton = RTrim(strButton) & vbNullChar
.strDir = RTrim(strDir) & vbNullChar
End With
If GetFileInfo(FSI, True) = 0 Then OpenFile = TrimNull(FSI.strFile)
GoTo Done
Errorhandler:
MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"
Done:
End Function
Private Function TrimNull(str As String) As String
Dim i As Integer
TrimNull = str
i = InStr(str, vbNullChar)
If i > 0 Then TrimNull = Left(str, i - 1)
TrimNull = (RTrim(TrimNull))
End Function
**Code********
HTH,
Bob
|

July 27th, 2004, 10:16 AM
|
Registered User
|
|
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Found this code very useful to open existing files. Could someone please post the corresponding save file code.
Thanks,
Rihallix
|

July 27th, 2004, 12:44 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Hi Rihallix,
What version of Access are you using? If its 2K2 or 2K3, you can simplify all this by using the FileDialog property of the Application object:
From the Help file:
' Display the Save As dialog box.
Dim dlgSaveAs As FileDialog
Set dlgSaveAs = Application.FileDialog( _
FileDialogType:=msoFileDialogSaveAs)
dlgSaveAs.Show
' Display the Open dialog box and allow a user to select multiple files to open.
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog( _
FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
If you need the API call let me know.
HTH,
Bob
|

July 27th, 2004, 01:13 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
Check out the following sample db assembled by Candace Trip for API calls to invoke three of the Windows Common Dialogs: color, open file, and save. The sample is at the bottom of the "Access 2000/2002" section of the page and is called "Windows Common Dialogs."
http://www.candace-tripp.com/_pages/..._downloads.asp
Or if you have a copy of the Access 2x Developer's Handbook, Ken Getz has but together a cool Common Dialog class module that encapsulates API calls for invoking the file open/save, color and font dialogs.
HTH,
Bob
|
|
 |