views:

55

answers:

2

I'm new to Access VBA development and being asked to debug and add features to an Access 2007 application that two previous developers worked on.

A form displays records from a database and shows a button for each record. The button is supposed to open a file using the appropriate path. But when the user clicks the button, it always uses the filepath from the first record that the form displays, instead of the filepath from the correct record.

The code looks like it is trying to use a bookmark to open the correct file, but as stated above, that isn't working. Here is the relevant code from the button click event. When I try to Debug.Print form.Bookmark to the immediate window, it just displays a question mark.

Dim rs As Recordset
Set rs = form.RecordsetClone
rs.Bookmark = form.Bookmark

Edit: adding more code per @Remou's request. When button is clicked:

Private Sub OpenFile_Click()
    Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub

Which calls:

Public Sub Subform_cmdOpenFile_Click(frm As Form)
Set rs = frm.RecordsetClone
rs.Bookmark = frm.Bookmark

And then it goes on to open the file.

+3  A: 

If the button is for each record, there is no need for any messing around with the recordset. You can use the name of the control to get the file:

TheFile=Me.MyControl

It seems that you have both a form and subform. I am guessing from your answers that the set-up is something like this:

   |------------------------------|
   |  Main Form                   |
   --------------------------------
    Sub form
   --------------------------------
    Row                     Button
   --------------------------------
    Row                     Button
   --------------------------------

If the name of the button is OpenFile, try:

Private Sub OpenFile_Click()
    MsgBox Me.NameOfAContolHere & ""
    'Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub

This can then be used to ope a file like so:

Private Sub OpenFile_Click()
    FollowHyperlink Me.NameOfControlWithPathToFile
    'Form_FilingProcess.Subform_cmdOpenFile_Click Me
End Sub
Remou
The button is not tied to the record or the file. It just triggers the On Click event, which runs a method that opens the file from the path in a record. Is there a way for me to find out which button was clicked?
LCountee
Does your form in form view consist of a number of rows with a button on each row?
Remou
Yes, that's right.
LCountee
In that case, there is no need to know which button was clicked, there is only one button, and that is the button on the current row. You can show this by adding a message box to the click event: `MsgBox Me.SomeControlName`
Remou
I think would save a lot of trouble trying to use a bookmark.
Jeff O
@Remou Sorry, I don't understand your last comment. Can you be more specific, show more code, or give me a link please? I am really trying here - this seems so simple but I can't get it to work.
LCountee
Please post the click event code and I can show you where the message box would fit in.
Remou
Please see additional code in OP above.
LCountee
Is that clearer?
Remou
The file path is in the database row associated with the subform row. The file path is not actually stored in a control. So I don't understand the point of referring to the control?
LCountee
What is in the rows and what is the purpose of having a button associated with each row if the row is not relevant?
Remou
You understand that the bookmark is just a way of moving to a record and that recordsetclone is simply a copy of the recordset contained in the subform? The trend of my comments is to move away from this complication and to refer to the subform itself, rather than a copy.
Remou
Let me second @Remou here. I see no reason whatsoever for mucking about with bookmarks. When you have a command button on a continuous form and click it, the record that button is on is the current record, so you don't need to move any recordset pointer at all -- you're on the record that it applies to already. You can confirm this by putting MsgBox() in the subform's OnCurrent event if you don't believe it. Also, I recommend NOT using bookmarks with anything but the RecordsetClone and the form -- mucking about with the form's Recordset seems unpredictable to me.
David-W-Fenton
Thank you so much for your help and patience.
LCountee
A: 

Here is what your sub should look like:

Public Sub Subform_cmdOpenFile_Click(frm As Form) 
  Dim CurrentBookmark as String
  Set rs = frm.RecordsetClone 
  CurrentBookmark = frm.Bookmark
  rs.Bookmark = CurrentBookmark
End Sub

Set a string variable to the value of the form's bookmark. Then set the recordset's bookmark to the string variable value.

Dim rs As Recordset 
dim CurrentBookmark as String

Set rs = Me.RecordsetClone 

CurrentBookmark = Me.Bookmark
rs.Bookmark = CurrentBookmark 

http://msdn.microsoft.com/en-us/library/aa223967(office.11).aspx

Jeff O
What is the reason for doing this?
Remou
@Jeff O - form.Bookmark has a value of "?" and Me.Bookmark returns an error: "You have entered an expression that has an invalid reference to the property Bookmark."
LCountee
Because Microsoft said so ;)
Jeff O
@LCountee - check the link I included in my answer.
Jeff O
@Jeff O - I've seen that link/info before but it doesn't address the specific problem I'm having.
LCountee
I am afraid these bookmarks seem to serve no purpose whatsoever in the current situation.
Remou
Also, the storing of a bookmark is always a red flag of something wrong. I also believe that MS's documentation recommends that if you *must* store the bookmark, use a variant, not a string. It may very well end up as a variant of subtype string, but that's what I recall MS recommending. I don't recall well, because I don't believe there is 1 in a 1000 situations where one would use a bookmark where one should store it in as variable.
David-W-Fenton