Is it possible to use something like Common Dialog within Access?
If so How do I add it and use it for something like saving paths to files I want to use???
Insert the ActiveX control "Microsoft Common Dialog Control". Then add a command button to your form so when pressed launches the following code. Note that the properties set in this code can be set in the property sheet of the ActiveX control. Also, the name I gave to the ActiveX control is "dlgBrowse"
Function Browse()
On Error GoTo ErrHandler
'******************************************************************************************
'* The following CommonDialog properties can be set via the Custom property dialog box.
'* They are set here in case the custom property is overwritten for some reason.
'******************************************************************************************
Sorry I didn't make myself clear in my original query
What I need to do is allow my users to go off and find a file then I need to store the path complete with file name to allow me to use this later in the report section of this database
Again I am sorry for not explaining myself properly
This is what FancyPrairie has given you, modify the name of txtOutputfile to some variable of your choice. That variable will contain the path of the file that has been selected, you can then save that to your db.
However I am still having a problem with this common dialog query
I have used the code that FancyPrairie provided only to be told by access that the objuect was missing.
I have followed the instructions but it still fails to work,
I have tried examining the code that is what prompted my reply, obviously there is something I am not understanding about the code and how to action it..
Don't forget to set a Reference to "Microsoft Common Dialog Control".
Also, dlgBrowse is the name I gave to the ActiveX control I inserted in my form. If you did not change the name of your ActiveX control to match mine or change dlgBrowse to match yours, you'll have problems. And, as GKProgrammer pointed out, txtOutputFile is a text control.
If this doesn't solve your problem, post your code and let us look at it.
I think where I am going wrong or where I am being confused is the statement you are making
The Activex control I have inserted into the form...
I have referenced the common dialog and attached the code to a button, what exactly do I have to insert and how do I get to it
In visual basic it is a case of dragging the common dialog to the form then writing the code under the button as you have shown me here. However in access I can't see how I would do this..
Hi ninash,
In form design view click on the "More Controls" button, scroll down to the control named "Microsoft Common Dialog Control, version (whatever version you have). Click on it and add it too your form. To make it work with the code above rename it to dlgbrowse. The variable txtoutputFile in the code above will then recieve the filename path selected from the common dialog box.
Actually the control is not necessary if you use Windows code. When a database application is distributed with the control dropped on a form, then every machine would have to have a copy of the control registered. This has the potential to cause errors when the new host system did not have the control and the cause for much gnashing of teeth.
----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
scking is correct I always use something like the following instead of the common dialog control (place this code in a seperate module and it can be called from your command button):
'declare library
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 'create class type to call file open dialog box
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 mvarDialogTitle As String
Private mvarFileName As String
Private mvarFileTitle As String
Private mvarInitDir As String
Private mvarMaxFileSize As Integer
Private mvarhWnd As Long
Public Property Let hwnd(ByVal vData As Long)
' The owner of the window
mvarhWnd = vData
End Property
Public Property Get hwnd() As Long
hwnd = mvarhWnd
End Property
Public Function ShowOpen(Title As String) As String
Dim Extension As String, Name As String, StrPath As String
Dim OFN As OPENFILENAME
Dim Result
Dim retval As Long
Dim FileTitle As String
Dim intNullChr As Integer
On Error GoTo ErrHandler
Class_Initialize (Title) 'initialize class with title passed
With OFN
.hwndOwner = hwnd
.hInstance = 0
.lCustData = 0
.lpfnHook = 0
.lpstrFile = FileName & String$(MaxFileSize - Len(FileName) + 1, 0)
.lpstrFileTitle = FileTitle & Space$(256) 'set values for class object
.lpstrInitialDir = InitDir
.lpstrTitle = DialogTitle
.lStructSize = Len(OFN)
.nMaxFile = MaxFileSize
.nMaxFileTitle = 260
End With
retval = GetOpenFileName(OFN) 'open dialog box for user to select file
If retval > 0 Then
With OFN
StrPath = Left$(Trim$(.lpstrFile), .nFileOffset)
Name = Left$(Trim$(.lpstrFileTitle), Len(Trim$(.lpstrFileTitle)) - 1) 'get path,name and extension of file
Extension = Right$(Name, 3)
ShowOpen = StrPath & Name 'set to path and file name
End With
Else
ShowOpen = "Cancelled"
End If
Exit Function
ErrHandler:
Resume Next
End Function
Public Property Let MaxFileSize(ByVal vData As Integer)
' The maximum length of file name returned
mvarMaxFileSize = vData
End Property
Public Property Get MaxFileSize() As Integer
MaxFileSize = mvarMaxFileSize
End Property
Public Property Let InitDir(ByVal vData As String)
' Directory to open window in
mvarInitDir = vData
End Property
Public Property Get InitDir() As String
InitDir = mvarInitDir
End Property
Public Property Let FileTitle(ByVal vData As String)
' The name of the file without path
mvarFileTitle = vData
End Property
Public Property Get FileTitle() As String
FileTitle = mvarFileTitle
End Property
Public Property Let FileName(ByVal vData As String)
' Name of the file, including path
mvarFileName = vData
End Property
Public Property Get FileName() As String
FileName = mvarFileName
End Property
Public Property Let DialogTitle(ByVal vData As String)
' The name of the dialog box
mvarDialogTitle = vData
End Property
Public Property Get DialogTitle() As String
DialogTitle = mvarDialogTitle
End Property
Private Sub Class_Initialize(Title As String)
DialogTitle = Title
FileName = " "
FileTitle = " " 'set original values for class
InitDir = "C:\"
MaxFileSize = 260
hwnd = Screen.Application.hWndAccessApp
End Sub
****************end code*********************
Call the function ShowOpen with the title of the dialog box and it will return the path of the file selected. Regards,
gkprogrammer
You are using a sub call on a method. It appears you need to paste the above code into a class not a module. Property Let/Get does not exist in modules. Then, to use it you would need to do something like the following in your code to invoke it.
Dim strFilename As String
Dim clsBrowse As clsBrowserClass
Set clsBrowse = New clsBroswerClass
You are using a sub call on a method. It appears you need to paste the above code into a class not a module. Property Let/Get does not exist in modules. Then, to use it you would need to do something like the following in your code to invoke it.
Dim strFilename As String
Dim clsBrowse As clsBrowserClass
Set clsBrowse = New clsBroswerClass
strFilename = clsBrowse.ShowOpen("Your Title"
----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
You are close, the text you place as the parameter passed is just the title of the dialog box, you must set a variable equal to the value returned by the function you are calling:
Dim strFileName as string 'this will contain the file path
strfFileName = YourModuleName.ShowOpen("Title of Dialog Here"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.