hidden button is the active control

ClaraBarton

Registered User.
Local time
Today, 10:48
Joined
Oct 14, 2019
Messages
676
I have a hidden button that calls a calculator (with a shortcut key). There are several fields on one form that use this button so I need to know the field name that's calling the button.
Me.ActiveControl.Name returns the hidden button.
How do I get the name of the field that is actually calling the hidden button (calculator)?
This is important for using the amount in the field and for returning the calculator results to this same field.
 
maybe try this.
Add a variable at top of form

private LastControlEntered.

Then build a function
Code:
Private Function LastEntered()
  Set LastControlEntered = Me.activeControl
end Function

In the enter property of the controls that can use the calculator
=LastEntered()

Because I assume the shortcut key moves the active control to the button, so you need to know where it was previously.
 
Last edited:
Thank you! I also discovered there's a PreviousControl.
Screen.PreviousControl.Name. Discovered it (thought of it, whatever) after I'd posted.
 
It's good that you found that because there seems to be some consensus among articles I've searched that say that the control to which you set focus (VBA or via key or mouse action) cannot be hidden. "Hidden" and "Active Control" are not compatible.

There is a general principle in programming (not limited to Access and VBA) that when you want to know where you were when something happened, you need to program a trace feature. If you have a limited number of places where this putative transfer occurs, just store a name or code in a slot for each stretch of code that could trigger this event. I would be careful, though, if you have more than one form open at the same time, because in that case the previous control might be a little bit ambiguous.
 
I have a hidden button that calls a calculator (with a shortcut key). There are several fields on one form that use this button so I need to know the field name that's calling the button.
Me.ActiveControl.Name returns the hidden button.
How do I get the name of the field that is actually calling the hidden button (calculator)?
This is important for using the amount in the field and for returning the calculator results to this same field.
Why can't you call the code the button calls directly from those fields?
 
OK. Point taken.
I don't want to call from the field because I use the keyboard and not the mouse. Double click moves to another field, single click the same... Is there a way to use shortcut keys? Open for suggestions here.

Here's where I am:
Code:
Private Sub btnCalc_Click()
Dim rtn As Variant
Dim strControlName As String
    If Forms!frmCheck.ActiveControl.Name = "Category" Then ' Check if focus is within the subform control
        strControlName = Screen.PreviousControl.Name ' Get the name of the control within the subform that has the focus
    End If

  Me.Dirty = False
  rtn = getValueFromPopUp("frmCalculator", "lblReadOut", Nz(strControlName, 0))
  If Not IsNull(rtn) Then strControlName = rtn
End Sub
And this is what I get: Name of the field... not the amount
1748782655039.png
 
? screen.PreviousControl.Name
txtFactor
? screen.PreviousControl
7

1748785451400.png
 
Double click moves to another field, single click the same...
why would you change the standard meaning of keys? Tab is the normal key to progress from one field to the next. Then you could use the dbl-click to call up the calculator and since you would be in the field where the calculator should place the results, you can pass that along to the calculator.
 
Oh if only I would think before speaking! The answer is
Code:
strControlName = Screen.PreviousControl.VALUE!!!

Thanks for your replies!
@Pat Hartman The tab key is right above the Capslock key. Very nasty.
 
I thought I showed that you do not need the value property, as it is the default property? :(
 
It's good that you found that because there seems to be some consensus among articles I've searched that say that the control to which you set focus (VBA or via key or mouse action) cannot be hidden. "Hidden" and "Active Control" are not compatible.

There is a general principle in programming (not limited to Access and VBA) that when you want to know where you were when something happened, you need to program a trace feature. If you have a limited number of places where this putative transfer occurs, just store a name or code in a slot for each stretch of code that could trigger this event. I would be careful, though, if you have more than one form open at the same time, because in that case the previous control might be a little bit ambiguous.
I agree with this. I'm sure I've seen error messages like "you can't set the focus to a hidden control".
 
Technically it is the visible property- a control with visible set to yes but hidden behind another control can still receive the focus, as can a control with height and width set to 0
 
Transparent buttons are still clickable I think.
Correct. That can be useful or a nuisance depending on what you are trying to do.
I use transparent buttons for many purposes. For example, see

@ClaraBarton
One way of getting the name of any control, including hidden or transparent controls, is to use one of the Accessibility functions: AccHitTest or AccLocation. I have used these in several apps, For example, see

If you want to investigate either of these approaches, I can give more detail if you need it
 
The bottom line - it is dangerous to rely on any of the active.control methods. Use one of the other suggested techniques.
 
I don't need to know the clickable button's size or location. I need to know the name of the field I'm occupying when I shortcut key the transparent button. It could be one of two options on each subform. The previous control works but it's dangerous? Not dependable?
 
you can also create a Public Function to save the current Control, on a Module:
Code:
Public gblCurrentCtl As Control

Public Function fnGetCurrentCtl()
Set gblCurrentCtl = Screen.ActiveControl
End Function

on the Load Event of your form, Reset the gblCurrentCtl variable
Code:
Private Sub Form_Load()
Set gblCurrentCtl = Nothing
End Sub

on design view of your form, add code to the Enter Event of your control
you need to save (On Enter Event):
Code:
=fnGetCurrentCtl()

Code:
Private Sub btnCalc_Click()
Dim rtn As Variant
        If Not (gblCurrentCtl Is Nothing) Then
        strControlName = gblCurrentCtl.Name ' Get the name of the control within the subform that has the focus
    End If

  Me.Dirty = False
  rtn = getValueFromPopUp("frmCalculator", "lblReadOut", Nz(gblCurrentCtl, 0))
  If Not IsNull(rtn) Then gblCurrentCtl = rtn
End Sub
 

Users who are viewing this thread

Back
Top Bottom