Results 1 to 5 of 5
  1. #1
    Williams485 is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    42

    Detect which control is active

    Hello Access Forum,



    I'm trying to find the most effective and efficient way to create an event handler that will detect when certain controls on a form are selected (and then carry out some actions). I've read a couple of methods that I either can't get to work or don't understand. The latter is to use class modules to handle the form events. While I've used classes in an Excel application and have some understanding, I haven't been able to follow the articles relating to Access forms.

    So, the other option I've seen, which seems like the simpler option, is to use the ActiveControl property. The code I'm trying is:
    Code:
    Private Sub Form_SelectionChange()
        
        Dim ctlCurrentControl As Control
        
        MsgBox "selection changed"
        
        Set ctlCurrentControl = Screen.ActiveControl
        If ctlCurrentControl.Name = "field1" Then
            MsgBox "field1"
        ElseIf ctlCurrentControl.Name = "field2" Then
            MsgBox "field2"
        End If
        
    End Sub
    However, I can't get this to work. The form's 'On Selection Change' property (in the Property Sheet) is set to "[Event Procedure]".

    Please can you advise on whether the SelectionChange event is the best way and why it isn't working for me in the test form?

    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    put a modular flag at the top of the form code.
    set it to whatever control is picked, then you can recall it anytime.

    Code:
    private mvActiveControl
    
    public btnSave_OnClick()
        mvActiveControl = "btnSave"
         '...more button events
    end sub

  3. #3
    Williams485 is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    42
    Thanks for the reply, Ranman. Am I right in thinking that I'd have to write a short event handler for each control that I want to monitor and set it to a variable (as you've demonstrated with the OnClick event)? If so, I have a large number of controls that I want to use so I was wondering if there was a way to do this without writing code for each control that it affects. The controls I want to detect selection for are text boxes or check boxes and they may not necessarily be clicked (tabbed to, for example).

    Are there any other ways I could achieve this or do I need to write the event handler for each control?

    Thanks

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Code:
    Are there any other ways I could achieve this or do I need to write the event handler for each control?
    put your sub into a new module, change it to a function and make it public. e.g.

    Code:
    Public Function SelectionChange()
        
        Dim ctlCurrentControl As Control
        
        MsgBox "selection changed"
        
        Set ctlCurrentControl = Screen.ActiveControl
        If ctlCurrentControl.Name = "field1" Then
            MsgBox "field1"
        ElseIf ctlCurrentControl.Name = "field2" Then
            MsgBox "field2"
        End If
        
    End Sub
    then on each of the controls click event replace [Event Procedure] with =SelectionChange()

    if you have many controls, you can just select them all and replace all in one go

    I'm not sure why you are using the SelectionChange event - it relates to pivot tables and charts but you are talking about controls

  5. #5
    Williams485 is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Feb 2013
    Posts
    42
    Thanks, Ajax. I clearly hadn't researched the SelectionChange event properly! Your suggestion works fine and I can now go on to add the functionality I need.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Active Control on Image
    By LonghronJ in forum Modules
    Replies: 12
    Last Post: 07-15-2015, 03:57 PM
  2. Replies: 12
    Last Post: 10-01-2014, 08:17 PM
  3. Active X control comes grayed out
    By jj1 in forum Access
    Replies: 1
    Last Post: 05-29-2014, 09:13 AM
  4. Active X - Web Browser control doesn't work
    By forstatd in forum Reports
    Replies: 1
    Last Post: 06-02-2010, 10:56 PM
  5. Active X form control
    By amitsingha4u in forum Access
    Replies: 2
    Last Post: 05-18-2010, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums