0
votes

In ms access, I have a mainform with 2 subforms. SubformA is a continuous form with record title and a transparent button overlay for each record. SubformB contains the detail of the selected record (this is on tabcontrol elsewhere within the mainform, mimicking a popup overlay).

What I want to happen is:
1. Clicking a record subformA displays subformB and filters it to that record.
2. Moving through records on subformA updates so that subformB always displays the corresponding record.
3. Clicking a record on subformA hides subformB again ONLY IF it is the record that is currently being displayed.

However I cannot work out how I can test for if the current record is the one that was clicked because as soon as I click the button, if it is not the current record, the current event fires first, making it the current record, and hence always hiding subformB.

I tried using a variable to check at the start and end of the current event if the id on subformB stays the same. However, the current event of course does not trigger in the situations where the record has not changed, which is when I need to test this variable.

Stripped down/pseudocode in subformA for what I have tried to do:

Sub Form_Current
  If subformB.Visible Then set filter on subformB to ID = subfromA.ID
End Sub

Sub cmdButton_Click
  If subformB.Visible Then
    If subformB.ID = subformA.ID Then 
      Hide subformB
    Else
      'code never gets to here because the condition is always true 
      'do nothing - current event has already set the filter
    End If
  Else
    Unhide subformB
    Set filter to ID = subformA.ID
  End If
End Sub

Is anybody able to help me here? Thanks.

EDIT: what I am asking is how can I test the current record ID against the record ID of the record where the command button was clicked - but the record ID when clicking the button is of course just taking the new current record ID. Therefore it is comparing old current record ID to new current record ID, the problem being how do I store the old ID but prevent it from being updated again before the condition is tested.

3

3 Answers

0
votes

Set a module level private field and set its value to the value:

a) of the selected ID if new.
b) zero if same ID supplied (clicking the button twice).

Private mSelectedId as Long

Sub cmdButton_Click
    If Me!ID = mSelectedId  Then
        mSelectedId = 0
    Else
        mSelectedId = Me!ID
    End If

    FilterSubFormB
End Sub

You could expand this to filter subformB:

Private Sub FilterSubFormB()
    With Me.Parent.SubformB.Form
        .Filter = "ID=" & mSelectedId 
        .FilterOn = True
    End With
End Sub
0
votes

An alternative to checking the ID that might work for what you're doing is to: 1. Add a hidden, unbound textbox to your form. 2. Use the click event to stash the ID in the textbox, after the check takes place. 3. Instead of checking the ID in the click event code, check the value saved in the textbox. Untested, but something like this.

    Sub cmdButton_Click
    If subformB.Visible Then
         If subformA.ID = HiddenTextBox Then 
            Hide subformB
         Else

         End If
     End If
     Else
         Unhide subformB
         Set filter to ID = subformA.ID
     End If
     HiddenTextBox = subformA.ID
    End Sub
0
votes

You should not have to filter on the other subform, just make the record the current record.

This article will show you how - it may also be a bit more intuitive for the user:

Synchronizing Multiple Subforms in Access