+ Reply to Thread
Results 1 to 6 of 6

how to keep focus on txt_box in userform

  1. #1
    Pierre via OfficeKB.com
    Guest

    how to keep focus on txt_box in userform

    Hi,

    i have the following code for a txt_box on a userform:

    Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    OnlyNumbers nw_premieNP
    Worksheets("staffelberekening").Range("G12") = nw_premieNP.Value
    End Sub

    the Only numbers sub looks like

    Private Sub OnlyNumbers(ctl As Object)
    With ctl
    If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    MsgBox "Sorry, alleen getallen toegestaan", vbOKOnly, "De Pensioen
    Adviseur"
    .Value = vbNullString
    .SetFocus
    End If
    End With
    End Sub

    The problem is that if the input is not numeric, the input is deleted but the
    focus is put on the next input box.
    How can i have the focus on the field that was input wrong ?

    thanks,
    Pierre


    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200509/1

  2. #2
    Norman Jones
    Guest

    Re: how to keep focus on txt_box in userform

    Hi Jean-Pierre,

    Try:

    Option Explicit
    Public blInvalidEntry As Boolean '<<=== Module level flag


    Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    OnlyNumbers nw_premieNP
    Cancel = blInvalidEntry <<============ ADDED
    Worksheets("Sheet2").Range("G12") = nw_premieNP.Value
    End Sub

    Private Sub OnlyNumbers(ctl As Object)
    With ctl
    If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    MsgBox "Sorry, alleen getallen toegestaan", _
    vbOKOnly, "De Pensioen Adviseur """
    .Value = vbNullString
    blInvalidEntry = True
    Else
    blInvalidEntry = False
    End If
    End With
    End Sub

    ---
    Regards,
    Norman



    "Pierre via OfficeKB.com" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > i have the following code for a txt_box on a userform:
    >
    > Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > OnlyNumbers nw_premieNP
    > Worksheets("staffelberekening").Range("G12") = nw_premieNP.Value
    > End Sub
    >
    > the Only numbers sub looks like
    >
    > Private Sub OnlyNumbers(ctl As Object)
    > With ctl
    > If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString
    > Then
    > MsgBox "Sorry, alleen getallen toegestaan", vbOKOnly, "De
    > Pensioen
    > Adviseur"
    > .Value = vbNullString
    > .SetFocus
    > End If
    > End With
    > End Sub
    >
    > The problem is that if the input is not numeric, the input is deleted but
    > the
    > focus is put on the next input box.
    > How can i have the focus on the field that was input wrong ?
    >
    > thanks,
    > Pierre
    >
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200509/1




  3. #3
    Norman Jones
    Guest

    Re: how to keep focus on txt_box in userform

    Hi Jean-Pierre,

    As an alternative apporoach, you might consider using the textbox's KeyPress
    event to ensure numeric entry, e.g:

    Private Sub nw_premieNP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case 48 To 57
    Case Else
    MsgBox "Sorry, alleen getallen toegestaan", vbOKOnly, "De Pensioen"
    KeyAscii = 0
    End Select
    End Sub

    ---
    Regards,
    Norman



    "Norman Jones" <[email protected]> wrote in message
    news:%[email protected]...
    > Hi Jean-Pierre,
    >
    > Try:
    >
    > Option Explicit
    > Public blInvalidEntry As Boolean '<<=== Module level flag
    >
    >
    > Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > OnlyNumbers nw_premieNP
    > Cancel = blInvalidEntry <<============ ADDED
    > Worksheets("Sheet2").Range("G12") = nw_premieNP.Value
    > End Sub
    >
    > Private Sub OnlyNumbers(ctl As Object)
    > With ctl
    > If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    > MsgBox "Sorry, alleen getallen toegestaan", _
    > vbOKOnly, "De Pensioen Adviseur """
    > .Value = vbNullString
    > blInvalidEntry = True
    > Else
    > blInvalidEntry = False
    > End If
    > End With
    > End Sub
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Pierre via OfficeKB.com" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi,
    >>
    >> i have the following code for a txt_box on a userform:
    >>
    >> Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    >> OnlyNumbers nw_premieNP
    >> Worksheets("staffelberekening").Range("G12") = nw_premieNP.Value
    >> End Sub
    >>
    >> the Only numbers sub looks like
    >>
    >> Private Sub OnlyNumbers(ctl As Object)
    >> With ctl
    >> If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString
    >> Then
    >> MsgBox "Sorry, alleen getallen toegestaan", vbOKOnly, "De
    >> Pensioen
    >> Adviseur"
    >> .Value = vbNullString
    >> .SetFocus
    >> End If
    >> End With
    >> End Sub
    >>
    >> The problem is that if the input is not numeric, the input is deleted but
    >> the
    >> focus is put on the next input box.
    >> How can i have the focus on the field that was input wrong ?
    >>
    >> thanks,
    >> Pierre
    >>
    >>
    >> --
    >> Message posted via OfficeKB.com
    >> http://www.officekb.com/Uwe/Forums.a...mming/200509/1

    >
    >




  4. #4
    Pierre via OfficeKB.com
    Guest

    Re: how to keep focus on txt_box in userform

    Hi Norman,

    This seems to work but i still don't get the focus on the curent textbox.
    In my code the cursor was blinking in the next txt_box.
    With your code, i don't see a blinking cursor at all....
    Is there a way tot select the 'invalid' code in the field so that the user
    can give a new input?
    thanks,
    Pierre

    Norman Jones wrote:
    >Hi Jean-Pierre,
    >
    >Try:
    >
    >Option Explicit
    >Public blInvalidEntry As Boolean '<<=== Module level flag
    >
    > Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > OnlyNumbers nw_premieNP
    > Cancel = blInvalidEntry <<============ ADDED
    > Worksheets("Sheet2").Range("G12") = nw_premieNP.Value
    > End Sub
    >
    >Private Sub OnlyNumbers(ctl As Object)
    > With ctl
    > If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    > MsgBox "Sorry, alleen getallen toegestaan", _
    > vbOKOnly, "De Pensioen Adviseur """
    > .Value = vbNullString
    > blInvalidEntry = True
    > Else
    > blInvalidEntry = False
    > End If
    > End With
    >End Sub
    >
    >---
    >Regards,
    >Norman
    >
    >> Hi,
    >>

    >[quoted text clipped - 27 lines]
    >> thanks,
    >> Pierre



    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200509/1

  5. #5
    Norman Jones
    Guest

    Re: how to keep focus on txt_box in userform

    Hi Pierre,

    > With your code, i don't see a blinking cursor at all....


    Try restoring your original SetFocus instruction in the OnlyNumbers sub,
    i.e.:

    Private Sub OnlyNumbers(ctl As Object)
    With ctl
    If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    MsgBox "Sorry, alleen getallen toegestaan", _
    vbOKOnly, "De Pensioen Adviseur """
    .Value = vbNullString
    .SetFocus ' <<=========== Restored
    blInvalidEntry = True
    Else
    blInvalidEntry = False
    End If
    End With
    End Sub

    ---
    Regards,
    Norman



    "Pierre via OfficeKB.com" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Norman,
    >
    > This seems to work but i still don't get the focus on the curent textbox.
    > In my code the cursor was blinking in the next txt_box.
    > With your code, i don't see a blinking cursor at all....
    > Is there a way tot select the 'invalid' code in the field so that the user
    > can give a new input?
    > thanks,
    > Pierre
    >
    > Norman Jones wrote:
    >>Hi Jean-Pierre,
    >>
    >>Try:
    >>
    >>Option Explicit
    >>Public blInvalidEntry As Boolean '<<=== Module level flag
    >>
    >> Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    >> OnlyNumbers nw_premieNP
    >> Cancel = blInvalidEntry <<============ ADDED
    >> Worksheets("Sheet2").Range("G12") = nw_premieNP.Value
    >> End Sub
    >>
    >>Private Sub OnlyNumbers(ctl As Object)
    >> With ctl
    >> If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    >> MsgBox "Sorry, alleen getallen toegestaan", _
    >> vbOKOnly, "De Pensioen Adviseur """
    >> .Value = vbNullString
    >> blInvalidEntry = True
    >> Else
    >> blInvalidEntry = False
    >> End If
    >> End With
    >>End Sub
    >>
    >>---
    >>Regards,
    >>Norman
    >>
    >>> Hi,
    >>>

    >>[quoted text clipped - 27 lines]
    >>> thanks,
    >>> Pierre

    >
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200509/1




  6. #6
    Dave Peterson
    Guest

    Re: how to keep focus on txt_box in userform

    You have an alternative at your first post.

    "Pierre via OfficeKB.com" wrote:
    >
    > Hi,
    >
    > i have the following code for a txt_box on a userform:
    >
    > Private Sub nw_premieNP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > OnlyNumbers nw_premieNP
    > Worksheets("staffelberekening").Range("G12") = nw_premieNP.Value
    > End Sub
    >
    > the Only numbers sub looks like
    >
    > Private Sub OnlyNumbers(ctl As Object)
    > With ctl
    > If Not IsNumeric(Left(.Value, 1)) And .Value <> vbNullString Then
    > MsgBox "Sorry, alleen getallen toegestaan", vbOKOnly, "De Pensioen
    > Adviseur"
    > .Value = vbNullString
    > .SetFocus
    > End If
    > End With
    > End Sub
    >
    > The problem is that if the input is not numeric, the input is deleted but the
    > focus is put on the next input box.
    > How can i have the focus on the field that was input wrong ?
    >
    > thanks,
    > Pierre
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200509/1


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1