Results 1 to 9 of 9
  1. #1
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    42

    How to .SetFocus after a timer event?

    I have a form and if the user enters the wrong info in the txtbox, I have it flashing a message in a label for 2 seconds.
    The problem is how can I get it go go back to the txtbox after the timer event. (I've tried the SetFocus command inside and outside the Private Sub and it still don't set the focus back.)

    Private Sub Form_Timer()
    LblError.Caption = "Wrong Value"
    Me!LblError.Visible = Not Me!LblError.Visible
    txtbox = ""


    txtbox.SetFocus
    End Sub

    Help-Please?

  2. #2
    J-Man is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    1
    Would you use something like DoCmd.GoToControl "txtBox"

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Using a timer like that is a bad idea IMO, because you'd have to alter the timer property after it runs, otherwise it just continues to run? I'd probably use BeforeUpdate event and a pause function. Then when control returns from the pause function, set the focus, but...

    I would not directly refer to controls on a form as in txtBox but rather Me.txtBox, nor would I use Me! instead of Me. Not sure if that has anything to do with your focus problem since I'd never write it that way. Probably not related. txtBox is somewhat lacking as a control name, no?
    Example of a pause function (5 seconds pause called as pause 5)

    Code:
    Sub pause(sngSecs As Single)
    Dim endTime As Single
    
    endTime = Timer
    Do Until Timer > endTime + sngSecs
    'Debug.Print Timer
    Loop
    
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Does it even clear the textbox?
    I would use Me.txtbox ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    42
    Still not working proper.. I tried this with the new timer code:

    LblMB.Visible = True
    LblMB.Caption = "Wrong Value"
    pause 5
    LblMB.Caption = ""
    txtIIRNumber = ""
    DoCmd.GoToControl "txtIIRNumber"
    Exit Sub

    It does not even update the LblMB with "Wrong Value". (It may flash it but I don't see it on screen.)
    It also does not set the focus still.
    txtIIRNumber does clear.
    (With the Form timer on or off / Set to 5000 or 0)

    It seems it does not update until the code is released/finished (including with pauses set which you have to wait for.)
    The form only releases after the delay but the focus jumps to my exit button. (This button I tried moving in my tab order and around the screen and it still only releases it to it.)

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Try a

    Me.Repaint

    after the caption settings to refresh any from changes.
    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 ↓↓

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    try using the before update event.(as Micron suggested) If you cancel the update, focus remains in the textbox.

    Code:
    Private Sub Text0_BeforeUpdate(Cancel As Integer)
        Dim i As Integer
    
    
        If Me.Text0 <> "a" Then   'change for your criteria
            Cancel = True
        
            For i = 1 To 10
                If Me.LWarning.Visible = False Then
                    Me.LWarning.Visible = True
                Else
                    Me.LWarning.Visible = False
                End If
                Pause 0.25
            Next i
    
            Me.Text0.SelStart = 0
            Me.Text0.SelLength = Len(Me.Text0)
    
        End If
    
    End Sub
    Code:
    Public Function Pause(NumberOfSeconds As Variant)
    
        On Error GoTo Err_Pause
    
        Dim PauseTime As Variant, start As Variant
    
        PauseTime = NumberOfSeconds
        start = Timer
        Do While Timer < start + PauseTime
            DoEvents
        Loop
    
    Exit_Pause:
        Exit Function
    
    Err_Pause:
        MsgBox Err.Number & " - " & Err.Description, vbCritical, "Pause()"
        Resume Exit_Pause
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    JB510's Avatar
    JB510 is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Ontario
    Posts
    42
    Thanks! I played with your code listed and got it to work! Thanks for your time, it was greatly appreciated!

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    We're all happy to help.

    Good luck with your project.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. On timer event strange effects
    By roberto21 in forum Forms
    Replies: 6
    Last Post: 10-23-2022, 04:58 AM
  2. On Timer event issue
    By jrock1203 in forum Forms
    Replies: 1
    Last Post: 05-01-2018, 09:42 AM
  3. Access as event timer?
    By Jennifer Murphy in forum Access
    Replies: 1
    Last Post: 01-31-2014, 08:30 PM
  4. Timed Reminder Box; Event Timer?
    By WEJ in forum Access
    Replies: 6
    Last Post: 05-26-2013, 03:12 PM
  5. Timer Event - Why does this happen
    By Rhino373 in forum Programming
    Replies: 2
    Last Post: 05-26-2011, 07:18 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