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!

adding a bowse button to a form 8

Status
Not open for further replies.

WaltLukeIII

Programmer
Jun 28, 2000
199
0
0
US
I have a form that imports an excel spreadsheet and I want to add a browse Button to the form does anyone know how to do this<br>
 
I am assuming you mean an Open dialog box.<br><br>1) On your form, add a common dialog control (Insert ¦ ActiveX Control) (Microsoft common dialog control, version 5)<br>2) Name the control &quot;cdlgOpen&quot;<br>3) Add a button<br>4) Name the button &quot;cmdBrowse&quot;<br>5) Copy and paste the following code on the <b>Click Event</b> of the button (minus the Private & End Sub lines).<br><br>-----<br>Private Sub cmdBrowse_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim strFile As String<br>&nbsp;&nbsp;&nbsp;&nbsp;With Me.cdlgOpen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CancelError = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Filter = &quot;Excel Files (*.xls)¦*.xls¦&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ShowOpen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strFile = .filename<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br>End Sub<br>-----<br><br>6) Now your variable <b>strFile</b> will represent your file and you can do with it as you wish from that point.<br><br>HTH <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
I can't find an activex control<br><br>I went under the insert menu and got a Custom Control and that brought up a window in which there where several choices. it had activeX Object but wouldn't let me do that saying that I was not registered. I am not sure if that is a version problem of what. Thanks for the help so far.<br><br>Walt III
 
Jim, I'll find this useful too. Could you demo how to add a default path and filename stub? For example I'd like mine to defualt to:<br>\\xxx\yy$\zzz*.xls<br><br>would it be strFile = \\xxx\yy$\zzz*.xls or do I have to create some kind of default setting to see what's out there?<br><br>Thanks<br><br>
 
Walt, click on the &quot;More Controls&quot; (hammer) icon on your toolbar, then click on &quot;Microsoft Common Dialog Control (in Access 2000 I have version 6.0).
 
I can not find a &quot;more controls&quot;(hamme) on my toolbar. I found a hammer and wrench but that is for the &quot;form design&quot; toolbar. I don't know if it matters but I am using an updated version of office 97. But I don't think that that should matter.
 
Walt, it's on your Toolbox's toolbar.<br><br>Jim, I'm monkeying around with the common dialog control's InitDir and FileName properties. My path is long, more like<br><br>\\aaa-bb\ccy$\eeeeee\ffffffff\gggggg\ZZZyyyymmdd*.xls<br><br>The FileName seems to work fine, but of the path only \\aaa-bb\ccy$ displays. there doesn't seem to be any Help on this to tell me if there is a character string limit.
 
I do not have that option on this version. I have looked on another version and found it do you know of a back door way to get to the &quot;Microsoft Command Diolag Control&quot;.
 
What version of access are the two of you using?<br>
 
I'm using Access 2000.<br><br>Please ignore my previous question, the InitDir wasn't working simply because I miskeyed one of the folder names, so the control only displayed the portion that was valid.
 
Elizabeth, try the following:<br><br>----------<br><br>Private Const EXISTINGFILE As String = &quot;C:\My Documents\Zips\tasks.xls&quot;<br><br>Private Sub cmdBrowse_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim strNewFile As String<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo Browse_Err<br>&nbsp;&nbsp;&nbsp;&nbsp;With Me.cdlgOpen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filename = EXISTINGFILE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CancelError = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Filter = &quot;Excel Files (*.xls)¦*.xls¦&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ShowOpen<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strNewFile = .filename<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>Browse_Exit:<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br><br>Browse_Err:<br>&nbsp;&nbsp;&nbsp;&nbsp;If Err.Number = 32755 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resume Browse_Exit<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox Err.Number & &quot;: &quot; & Err.Description<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resume Browse_Exit<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>End Sub<br><br>-----<br><br>You can put the path (EXISTINGFILE) in a table, as a public constant, or even in an .ini file.<br><br>Let me know if that works, or doesn't<br><br>BTW: I am using Access 97<br><br>HTH <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
I have managed to find the activeX control but there is no microsoft common dialog control are you using a developer edition of access?<br>if So is there another way to get what I want?<br>Thanks for the excellent help.<br><br>Walt III
 
Do you have the following file?<br><br>C:\Windows\System\Comdlg32.ocx<br><br>If you don't, then find it on somebody's system and copy it there, then continue.<br><br>If you do have it, then:<br><br>Go to the design of your form<br>Go to the code window (View ¦ Code)<br>Go to Tools ¦ ActiveX Controls<br>Select Register<br>Find the .ocx above<br>Select it, and hit Open<br><br>Now you should be able to insert it on your form by going to Insert ¦ ActiveX Control<br><br> <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
It now tells me that I do not have the liscense required to insert this object. <br>Your Persistent help has been most appreciated.
 
Jim, what's that error number 32755 that we're ignoring?
 
Hi Jim,<br>I was trying to do something similar to this when I ran across this discussion. Maybe you can help with what I am trying to do. I have added the browse button to my program per your intructions and the open dialog works just fine.&nbsp;&nbsp;Although, I only need to browse for a <font color=red>directory</font> not a filename. How would I get just the directory name?
 
FYI, InitDir is a property of the control. If you key it into the property area, the control will display all files in that directory. If you wanted to you could set that property in code instead I imagine.
 
Elizabeth, <br><br>The error is: &quot;Cancel was selected&quot;<br>If the user selects the cancel button, you get that error. <br>I thought the .CanclError = True was supposed to handle this, but it doesn't seem to, so that's why I added the err trap.<br><br>=====<br><br>hcsd<br><br>Elizabeth was right, instead of using .Filename, use .InitDir instead. This will default to the directory you specify.<br><br>You can hard code it, use a variable, constant, value from a table, or a value in an .ini file, whatever you prefer.<br><br>=====<br><br>Walt,<br><br>I am not quite sure why you are having so much trouble. What version of Access are you using? The .ocx file may be too old, or too new for the version you are using. If you continue to have trouble, try the Microsoft knowledge base, or try to contact Microsoft regarding the license issue.<br><br>=====<br><br>All,<br><br>Let me expound on the point of where to store the path. I am a big fan of NOT hard coding anything that has a possiblity of changing. So I either put my values in a table called tblSettings:<br><br>Key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Setting<br>-----------------------------------------------------------------------<br>ImportPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;C:\My Documents\&quot;<br>ExportPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;\\Prod1\Data\Access\Project1\&quot;<br><br>I then use a DLookUp in the code to get the value:&nbsp;&nbsp;.FileName = DLookUp(&quot;Setting&quot;, &quot;tblSettings&quot;, &quot;Key = 'ImportPath'&quot;). This allows me to change the path anytime I wish without having to adjust code. This is crucial for us, because every time we touch code, we have to re-compact, re-compile, make a new .mde, and roll it out. If it's in a table, I can change the value, and never worry about anything else.<br><br>The other option is to use an .ini File. This is just as effective. I use a function Called GetINI to return the value. This allows me to change the .ini file and never have to touch the database at all.<br><br><br>Hope this helps. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
I know this thread is old, but I am having the same problem as Walt (i.e. the licensing problem). I have no idea what the problem is, and can not seem to find an answer on the microsoft knowledge base. Does anyone know what the deal is? I have Access 2000. Is there something special needed to use this particular control? Thanks.
 
Instead of using an activex control, I always use this:


'***************** Code Start **************
'This code was originally written by Ken Getz.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
' Code courtesy of:
' Microsoft Access 95 How-To
' Ken Getz and Paul Litwin
' Waite Group Press, 1996

Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Declare Function aht_apiGetOpenFileName Lib &quot;comdlg32.dll&quot; _
Alias &quot;GetOpenFileNameA&quot; (OFN As tagOPENFILENAME) As Boolean

Declare Function aht_apiGetSaveFileName Lib &quot;comdlg32.dll&quot; _
Alias &quot;GetSaveFileNameA&quot; (OFN As tagOPENFILENAME) As Boolean

Declare Function CommDlgExtendedError Lib &quot;comdlg32.dll&quot; () As Long

Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
' You won't use these.
'Global Const ahtOFN_ENABLEHOOK = &H20
'Global Const ahtOFN_ENABLETEMPLATE = &H40
'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000


Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal Hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant

' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
'
' In:
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Out:
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(filter) Then filter = &quot;&quot;
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = &quot;&quot;
If IsMissing(FileName) Then FileName = &quot;&quot;
If IsMissing(DialogTitle) Then DialogTitle = &quot;&quot;
If IsMissing(Hwnd) Then Hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = True
' Allocate string space for the returned strings.
strFilename = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
With OFN
.lStructSize = Len(OFN)
.hwndOwner = Hwnd
.strFilter = filter
.nFilterIndex = FilterIndex
.strFile = strFilename
.nMaxFile = Len(strFilename)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
.strCustomFilter = &quot;&quot;
.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
End With


' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
Else
fResult = aht_apiGetSaveFileName(OFN)
End If

' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
Else
ahtCommonFileOpenSave = &quot;NoFile&quot;
End If
End Function


Function ahtAddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like &quot;Databases&quot;), a null character, the skeleton
' (like &quot;*.mdb;*.mda&quot;) and a final null character.

If IsMissing(varItem) Then varItem = &quot;*.*&quot;
ahtAddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
End Function


Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
Else
TrimNull = strItem
End If
End Function
'************** Code End *****************
Call it like this:


Function TestIt()
Dim strFilter As String
Dim lngFlags As Long
strFilter = ahtAddFilterItem(strFilter, &quot;Access Files (*.mda, *.mdb)&quot;, _
&quot;*.MDA;*.MDB&quot;)
strFilter = ahtAddFilterItem(strFilter, &quot;dBASE Files (*.dbf)&quot;, &quot;*.DBF&quot;)
strFilter = ahtAddFilterItem(strFilter, &quot;Text Files (*.txt)&quot;, &quot;*.TXT&quot;)
strFilter = ahtAddFilterItem(strFilter, &quot;All Files (*.*)&quot;, &quot;*.*&quot;)
MsgBox &quot;You selected: &quot; & ahtCommonFileOpenSave(InitialDir:=&quot;C:\&quot;, _
filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:=&quot;Hello! Open Me!&quot;)
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
End Function

HTH

Ben ----------------------------------
Ben O'Hara
bo104@westyorkshire.pnn.police.uk
----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top