Solved

Determine which control has focus/active/clicked

Posted on 2011-02-17
4
465 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
  • 2
4 Comments
 
LVL 1

Author Comment

by:G Scott
Comment Utility
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 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now