Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58

    Unanswered: setfocus dosn't work

    User can update a text box & in after update event for that textbox I do some checking, if error found, msgbox reports it then I do .setfocus back to that textbox. But setfocus doesn't work. Why is it so ?
    I've tried setfocus before & after msgbox, & I've tried repaint of form.

    I'm using Access 2000 Sp 3
    catkins

  2. #2
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    please post code

  3. #3
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    here is my code:
    (the object is a combo box, not text box as stated orig, but same applies)

    Private Function AllFieldsFilled() As Boolean

    If IsNull(Me.cboTagColour) Then
    MsgBox ("Tag Colour not entered")
    Me.cboTagColour.SetFocus
    AllFieldsFilled = False
    GoTo Exit_AllFieldsFilled
    ....

    on return from this function, focus is not on cboTagColour
    catkins

  4. #4
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    your code looks fine...is there any other code that executes after this function that would be pulling the focus away.

    Where does the focus go?

  5. #5
    Join Date
    Oct 2003
    Location
    Ger
    Posts
    1,969
    Provided Answers: 1
    I had something like taht, then I used the SendKey function with {TAB}

  6. #6
    Join Date
    Sep 2002
    Location
    South Wales
    Posts
    580

    Unhappy Out of interest

    Why not check during Before Update event?

    Makes sense to me!
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

  7. #7
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    Thanks for all the assistance.
    The object I am trying to return the focus to is a bound object, but the same happens when I "unbind" it.
    The focus remains on whatever object the cursor moved to when I finished the data entry on the object I'm trying to set the focus back to, either by typing the text & then using the mouse or the tab key.
    When I try to move the focus in the BeforeUpdate event I get an error stating: "You must save the field before you execute the goto control action ... or the SetFocus method".

    The sendkeys fn seems a bit of overkill ?? SetFocus should do the job - should it not ??

    I am sure I have done this in previous applications (no, I don't have access to them now).
    catkins

  8. #8
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    Me again.I've just set up a new access project with just a form with 2 text boxes.

    Private Sub Text0_AfterUpdate()
    MsgBox ("form0, after")
    Me.Text0.SetFocus
    End Sub

    after entering text in textbox text0 & hitting tab or moving to other textbox with the mouse, the message box comes up, I hit enter, then the focus is on the next textbox ??
    catkins

  9. #9
    Join Date
    Jul 2003
    Location
    Kaukapakapa, NZ
    Posts
    15
    Hi,

    I think its because as your textbox already has the focus you can't set focus to its self.

    I found a way around this by doing the checking with the gotfocus event for the next textbox and if it failed then showed the message and setfocus back to the ist textbox.

    May not be the correct way but works.

  10. #10
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    ooww! that sounds messy, & it wouldn't work if user used mouse to go to next object rather than tab, & what happens with last object on form ??
    catkins

  11. #11
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    Private Sub cboTagColour_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.cboTagColour) Then
    MsgBox ("Tag Colour not entered")
    Cancel = True
    End If
    End Sub

    Private Sub cboTagColour_LostFocus()
    If IsNull(Me.cboTagColour) Then
    MsgBox ("Tag Colour not entered")
    Me.cboTagColour.SetFocus
    End If
    End Sub

  12. #12
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    Thanks Rocky, I presume you meant something like this :

    Public gblnCancel As Boolean

    Private Sub Text0_BeforeUpdate(Cancel As Integer)
    If Me.Text0 = 1 Then
    MsgBox ("error")
    DoCmd.CancelEvent
    gblnCancel = True
    Else
    gblnCancel = False
    End If
    End Sub

    Private Sub Text0_LostFocus()
    If gblnCancel Then
    Me.Text0.SetFocus
    End If
    End Sub

    That works, but it seems a bit of a sledgehammer-to-crack-a-banana solution.
    catkins

  13. #13
    Join Date
    Oct 2001
    Location
    Chicago
    Posts
    440
    Not sure what you are checking - you mentioned
    IsNull(Me.cboTagColour)
    and now
    Me.Text0 = 1

    You need to use the Exit event rather than the Lost focus as I posted earlier since this has a cancel event and occurs befor the control has lost the focus. Assuming that is what you want.

    Private Sub Text0_Exit(Cancel As Integer)
    If Me.Text0 = 1 Then
    MsgBox ("error")
    Cancel = True
    End If
    End Sub

    As far as the complexity of a solution to your needs. Well.. you have to account for a lot unless you tightly lock the form down. What if the user closes the form and the above events never fire. This will take a sledgehammer to trap all events that may occur. Use the control's events, the form's events, the Dirty property, etc...

    This is much easier if the form is not bound to a recordset.

    Matt

  14. #14
    Join Date
    Dec 2003
    Location
    Tasmania
    Posts
    58
    Thanks Matt - that was all I needed to know - use the exit event rather than the after update.

    You're a wiz

    Colin
    catkins

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •