Results 1 to 12 of 12
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Passing a control to a sub

    I need to have a sub deal with changes made to text strings in text box controls, in this case a sub named "TestForChange". There will be quite a number of "GotFocus"/"LostFocus" pairs associated with the current form. Corresponding to the text boxes are boolean fields where a flag is set to denote that a field has changed. The boolean controls have consistent naming with their text box counterparts. E.g., the tbFirstName counterpart is "ChgFirstName" so the "TestForChange" sub needs to parse the control name in order to produce the counterpart name and hense the need to pass the control name. The code below IS WRONG in that it passes the text as opposed to the control that contains it.



    What do I need to do to fix this?

    Thanks,
    Bill

    Code:
    Private Sub tbFirstName_GotFocus()
    strAtFocus = Me.tbFirstName.Text
    End Sub
    
    Private Sub tbFirstName_LostFocus()
    Call TestForChange(tbFirstName)
    End Sub
    
    Private Sub TestForChange(CtrlName As Control)
    If Me(CtrlName).Text <> strAtFocus Then _
        MsgBox "Strings not equal"
    End Sub

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I understand now how to get the control name from the reference you gave. Having looked at that, it makes no sense to have a special "TestForChange" sub, but rather just attend to the control content in the LostFocus event.

    Thanks,
    Bill

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The AfterUpdate event might be a better place for your test. Did you know each control has an OldValue property?

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I did not know that........... this caper is getting simpler by the minute. I will test for changes in the AfterUpdate event and proceed accordingly.
    Thanks,
    Bill

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    We aim to please.

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    As long as you're being so pleasing, why does a text box become blank when one presses the "Enter" key after entering or changing text?

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    It doesn't unless you have some code running elsewhere. It would if all of the "text" were highlighted (selected).

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Hummmmmm! If I click on a text box that contains the text "MyText", change the text to "MyTextTest" and press the "Enter" key the text box becomes blank until I move the focus elsewhere on the form.

    In another case where there is an AfterUpdate event coded, the AfterUpdate code you see below doesn't run until I move the focus.

    Code:
    Private Sub tbFirstName_AfterUpdate()
    
        If Me.tbFirstName.Text <> Me.tbFirstName.OldValue Then
            Me.ChgFirstName = True
            Me.Refresh
            Me.tbPCCode.SetFocus
            Call tbPCCode_Click
        End If
        
    End Sub

  10. #10
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    After Update does exactly what it says on the tin, it fires once you have left the control and only if the data has changed.

    The BeforeUpdate form event is most normally used as it will fire before any record update is saved allowing you to evaluate the changes made before committing them.
    If you simply want to check for a change in data you can also use the Me.Dirty property - this will be true if any data on the record has changed.

    In the case you described above, what control is next on the tab order after your text box? It will get focus, and if it has a OnGotFocus event that will fire.
    OnGotFocus and OnLostFocus events are generally misused, as they do fire if the control gets focus through VBA, unlike all other events which don't.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    In the case you described above, what control is next on the tab order after your text box? It will get focus,
    Your comment rattled some memory from way back. Many moons ago, I had a form that had a few text boxes that "behaved" strangely when the "Enter Key" was pressed. It was then that I learned that one can control the "Enter Key Behavior" on a control-by-control basis. Upon investigation this morning, I found that 5 of the 16 text boxes on my form had the behavior setting to "new line". One of the 5 was the test case I'd been using to test some condition handling pertinent to the App. I have no idea as to the mix I observed, as I didn't use the wizard in creating the form but rather text box by text box in design mode. I don't normally copy text boxes as that's a good way to entirely corrupt a form and/or the database itself............ I speak from experience on that point. So, having adjusted the behavior to "Default", the AfterUpdate event will now fire when appropriate and I can move forward, hopefully without any further issues.

    Thanks for the wake-up,
    Bill

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Thanks for the update.

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

Similar Threads

  1. Replies: 3
    Last Post: 11-30-2016, 07:18 PM
  2. Replies: 15
    Last Post: 05-12-2016, 02:27 PM
  3. Replies: 4
    Last Post: 02-18-2016, 12:06 PM
  4. passing a control name as as variable
    By newbieX in forum Programming
    Replies: 3
    Last Post: 09-26-2014, 02:06 PM
  5. Passing a control as a parameter to a code module.
    By MatthewGrace in forum Programming
    Replies: 4
    Last Post: 06-20-2014, 11:14 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