Button action on double select (3 Viewers)

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
I've been trying to do this for hours; can't find a solution.

I've got a continuous form, with a button (cmd1) on each record.
Each record has a unique primary key, RecordID.
cmd1 is clicked to open a form (frm1) based on the RecordID using the following code:
Code:
DoCmd.OpenForm "frm1", , , "RecordID = " & Me.RecordID

I want frm1 to open, ONLY when a user clicks on cmd1 while it's already in focus ON that specific record.

Any suggestions?
 

MarkK

bit cruncher
Local time
Yesterday, 21:41
Joined
Mar 17, 2004
Messages
8,186
Check out the Screen object, which exposes a PreviousControl property. Check if the Screen.PreviousControl is the same object as Form.ActiveControl.
Code:
If Screen.PreviousControl Is Me.ActiveControl Then
[COLOR="Green"]   'open your form here[/COLOR]
End If
. . . or some variation of this approach.
 

bob fitz

AWF VIP
Local time
Today, 05:41
Joined
May 23, 2011
Messages
4,727
I've been trying to do this for hours; can't find a solution.

I've got a continuous form, with a button (cmd1) on each record.
Each record has a unique primary key, RecordID.
cmd1 is clicked to open a form (frm1) based on the RecordID using the following code:
Code:
DoCmd.OpenForm "frm1", , , "RecordID = " & Me.RecordID

I want frm1 to open, ONLY when a user clicks on cmd1 while it's already in focus ON that specific record.

Any suggestions?
Can you confirm that the data type of "RecordID" in the table is numeric and not text.
 

bob fitz

AWF VIP
Local time
Today, 05:41
Joined
May 23, 2011
Messages
4,727
I've just been reviewing this thread and have just noticed its title: "Button action on double select".
Not sure what you mean by "double select" but if your code is in the "Double Click" event of the button and you have code in the "On Click" event I don't think your code will ever run.
 

MarkK

bit cruncher
Local time
Yesterday, 21:41
Joined
Mar 17, 2004
Messages
8,186
Bob, I assumed the "double select" was that the record has to be current first, and only then will the button perform the action, so not a double click.
 

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
I want frm1 to open, ONLY when a user clicks on cmd1 while it's already in focus ON that specific record.
The way I understand folks is that Zak14 wants to open frm1 if it's on record 1, or frm2 if he's on record 2, and so on.

If this is the case, then we need to understand more with regards why this setup. Normally you would use the code you already have to open one form and filter it down based on an ID field.
 

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
But MarkK you might be getting the right end of the stick on this one. Perhaps Zak14 doesn't realise that once a button is clicked, the focus shifts to the record before the Click event is fired.
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
Hi guys. Thanks for the replies.

-

I've just been reviewing this thread and have just noticed its title: "Button action on double select".
Not sure what you mean by "double select" but if your code is in the "Double Click" event of the button and you have code in the "On Click" event I don't think your code will ever run.

I realised the title might've caused some confusion. I don't mean double click. What I mean to say is, a click on an active control, whether the click be straight away or a minute later.
And yes, the data type is numeric.

-

The way I understand folks is that Zak14 wants to open frm1 if it's on record 1, or frm2 if he's on record 2, and so on.

If this is the case, then we need to understand more with regards why this setup. Normally you would use the code you already have to open one form and filter it down based on an ID field.

Only frm1 opens upon clicking any record and get's filtered, based on the RecordID, as in the following code:
Code:
DoCmd.OpenForm "frm1", , , "RecordID = " & Me.RecordID

-

Check out the Screen object, which exposes a PreviousControl property. Check if the Screen.PreviousControl is the same object as Form.ActiveControl.
Code:
If Screen.PreviousControl Is Me.ActiveControl Then
[COLOR="Green"]   'open your form here[/COLOR]
End If
. . . or some variation of this approach.

I tried this method, but I've got a continuous form. By this approach, if a user selects cmd1 on one record, then proceeds to select cmd1 on another record, frm1 opens.
 
Last edited:

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
I feel I haven't explained it well enough; I tried to simplify it for ease of understanding, but here's the exact scenario:

See the attached image.

The button is transparent and overlays the width of the individual records, as seen selected on the form (dotted box)
Currently, when you click on one of the records (transparent button), the profile of that individual opens on another form.
My objective is, to make the profile open upon clicking as it does now, but only if the record is already selected. Whether the record is selected (and has got focus) by clicking on it, or by other means - like using the keyboard, the subsequent click on that record should open the profile.

When I was attempting a solution, one idea comprised of using an unbound textbox (that you see on the footer on the right). It updates to show the ID of a selected record when it's transparent button gains focus. I used this textbox to determine if the previously selected record is the same as the current selected record, but I couldn't figure out a way to implement this effectively - only because the gotfocus event happens before the click event.

-
 

Attachments

  • Capture.jpg
    Capture.jpg
    83.4 KB · Views: 68

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
In such circumstances one would use a double click.
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
In such circumstances one would use a double click.

A double click isn't my desired effect. The reason for this is, the user should be able to select a record and see its ID number in the footer. The main reason, however, is that I plan to add another feature after implementing this feature, which depends on it.
 

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
* Create a global boolean variable that's only in scope within the form
* Set this variable to false in the Current event
* Perform a True check in the button's Click event and if it passes, perform your OpenForm operation. Then just after the END IF set the variable to True.
* If you can select records using the arrow keys, you'll need to incorporate it
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
* Perform a True check in the button's Click event and if it passes, perform your OpenForm operation. Then just after the END IF set the variable to True.

How to do that is what I've been trying to figure out. How would I check this?

Something like:

If ButtonOfCurrentSelectedRecord = ButtonOfPreviousSelectedRecord Then ...

EDIT: I just understood what you're saying, after I realised the current event happens every time you switch focus to a different record. Let me try it.
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
EDIT: I just understood what you're saying, after I realised the current event happens every time you switch focus to a different record. Let me try it.
How's this going? The True test should be performed on the global variable mentioned in step 1.
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
How's this going? The True test should be performed on the global variable mentioned in step 1.

I'm still trying to figure out a way, but I always come back to the same issue;
the GotFocus event happens before the Click event. It would be easy if it was the opposite.

Code:
Dim b as Boolean

Private Sub Form_Current()
    b = False
End Sub

Private Sub cmdSelectRecord_Click()
    If b = True Then
        ' Open form
    End If
End Sub

Private Sub cmdSelectRecord_GotFocus()
    b = True
End Sub
If the above were the order of events, clicking on the button would have no effect, unless it has got already focus,
but because the GotFocus happens first, clicking on the button gives it focus before the click event is fired, rendering the boolean variable useless.
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
Something like this:
Code:
Dim b as Boolean

Private Sub Form_Current()
    b = False
End Sub

Private Sub cmdSelectRecord_Click()
    If b = True Then
        ' Open form
    End If
[COLOR="blue"]    b = True[/COLOR]
End Sub

Private Sub [COLOR="Blue"]Form_Load[/COLOR]()
    b = True
End Sub
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
Something like this:
Code:
Dim b as Boolean

Private Sub Form_Current()
    b = False
End Sub

Private Sub cmdSelectRecord_Click()
    If b = True Then
        ' Open form
    End If
[COLOR="blue"]    b = True[/COLOR]
End Sub

Private Sub [COLOR="Blue"]Form_Load[/COLOR]()
    b = True
End Sub

Wow, I checked this thread a few times after you posted this, not realising there was a second page.

Nevertheless, thanks for your suggestion. I tried the code, but it doesn't account for a record that is already selected, as in, the user must click on the record twice, whether it's already in focus or not.

Sorry, I sound spoilt, but it seems inconsistent and I need this for a feature that I was asked to implement, albeit not necessary.

Is there no way to perhaps get the Click event to happen first; maybe not directly, but by playing around with functions and variables?

P.S. I'm curious how having the boolean set to true upon load affects the function.
 

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
Your button is an overlay so the only clickable area is your button, no where else. You have most likely added/removed code that is different to what I wrote. If there are other clickable areas in the detail section of your form, then you need to mention this.

I've just tested my theory and it works as expected. The only time you'll need to do more is when the user uses the arrow keys to go up and down.
 

Zak14

Registered User.
Local time
Today, 05:41
Joined
Jun 27, 2014
Messages
166
Exactly. The arrow key selection is what I meant. If the only solution means having to detect (arrow) key presses, I'd rather not carry on with this feature, as I've heard its unreliable.
 

vbaInet

AWF VIP
Local time
Today, 05:41
Joined
Jan 22, 2010
Messages
26,374
Not sure where you heard that, but there's nothing unreliable about detecting key presses. Perhaps you're thinking about SendKeys.

Here's how it should look:
Code:
Private blAllowClick As Boolean
Private blIsLoading  As Boolean

Private Sub Command17_Click()
    If blAllowClick Then MsgBox "click"
    blAllowClick = True
End Sub

Private Sub Form_Current()
    blAllowClick = blIsLoading
    blIsLoading = False
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    ' Wrap the below in a test for a Left, Right, Up and Down KeyCode
    blAllowClick = True
End Sub

Private Sub Form_Load()
    blIsLoading = True
End Sub
... also think about what would happen when the current record is in the blank/new record row.
 

Users who are viewing this thread

Top Bottom