[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Determine which control has focus/active/clicked

Posted on 2011-02-17
4
Medium Priority
?
521 Views
Last Modified: 2013-11-28
I have a continuous form that has 48 text boxes (2 per hour) and a total of 4 rows. I want to know how to determine which of those boxes was clicked. When you click the box it opens up a popup form with a combo box. The value of the combo box is then passed back to the box that was clicked.

Maybe you need to look at it to see what is going on. It works if I program each and every textbox, but I am trying to 'automate' it a bit better.

Thanks for taking a look. MoldChanges.accdb

P.S. I have tried something like this on a few form events: Dirty, After_Update, Current. None of those worked.

Dim strActiveCtl As String

strActiveCtl = Screen.ActiveControl.Name

MsgBox strActiveCtl

And I may need to know how to do something like Forms!FormName! & Variable.  If that is possible.
0
Comment
Question by:G Scott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Author Comment

by:G Scott
ID: 34921739
P.S. I have tried something like this on a few form events: Dirty, After_Update, Current. None of those worked.

Dim strActiveCtl As String

strActiveCtl = Screen.ActiveControl.Name

MsgBox strActiveCtl
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 750 total points
ID: 34921968
If you MUST know which control is active, you'll have to include code in the GotFocus or Enter event of every control to set your module level variable. Screen.ActiveControl will tell you which is active, but that may or may not be what you expect. The only way to be sure is to code in the control events - not the form event.
0
 
LVL 14

Assisted Solution

by:pteranodon72
pteranodon72 earned 750 total points
ID: 34929214
If you want all 48 boxes to behave the same way on a click, you need to put similar code in the event of all 48 boxes. VB classic allowed control arrays on a form, but that functionality is not available in Access forms. You must write separate code.

You can reduce the burden of writing 48 routines (and especially modifying all 48 routines) by having them all call the same function (must be a function, not a sub) and pass their own control name.

'funtion within form's module:
Public Function ReactToClick(strCaller As String)
Me(strCaller) = InputBox("What do you want to put in box " & strCaller)
End Function

Then, select all 48 textboxes, open the property window, find the space for On Click on the Events tab, and type:

=ReactToClick("")

Next (and this is what you dreaded), select the textboxes one-at-a-time and insert the name of the textbox or name of the bound field between the double quotes. This makes every event able to reach the control through the Me(controlName) construct.

Unfortunately, VBA does not have a construction like "this" in JavaScript or Java. The closest you get is Me, which points to the current form or report.

Hope this helps,

pT72
0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 34931094
Thanks. You guys pointed me in the right direction. I used a bit of:

Public Function controlName(thecontrol As String)
Dim currentControl As String
Dim currentForm As String
currentControl = Screen.ActiveControl.Name
currentForm = Screen.ActiveControl.Parent.Name
DoCmd.OpenForm "Form1"
Forms!Form1!txtControlName = currentControl
Forms!Form1!txtCurrentForm = currentForm
Forms!Form1!txtRow = Forms!Form2(currentForm)!Row


Where I would call that fuction by, like you said pteranodon72, putting the control name on each and every call. Tedious, but it worked. Only to find out it really isn't a valid solution after all. Guess I need to really think it all the way through.

Thanks for the help. I learned something new. :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question