Results 1 to 3 of 3
  1. #1
    bonecone is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    25

    Adding events at runtime

    I Loop through my controls with the following code



    Dim Ctrl As Control

    For Each Ctrl In Form_frmName
    Some Code Here
    Next Ctrl

    And I want the "Some Code Here" section to add a Got Focus event to each of my textboxes. I tried using the Add Handler method but it isn't recognized in Access 2007. I read about the CodeModule property, which allows you to just add code directly, line by line, but I can't figure out the proper syntax for using it in Access. Could only find instructions on using it in Excel.

    These are NOT dynamically generated textboxes, if that matters. Any help at all would be great at this point. Thanks!

  2. #2
    ChrisO is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2005
    Location
    Brisbane, Australia
    Posts
    27
    You did not say what you want to do in the GotFocus event but I will assume it will be the same for all Text Boxes.

    The following code will pass a pointer to each text box to a common got focus event handler when each text box receives the focus.

    Code:
    Private Sub Form_Open(ByRef intCancel As Integer)
        Dim ctl As Control
        
        For Each ctl In Me
            If ctl.ControlType = acTextBox Then
                ctl.OnGotFocus = "=HandleGotFocus([" & ctl.Name & "])"
            End If
        Next ctl
    
    End Sub
    
    
    Private Function HandleGotFocus(ByRef ctl As Control)
    
        MsgBox ctl.Name
    
    End Function
    Chris.

  3. #3
    bonecone is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    25
    Thanks Chris! That worked perfectly!

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

Similar Threads

  1. Following Environmental Events
    By seaape in forum Database Design
    Replies: 4
    Last Post: 06-23-2011, 06:17 PM
  2. Different Events
    By jo15765 in forum Programming
    Replies: 2
    Last Post: 12-07-2010, 12:45 PM
  3. Events Confusion?
    By homerj56 in forum Programming
    Replies: 5
    Last Post: 10-27-2010, 01:45 PM
  4. Form_Load Events
    By TheDeceived in forum Programming
    Replies: 1
    Last Post: 09-20-2010, 12:19 PM
  5. Timed events
    By summitrider in forum Access
    Replies: 2
    Last Post: 08-14-2008, 04:38 PM

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