MS Access Forum / General 2 / March 2007
Error message for Browse button
|
|
Thread rating:  |
Sue - 28 Mar 2007 02:09 GMT I'm having a similar problem. I keep getting the message Maria was getting with a different website's code -- "The expression On Click you entered as the event property setting produced the following error: Only comments may appear after End Sub, End Function, or End Property." Like Maria, I also copied Bill's code from http://community.compuserve.com/n/pfx/forum.aspx?nav=libraryMessages&tsn=1&tid=2 2415&webtag=ws-msdevapps in the BrowseForFileClass.txt document.
My textbox for the directory hyperlink is called Directory_Text_Box (so I replaced all instances of FormLetterCbo and txt.Link with Directory_Text_Box) and the button for Browse is called BrowseBtn. My code is like this:
'On Click Event Procedure for Browse button: Private Sub BrowseBtn_Click() ... End Sub
'On Click Event Procedure for Directory text box: Private Sub Directory_Text_Box_Click() ... End Sub
'Here's where there might be a problem. I wasn't sure where to put the End Sub: Private Sub BrowseForFileClass() Private Type OPENFILENAME ... End Sub
Public Function GetFileSpec() ... End Function
Public Property Let DialogTitle(strTitle As String) ... End Property
Public Property Let AdditionalTypes(str AddTypes As String) ... End Property
Public Property Let DefaultType(strType As String) ... End Function
Public Property Let InitialFile(strIFile As String) ... End Property
Public Property Let InitialDir(strIDir As String) ... End Property
Private Sub Class_Initialize() ... End Sub
Thanks for your help in advance!
 Signature Sue Programmer/Data Analyst Minnesota
Sue - 28 Mar 2007 02:55 GMT For those of you who can't read my mind, I meant to ask if anyone knew how to make the Browse button functional without getting the error message. :)
 Signature Sue Programmer/Data Analyst Minnesota
> I'm having a similar problem. I keep getting the message Maria was getting > with a different website's code -- "The expression On Click you entered as [quoted text clipped - 43 lines] > > Thanks for your help in advance! storrboy - 28 Mar 2007 03:39 GMT Hello Sue.
Well, I thought I'd managed to read your mind, but I realized have no idea where to start as you didn't post any actual code. If that is the extent of text in the module then it will never work. Your error message indicates that you have some line of text outside a proceedure that is not a comment ( preceeded by an apostrophy ' ). But nothing you've posted shows this. For starters, in a code window goto the Debug menu and choose Compile [project] and see what errors are displayed.
Sue - 28 Mar 2007 15:58 GMT Hi, storrboy.
Thanks for your help on this. I thought about posting all of the code but it is quite a bit and it's all listed in the BrowseForFileClass.txt document at http://community.compuserve.com/n/pfx/forum.aspx?nav=libraryMessages&tsn=1&tid=2 2415&webtag=ws-msdevapps. Here's all the code I have for BrowseForFileClass:
Private Sub BrowseForFileClass()
Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As Long nMaxCustrFilter 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 lCustrData As Long lpfnHook As Long lpTemplateName As Long End Type
'Here's where I get a compile error message. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
Private strDialogTitle As String Private intDefaultType As Integer Private strNewTypes As String Private strInitialFile As String Private strInitialDir As String Private strFilter As String Private strFltrLst As String Private strFltrCnt As String
End Sub
Should I put an End Function right before this last line of code? When I debugged my code, I got "Compile error: Only comments may appear after End Sub, End Function, or End Property" at the place in the code that starts like:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
This code comes right after this line of code: End Type. So I guess the compiler considers End Type to be like an End Property. I'm not sure what to do to fix the code. I'd appreciate you expertise in this matter. Thanks again.
 Signature Sue Programmer/Data Analyst Minnesota
> Hello Sue. > [quoted text clipped - 6 lines] > Debug menu and choose Compile [project] and see what errors are > displayed. storrboy - 28 Mar 2007 17:51 GMT > Hi, storrboy. > [quoted text clipped - 42 lines] > > End Sub Your function and probably the Type declarations are not supposed to be inside a sub. They must be at the module level. Common practise is to put them before any other code. I can't download that file from where I am but I would be surprised if that's straight from the file.
Basically the Private Sub BrowseForFileClass() and End Sub lines should not 'enclose' the rest of what you posted. There should be something else inside the sub. The variable declarations are likely alright where they are.
Sue - 28 Mar 2007 19:32 GMT Thanks. I see now that I needed to read the Readme document more carefully. I wasn't sure where BrowseForFileClass was supposed to be. I had never created a new class module before. Now I'm getting an error message that says "Compile error: Duplicate Option statement" at the top of the code in the BrowseForFileClass.txt document which start with:
Option Explicit Option Compare Database 'This is where the compile error message is.
Do you know how I would resolve this?
In the Readme document, it says "In your Access 97 database, ..." I don't have an Access 97 database so I'm hoping that won't matter. I have a Access 2003. It also says that the code displays the Open File dialog box and retrieves a file name and path. I don't need it to retrieve a file name, just a path and I'd like it to be a hyperlink when retrieved. I might need help with that later if I can't pinpoint where in the code I need to alter it.
 Signature Sue Programmer/Data Analyst Minnesota
> > Hi, storrboy. > > [quoted text clipped - 52 lines] > something else inside the sub. The variable declarations are likely > alright where they are. storrboy - 28 Mar 2007 20:09 GMT > Thanks. I see now that I needed to read the Readme document more carefully. > I wasn't sure where BrowseForFileClass was supposed to be. I had never [quoted text clipped - 6 lines] > > Do you know how I would resolve this? You must have more option statements in there somewhere. Do a File > Find > Current Module and see.
Sue - 28 Mar 2007 20:32 GMT Ok, I should have caught that myself. Sorry.
I used some of Ken Sheridan's code in the "Browser button" message he sent to Maria that creates an instance of the class and uses it. It's giving me a compile error message: "User-defined type not defined." Here's what it looks like:
Private Sub BrowseBtn_Click()
On Error GoTo Err_Handler Dim OpenDlg As New BrowseForFileClass 'Here's where I get an error message. Dim strPath As String
OpenDlg.InitialDir = "C:\" OpenDlg.DialogTitle = "Select File" strPath = OpenDlg.GetFileSpec Set OpenDlg = Nothing
If Len(strPath) > 0 Then Me.Directory_Text_Box = strPath End If Exit_Here: Exit Sub Err_Handler: MsgBox Err.Description, vbExclamation, "Error" Resume Exit_Here
End Sub
The directions given in Readme.txt says to provide a few lines of code that creates an instance of the class and uses it but I wasn't sure how to do that. The code given in Readme.txt for BrowserBtn_Click is for a combo box but I'm not using a combo box -- I'm using a text box instead so I thought it would be better to use Ken's code since Maria said she was using a text box too.
 Signature Sue Programmer/Data Analyst Minnesota
> > Thanks. I see now that I needed to read the Readme document more carefully. > > I wasn't sure where BrowseForFileClass was supposed to be. I had never [quoted text clipped - 9 lines] > You must have more option statements in there somewhere. Do a File > > Find > Current Module and see. storrboy - 28 Mar 2007 20:49 GMT And what is the name of the module where you placed all that other code? Judging by the example it should be named BrowseForFileClass. If not you'll need to change the line in the last posted code to reflect the actual module name.
Sue - 28 Mar 2007 21:48 GMT The name of the module is Class1. So I replaced BrowseForFileClass in my code with Class1 and now it's working! YAY!
Now about that issue about not retrieving a file name, just the path ... Looks like I'll need to change the code for the GetFileSpec function and the DialogTitle property. Right now, the DialogTitle is "Select File" and I'd like it to be "Select Directory". Here's the code for DialogTitle:
Public Property Let DialogTitle(strTitle As String) 'store the title for the dialog box strDialogTitle = strTitle End Property
I tried adding this code in DialogTitle but it didn't work:
strTitle = "Get Directory"
Here's the code for GetFileSpec:
Public Function GetFileSpec() Dim of As OPENFILENAME Dim intRet As Integer 'set up the file filter and the default type option If strNewTypes <> "" Then of.lpstrFilter = strNewTypes & strFilter of.nFilterIndex = 1 Else of.lpstrFilter = strFilter If intDefaultType <> 0 Then of.nFilterIndex = intDefaultType Else of.nFilterIndex = 1 End If End If 'define some other dialog options of.lpstrTitle = strDialogTitle of.lpstrInitialDir = strInitialDir of.lpstrFile = strInitialFile & String(512 - Len(strInitialFile), 0) of.nMaxFile = 511
' Initialize other parts of the structure of.hwndOwner = Application.hWndAccessApp of.hInstance = 0 of.lpstrCustomFilter = 0 of.nMaxCustrFilter = 0 of.lpfnHook = 0 of.lpTemplateName = 0 of.lCustrData = 0 of.lpstrFileTitle = String(512, 0) of.nMaxFileTitle = 511 of.lpstrDefExt = vbNullChar of.Flags = 0 of.lStructSize = Len(of)
'call the Open dialog routine intRet = GetOpenFileName(of) If intRet Then GetFileSpec = Left(of.lpstrFile, InStr(of.lpstrFile, vbNullChar) - 1) Else GetFileSpec = "" End If
End Function 'End of GetFileSpec
This will be the harder part. GetFileSpec calls GetOpenFileName so here's the code for that:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
Private strDialogTitle As String Private intDefaultType As Integer Private strNewTypes As String Private strInitialFile As String Private strInitialDir As String Private strFilter As String Private strFltrLst As String Private strFltrCnt As String
There's no End Function for GetOpenFileName. Is that because we're just declaring it? Here's the code for the OPENFILENAME type:
Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As Long nMaxCustrFilter 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 lCustrData As Long lpfnHook As Long lpTemplateName As Long End Type
Thanks again for your help!
 Signature Sue Programmer/Data Analyst Minnesota
storrboy - 29 Mar 2007 14:26 GMT Have tried looking at http://www.mvps.org/access/api/index.html The first two articles are the FileOpenSave and the BrowseForFolder, and I'm pretty sure I've used both with no problems.
Sue - 29 Mar 2007 18:02 GMT Ok, I'm using the hyperlink "Call Windows Browse for Folder Dialog box" code from your URL. I created another class module called Class2 and pasted the code into it (I hope that's what I was supposed to do):
'This code was originally written by Terry Kreft. '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 'Terry Kreft
Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _ "SHGetPathFromIDListA" (ByVal pidl As Long, _ ByVal pszPath As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _ "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _ As Long Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String Dim X As Long, bi As BROWSEINFO, dwIList As Long Dim szPath As String, wPos As Integer With bi .hOwner = hWndAccessApp .lpszTitle = szDialogTitle .ulFlags = BIF_RETURNONLYFSDIRS End With dwIList = SHBrowseForFolder(bi) szPath = Space$(512) X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath) If X Then wPos = InStr(szPath, Chr(0)) BrowseFolder = Left$(szPath, wPos - 1) Else BrowseFolder = vbNullString End If End Function
Then I pasted this code into the Form_Switchboard class object. The code was given as an illustration on how to call the BrowseFolder function:
Private Sub BrowseBtn_Click1()
Dim strFolderName As String strFolderName = BrowseFolder("What Folder you want to select?")
End Sub
Is there any part of the code from the "Call Windows File Open/Save Dialog box" hyperlink I should be pasting into my program as well? How do I "connect" the calling function (BrowseBtn_Click1) to Class2? In the previous code that I copied and pasted and that opened an Open File dialog window, it called Class1 with this code:
Private Sub BrowseBtn_Click()
On Error GoTo Err_Handler Dim OpenDlg As New Class1 Dim strPath As String
OpenDlg.InitialDir = "C:\" OpenDlg.DialogTitle = "Select File" strPath = OpenDlg.GetFileSpec Set OpenDlg = Nothing
If Len(strPath) > 0 Then Me.Director_Text_Box = strPath End If Exit_Here: Exit Sub Err_Handler: MsgBox Err.Description, vbExclamation, "Error" Resume Exit_Here
End Sub
I assume I should make the On Click Event Procedure for the Browse button be BrowseBtn_Click1, right? Thanks!!!
 Signature Sue Programmer/Data Analyst Minnesota
> Have tried looking at http://www.mvps.org/access/api/index.html > The first two articles are the FileOpenSave and the BrowseForFolder, > and I'm pretty sure I've used both with no problems. storrboy - 29 Mar 2007 18:32 GMT > Ok, I'm using the hyperlink "Call Windows Browse for Folder Dialog box" code > from your URL. I created another class module called Class2 and pasted the [quoted text clipped - 103 lines] > > The first two articles are the FileOpenSave and the BrowseForFolder, > > and I'm pretty sure I've used both with no problems. It dosen't need to be in a class module, just a standard one. When you use
strFolderName = BrowseFolder("What Folder you want to select?")
in say a button click, the function will be found and run.
Sue - 29 Mar 2007 21:12 GMT I'm getting a compile error in the code at the SHGetPathFromIDList declared function (see below) that says only comments may appear after End Sub, End Function, or End Property. "End Type" is the line of code right above it. Got any suggestions?
'This code was originally written by Terry Kreft. '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 'Terry Kreft
Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type
'Here's where the compile error is. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _ "SHGetPathFromIDListA" (ByVal pidl As Long, _ ByVal pszPath As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _ "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _ As Long Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String Dim X As Long, bi As BROWSEINFO, dwIList As Long Dim szPath As String, wPos As Integer With bi .hOwner = hWndAccessApp .lpszTitle = szDialogTitle .ulFlags = BIF_RETURNONLYFSDIRS End With dwIList = SHBrowseForFolder(bi) szPath = Space$(512) X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath) If X Then wPos = InStr(szPath, Chr(0)) BrowseFolder = Left$(szPath, wPos - 1) Else BrowseFolder = vbNullString End If End Function
 Signature Sue Programmer/Data Analyst Minnesota
storrboy - 30 Mar 2007 02:59 GMT It's hard to tell because of the line wraps. I don't see anything obviously wrong. Would it be easier if I just send you the module as a text file or bas so you can import it? Respond to my email if you want to.
Sue - 30 Mar 2007 15:40 GMT Would it be any different than the code at http://www.mvps.org/access/api/api0002.htm? If not, than I can't see how sending me the same thing in a .txt or .bas file would help me unless there's something I don't know about copying and pasting from a website. Maybe it has something to do with the fonts. I guess it couldn't hurt to give it a try. Thanks!
 Signature Sue Programmer/Data Analyst Minnesota
> It's hard to tell because of the line wraps. I don't see anything > obviously wrong. > Would it be easier if I just send you the module as a text file or bas > so you can import it? > Respond to my email if you want to. Douglas J. Steele - 30 Mar 2007 16:33 GMT It sounds as though you copied the code from "The Access Web" into an existing module.
Try putting it in a module all by itself.
 Signature Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!)
> Would it be any different than the code at > http://www.mvps.org/access/api/api0002.htm? If not, than I can't see how [quoted text clipped - 9 lines] >> so you can import it? >> Respond to my email if you want to. Sue - 30 Mar 2007 17:36 GMT Thanks, Doug. I had initially put the code in a separate module but I was told that it dosen't need to be in a class module, just a standard one. So I put the code in the Form_Switchboard Class Object. I had to modify the BrowseBtn_Click code Ken Sheridan gave to Maria in the previous posts with the subject "Browse button". I saved this code in the Form_Switchboard Class Object and now my browse button works! Yay!! Here's the modified code:
Private Sub BrowseBtn_Click()
On Error GoTo Err_Handler 'This next line of code gave me a compile error. I'm not sure why though 'because it worked for the BrowseForFileClass given on 'http://community.compuserve.com/n/pfx/forum.aspx?tsn=1&nav=libraryMessages&webta g=ws-msdevapps&tid=22415&redirCnt=1 'Dim OpenDlg As New Class1 Dim strPath As String
'The following 2 lines give me an "Object required" error when I click on the 'Browse button: 'OpenDlg.InitialDir = "C:\" 'OpenDlg.DialogTitle = "Select File" 'The following line of code was modified. It used to be 'strPath=OpenDlg.GetFileSpec: strPath = BrowseFolder("Select Directory") Set OpenDlg = Nothing
If Len(strPath) > 0 Then 'The next line was modified to have the name of my text box: Me.Directory_Text_Box = strPath End If Exit_Here: Exit Sub Err_Handler: MsgBox Err.Description, vbExclamation, "Error" Resume Exit_Here
End Sub
I also modified Ken's code for the text box's On Click Event Procedure to reflect the name of my text box:
Private Sub Directory_Text_Box_Click()
On Error GoTo Err_Handler FollowHyperlink Me.Directory_Text_Box, , True
Exit_Here: Exit Sub Err_Handler: MsgBox "Unable to open file.", vbExclamation, "Error" Resume Exit_Here End Sub
Thank you too, storrboy! Couldn't have made it this far without you.
 Signature Sue Programmer/Data Analyst Minnesota
> It sounds as though you copied the code from "The Access Web" into an > existing module. [quoted text clipped - 14 lines] > >> so you can import it? > >> Respond to my email if you want to. Douglas J. Steele - 30 Mar 2007 17:43 GMT You could just as easily created a new module (not a class module), and put it there.
That might actually be better, since you'd then be able to use it from elsewhere in the application: as you've got it, you can only use it in the Switchboard form.
 Signature Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!)
> Thanks, Doug. I had initially put the code in a separate module but I was > told that it dosen't need to be in a class module, just a standard one. [quoted text clipped - 82 lines] >> >> so you can import it? >> >> Respond to my email if you want to.
|
|
|