Consulting

Results 1 to 3 of 3

Thread: setfocus on a form

  1. #1
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location

    setfocus on a form

    Hello,

    I have a textbox on a userform with the following associated code :-

    [VBA]Private Sub txtAmount_AfterUpdate()

    'error handling
    On Error GoTo Err_txtAmount_AfterUpdate

    'if the user types in an amount covert it to millions with 2dp
    txtAmount.Value = Format(CDbl(FormatNumber(txtAmount.Text, 2)), "#,##0.00")

    'error handling
    Exit_txtAmount_AfterUpdate:
    Exit Sub

    Err_txtAmount_AfterUpdate:
    MsgBox "Please ensure you are typing in a number"
    Me.txtAmount.Text = ""
    Me.txtAmount.SetFocus
    Resume Exit_txtAmount_AfterUpdate

    End Sub[/VBA]


    but it doesnt set the focus on the control but moves onto to the next one

    Does anyone know why not???

    Thanks
    Phil

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I recall having a long discussion on this some years ago. The move that triggers the AfterUpdate seems to take precedence over the SetFocus. I don't recall that we worked out a resolution.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If I understand:
    [VBA]Dim Can As Boolean

    Private Sub txtAmount_AfterUpdate()
    Can = False
    'error handling
    On Error GoTo Err_txtAmount_AfterUpdate

    'if the user types in an amount covert it to millions with 2dp
    txtAmount.Value = Format(CDbl(FormatNumber(txtAmount.Text, 2)), "#,##0.00")

    Exit Sub

    Err_txtAmount_AfterUpdate:
    MsgBox "Please ensure you are typing in a number"
    Me.txtAmount.Text = ""
    Can = True
    End Sub

    Private Sub txtAmount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = Can
    End Sub

    Private Sub CommandButton1_Click()
    Unload Me
    End Sub[/VBA]

Posting Permissions

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