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!

Common Dialog 15

Status
Not open for further replies.

ninash

Technical User
Jul 6, 2001
163
0
0
GB
Hi All,

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???

Thanks in advance

Tony
 
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.
'******************************************************************************************

dlgBrowse.CancelError = True

dlgBrowse.DialogTitle = "Put your title here"
dlgBrowse.InitDir = "c:\"
dlgBrowse.FileName = "YourDefaultFileName.typ"
dlgBrowse.Filter = "DefaultName (DefaultName*.txt)|DefaultName*.txt|Text (*.txt)|*.txt|All Files (*.*)|*.*"
dlgBrowse.DefaultExt = "txt"

dlgBrowse.FilterIndex = 1
dlgBrowse.Flags = 0
dlgBrowse.MaxFileSize = 260


dlgBrowse.ShowOpen

If (Len(dlgBrowse.FileName) > 0) Then

txtOutputFile = dlgBrowse.FileName
End If

ExitProcedure:

Exit Function

ErrHandler:

If (Err <> cdlCancel) Then
MsgBox Err.Description, vbExclamation
End If

Resume ExitProcedure

End Function
 
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

Tony
 
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.
 
ninash,

Point your browser to
There is wonderful information there about doing a lot of the most common things, and some uncommon ones, that people around here ask about.

Be sure to give people a star when they are helpful!


Onwards,

Q-
 
Quintios

I do leave stars for those that help me....

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..

Tony
 
Don't forget to set a Reference to &quot;Microsoft Common Dialog Control&quot;.

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.
 
Sorry, didn't mean to imply that you didn't give stars.

I think FancyPrairie is correct, check the references when you are editing the VBA code.

What version of Access are you using?
Onwards,

Q-
 
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..

Sorry to sound a little dumb

Tony
 
Hi ninash,
In form design view click on the &quot;More Controls&quot; button, scroll down to the control named &quot;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.

Regards,
gkprogrammer
 
I should also mention that the &quot;More Controls&quot; button is located on the &quot;ToolBox&quot; toolbar. Regards,
gkprogrammer
 
Thanks GKProgrammer

I have often seen the more controls item but never have I gone into it. As soon as I get to my other machine I will give it a shot.

I thank FancyPrairie and yourself for your patience during this query.

You both have worked hard and deserve a couple of stars

Thanks Again
 
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):

*************BEGIN CODE***************************

Option Compare Database
Option Explicit

'declare library
Private Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetOpenFileNameA&quot; (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 = &quot;Cancelled&quot;
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 = &quot; &quot;
FileTitle = &quot; &quot; 'set original values for class
InitDir = &quot;C:\&quot;
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
 
Hi again,

Stuck again...

pasted your code into a module... fine
Generated a table and form
Created a button on form called your function

eg...
Private Sub Command0_Click()

Call ShowOpen(&quot;FilePath&quot;)

End Sub

Now what I was expecting was the field FilePath to get the path to the file pasted into it...

I might be going silly in my old age

Can you help me again??

Tony
 
ninash,

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(&quot;Your Title&quot;)

----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
 
ninash,

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(&quot;Your Title&quot;)
----------------------
scking@arinc.com
Life is filled with lessons.
We are responsible for the
results of the quizzes.
-----------------------
 
Hi nina,

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(&quot;Title of Dialog Here&quot;)



Regards,
gkprogrammer
 
Hi Again,

I think I must be stupid

I have followed all of your instuctions but in testing the routine using the msgbox trick the path that is returned is a null....

It opens the file requester allows me to navigate to a file and double click it to finish but it still comes up NULL

Am I being silly or is there something else I am missing here

Sorry to be a right royal pain

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top