+ Reply to Thread
Results 1 to 21 of 21

Focus

  1. #1
    David Unger
    Guest

    Focus

    Hello,

    I wonder if someone can help me with this: I'm trying to confirm a valid
    time entry in a form field, and if invalid, clear the entry and put the
    focus back in that field. If I use the following code it does work (not
    the best method, no doubt), but if I remove the UserForm1.Hide and
    UserForm1.Show lines, the focus always moves to the next field. I know I'm
    missing something basic here, not sure what. Thanks,

    Dave

    If Private Sub txtTime1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    On Error GoTo BadTime
    Dim X
    txtTime1.Text = Format(Val(txtTime1.Text), "00:00")
    X = TimeValue(txtTime1.Text)
    Exit Sub
    BadTime:
    UserForm1.Hide
    MsgBox ("Invalid time")
    txtTime1.Text = ""
    txtTime1.SetFocus
    UserForm1.Show
    End Sub



  2. #2
    Harlan Grove
    Guest

    Re: Focus

    "David Unger" <dave.unger@sasktel.net> wrote...
    >I wonder if someone can help me with this: I'm trying to confirm a valid
    >time entry in a form field, and if invalid, clear the entry and put the
    >focus back in that field. If I use the following code it does work (not
    >the best method, no doubt), but if I remove the UserForm1.Hide and
    >UserForm1.Show lines, the focus always moves to the next field. I know I'm
    >missing something basic here, not sure what. Thanks,

    ....
    >Private Sub txtTime1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > On Error GoTo BadTime
    > Dim X
    > txtTime1.Text = Format(Val(txtTime1.Text), "00:00")
    > X = TimeValue(txtTime1.Text)
    > Exit Sub
    >BadTime:
    > UserForm1.Hide
    > MsgBox ("Invalid time")
    > txtTime1.Text = ""
    > txtTime1.SetFocus
    > UserForm1.Show
    >End Sub


    Use the Cancel parameter to prevent exiting the field. Also, error trapping
    is a crude way to handle type checking. Try something like

    Private Sub Time_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If IsError(Evaluate("--""" & Time.Value & """")) Then
    MsgBox "invalid time: " & Time.Value
    Time.Value = ""
    Cancel = True
    End If
    End Sub



  3. #3
    Dave Unger
    Guest

    Re: Focus

    Harlan,

    Thanks for bringing my attention to the Cancel parameter, and the tip
    on error handling. I knew there had to be a better way. Much
    appreciated,

    Dave


  4. #4
    Dave Unger
    Guest

    Re: Focus

    Harlan,

    I've had a chance to try out your code, can't seem to get it to work.
    No matter what I enter in the field, eg., 2500, it doesn't get trapped.
    Also, I don't understand your Evaluate statement, the --"" and """".
    Thanks, sorry for being a nuisance,

    Dave


  5. #5
    ste
    Guest

    Re: Focus

    hi,
    try this

    BadTime:
    MsgBox ("Invalid time")
    txttime1.SetFocus
    txttime1.Text = ""
    Cancel = True
    End Sub

    bye, ste


  6. #6
    Harlan Grove
    Guest

    Re: Focus

    "Dave Unger" <dave.unger@sasktel.net> wrote...
    >I've had a chance to try out your code, can't seem to get it to work.
    >No matter what I enter in the field, eg., 2500, it doesn't get trapped.
    >Also, I don't understand your Evaluate statement, the --"" and """".
    >Thanks, sorry for being a nuisance,


    My fault. I only tried nonnumeric text in the Time field/textbox. If you
    want to avoid error trapping, use

    If IsError(Evaluate("TIMEVALUE(""" & Time.Value & """)")) Then

    The argument to Evaluate is a call to the worksheet TIMEVALUE function,
    which needs to look like a string constant, thus the doubled double quotes.



  7. #7
    Dave Unger
    Guest

    Re: Focus

    Harlan, me again,

    I just can't seem to get it to work properly - no matter what I throw
    at it (e.g., 09:55 or 09:65), it always follows the error path. I've
    tryed changing a few things, to no avail - any more suggestions?
    Thanks

    Dave


  8. #8
    Dave Unger
    Guest

    Re: Focus

    Harlan, me again,

    I just can't seem to get it to work properly - no matter what I throw
    at it (e.g., 09:55 or 09:65), it always follows the error path. I've
    tryed changing a few things, to no avail - any more suggestions?
    Thanks

    Dave


  9. #9
    Tom Ogilvy
    Guest

    Re: Focus

    Show the code you are currently using. Perhaps you have implemented it
    incorrectly.

    --
    Regards,
    Tom Ogilvy

    "Dave Unger" <dave.unger@sasktel.net> wrote in message
    news:1111336621.611552.31990@l41g2000cwc.googlegroups.com...
    > Harlan, me again,
    >
    > I just can't seem to get it to work properly - no matter what I throw
    > at it (e.g., 09:55 or 09:65), it always follows the error path. I've
    > tryed changing a few things, to no avail - any more suggestions?
    > Thanks
    >
    > Dave
    >




  10. #10
    Dave Unger
    Guest

    Re: Focus

    Hi Tom,

    I'll have to do a 180 from what I just said - for some reason when I
    did a copy/paste of Harlan's code, an extra character got thrown in.
    Now that I've corrected that, ALL number combinations seem to pass. My
    form field (txtTime) restricts the entry to numbers only, the 2nd line
    in the code converts it to a time format. Thanks for having a look,

    Dave

    Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    txtTime.Value =3D Format(txtTime.Value, "00:00")
    If IsError(Evaluate("TIMEVALUE(""=AD" & txtTime.Value & """)")) Then
    txtTime.Value =3D ""
    Cancel =3D True
    End If
    End Sub


  11. #11
    Dave Peterson
    Guest

    Re: Focus

    Harlan didn't use "-" and he didn't use the Format statement, either:

    Option Explicit
    Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If IsError(Evaluate("TIMEVALUE(""" & txtTime.Value & """)")) Then
    txtTime.Value = ""
    Cancel = True
    End If
    End Sub



    Dave Unger wrote:
    >
    > Hi Tom,
    >
    > I'll have to do a 180 from what I just said - for some reason when I
    > did a copy/paste of Harlan's code, an extra character got thrown in.
    > Now that I've corrected that, ALL number combinations seem to pass. My
    > form field (txtTime) restricts the entry to numbers only, the 2nd line
    > in the code converts it to a time format. Thanks for having a look,
    >
    > Dave
    >
    > Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > txtTime.Value = Format(txtTime.Value, "00:00")
    > If IsError(Evaluate("TIMEVALUE(""Â*" & txtTime.Value & """)")) Then
    > txtTime.Value = ""
    > Cancel = True
    > End If
    > End Sub


    --

    Dave Peterson

  12. #12
    Dave Unger
    Guest

    Re: Focus

    Dave,

    I only use the Format statement because I'm entering the time as 900,
    not 09:00 - however, I've tried it without the Format statement,
    doesn't work either.

    It's interesting that you mention the "-", I don't see it in my
    posting, but that's the extra character I was getting when I copied and
    pasted Harlan's example.

    thanks,

    Dave


  13. #13
    Dave Peterson
    Guest

    Re: Focus

    I didn't realize you were entering your times that way, but I added that format
    statement back and it sure seemed to work ok for me (without that "-" stuff).

    But if I enter 966, your format statement makes it look like: 9:66 and excel is
    smart enough to change it to 10:06.

    Is that bad?

    If yes, then maybe you can incorporate Harlan's error checking and your
    conversion into one procedure:

    Option Explicit
    Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim res As Variant
    Dim myStr As String
    myStr = Format(txtTime.Value, "00:00")
    res = Evaluate("TIMEVALUE(""" & myStr & """)")
    If IsError(res) Then
    txtTime.Value = ""
    Cancel = True
    Else
    txtTime.Value = Format(res, "hh:mm")
    End If
    End Sub



    Dave Unger wrote:
    >
    > Dave,
    >
    > I only use the Format statement because I'm entering the time as 900,
    > not 09:00 - however, I've tried it without the Format statement,
    > doesn't work either.
    >
    > It's interesting that you mention the "-", I don't see it in my
    > posting, but that's the extra character I was getting when I copied and
    > pasted Harlan's example.
    >
    > thanks,
    >
    > Dave


    --

    Dave Peterson

  14. #14
    Tom Ogilvy
    Guest

    Re: Focus

    That appears to be a bug in the google beta. It appears to add a hyphen
    sometimes when you paste code.

    Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim s as String
    s = Trim(txtTime)
    if instr(s,":") then
    s = replace(s,":","")
    End if
    if len(s) <= 4 then
    if isnumeric(s) then
    txtTime.Value = format(clng(s),"00:00")
    if Not IsError(Evaluate("TIMEVALUE(""" _
    & txtTime.Value & """)")) then
    Exit sub
    End if
    end if
    End if
    txtTime.Value = ""
    Cancel = True
    End Sub

    there should be no hyphens (-) in this code although there is an underscore
    "_"

    --
    Regards,
    Tom Ogilvy




    "Dave Unger" <dave.unger@sasktel.net> wrote in message
    news:1111355683.534426.222190@l41g2000cwc.googlegroups.com...
    > Dave,
    >
    > I only use the Format statement because I'm entering the time as 900,
    > not 09:00 - however, I've tried it without the Format statement,
    > doesn't work either.
    >
    > It's interesting that you mention the "-", I don't see it in my
    > posting, but that's the extra character I was getting when I copied and
    > pasted Harlan's example.
    >
    > thanks,
    >
    > Dave
    >




  15. #15
    David Unger
    Guest

    Re: Focus

    Hello,

    Dave and Tom, thanks for your code, but in both cases, it would let all
    entries through, valid or invalid. I'm wondering, I'm using Excel 97, and
    the REPLACE function in Tom's code, "s = Replace(s, ":", "")" wasn't
    recognized, used "s = Left$(s, 2) & Right$(s, 2)" instead. Might it be
    that Excel 97 handles the ISERROR function differently than later versions?

    I won't bother you any more with this. For the time being I'll go back to
    using the ONERROR GOTO statement, that seems to work for me. Tom, thanks
    for the heads up about Google inserting the "-", that did throw me off at
    first. On a side note, what's the reason for the CLng in Format(CLng(s),
    "00:00")?

    Harlan, Tom, Dave, thank you for your efforts, much appreciated, and it was
    a learning experience for me.

    Until later,

    Thanks

    Dave

    "Tom Ogilvy" <twogilvy@msn.com> wrote in message
    news:uNLDo2ZLFHA.2136@TK2MSFTNGP14.phx.gbl...
    > That appears to be a bug in the google beta. It appears to add a hyphen
    > sometimes when you paste code.
    >
    > Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > Dim s as String
    > s = Trim(txtTime)
    > if instr(s,":") then
    > s = replace(s,":","")
    > End if
    > if len(s) <= 4 then
    > if isnumeric(s) then
    > txtTime.Value = format(clng(s),"00:00")
    > if Not IsError(Evaluate("TIMEVALUE(""" _
    > & txtTime.Value & """)")) then
    > Exit sub
    > End if
    > end if
    > End if
    > txtTime.Value = ""
    > Cancel = True
    > End Sub
    >
    > there should be no hyphens (-) in this code although there is an

    underscore
    > "_"
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    >
    >
    > "Dave Unger" <dave.unger@sasktel.net> wrote in message
    > news:1111355683.534426.222190@l41g2000cwc.googlegroups.com...
    > > Dave,
    > >
    > > I only use the Format statement because I'm entering the time as 900,
    > > not 09:00 - however, I've tried it without the Format statement,
    > > doesn't work either.
    > >
    > > It's interesting that you mention the "-", I don't see it in my
    > > posting, but that's the extra character I was getting when I copied and
    > > pasted Harlan's example.
    > >
    > > thanks,
    > >
    > > Dave
    > >

    >
    >




  16. #16
    Dave Unger
    Guest

    Re: Focus

    Thanks ste,

    The Cancel=True was the key

    Dave


  17. #17
    Dave Peterson
    Guest

    Re: Focus

    Replace was added in xl2k.

    This line:
    s = replace(s,":","")
    could be replaced with:
    s = application.substitute(s,":","")
    and it'll work in all versions.

    And format() works on numbers. So Tom converted the string in the textbox to a
    number before he applied the formatting.

    David Unger wrote:
    >
    > Hello,
    >
    > Dave and Tom, thanks for your code, but in both cases, it would let all
    > entries through, valid or invalid. I'm wondering, I'm using Excel 97, and
    > the REPLACE function in Tom's code, "s = Replace(s, ":", "")" wasn't
    > recognized, used "s = Left$(s, 2) & Right$(s, 2)" instead. Might it be
    > that Excel 97 handles the ISERROR function differently than later versions?
    >
    > I won't bother you any more with this. For the time being I'll go back to
    > using the ONERROR GOTO statement, that seems to work for me. Tom, thanks
    > for the heads up about Google inserting the "-", that did throw me off at
    > first. On a side note, what's the reason for the CLng in Format(CLng(s),
    > "00:00")?
    >
    > Harlan, Tom, Dave, thank you for your efforts, much appreciated, and it was
    > a learning experience for me.
    >
    > Until later,
    >
    > Thanks
    >
    > Dave
    >
    > "Tom Ogilvy" <twogilvy@msn.com> wrote in message
    > news:uNLDo2ZLFHA.2136@TK2MSFTNGP14.phx.gbl...
    > > That appears to be a bug in the google beta. It appears to add a hyphen
    > > sometimes when you paste code.
    > >
    > > Private Sub txtTime_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > > Dim s as String
    > > s = Trim(txtTime)
    > > if instr(s,":") then
    > > s = replace(s,":","")
    > > End if
    > > if len(s) <= 4 then
    > > if isnumeric(s) then
    > > txtTime.Value = format(clng(s),"00:00")
    > > if Not IsError(Evaluate("TIMEVALUE(""" _
    > > & txtTime.Value & """)")) then
    > > Exit sub
    > > End if
    > > end if
    > > End if
    > > txtTime.Value = ""
    > > Cancel = True
    > > End Sub
    > >
    > > there should be no hyphens (-) in this code although there is an

    > underscore
    > > "_"
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > >
    > >
    > > "Dave Unger" <dave.unger@sasktel.net> wrote in message
    > > news:1111355683.534426.222190@l41g2000cwc.googlegroups.com...
    > > > Dave,
    > > >
    > > > I only use the Format statement because I'm entering the time as 900,
    > > > not 09:00 - however, I've tried it without the Format statement,
    > > > doesn't work either.
    > > >
    > > > It's interesting that you mention the "-", I don't see it in my
    > > > posting, but that's the extra character I was getting when I copied and
    > > > pasted Harlan's example.
    > > >
    > > > thanks,
    > > >
    > > > Dave
    > > >

    > >
    > >


    --

    Dave Peterson

  18. #18
    Dave Unger
    Guest

    Re: Focus

    Dave,

    The reason I was asking - the Format line seems to work whether CLng is
    present or not. As I'm still on the steep part of the learning curve,
    I probably wouldn't have realized the need for it.

    Dave


  19. #19
    Gord Dibben
    Guest

    Re: Focus

    Tom

    True. I am finding I have to be very careful when copying from postings.

    The hyphen(s) are thrown in with more frequency lately.

    I have noticed it in posted worksheet formulas also.


    Gord Dibben Excel MVP

    On Sun, 20 Mar 2005 17:41:23 -0500, "Tom Ogilvy" <twogilvy@msn.com> wrote:

    >That appears to be a bug in the google beta. It appears to add a hyphen
    >sometimes when you paste code.



  20. #20
    Harlan Grove
    Guest

    Re: Focus

    Gord Dibben wrote...
    >True. I am finding I have to be very careful when copying from

    postings.
    >
    >The hyphen(s) are thrown in with more frequency lately.
    >
    >I have noticed it in posted worksheet formulas also.

    ....

    I wrote about this a week ago. It's unsafe to copy anything from Google
    Groups beta *except* from *ORIGINAL* versions of postings which include
    all the lovely NNTP tags above the body of the message. Those originals
    seem to be literal text without any HTML interpretation, so HTML 'soft'
    hyphens become visible.


  21. #21
    Harlan Grove
    Guest

    Re: Focus

    Dave Unger wrote...
    >The reason I was asking - the Format line seems to work whether CLng

    is
    >present or not. As I'm still on the steep part of the learning curve,
    >I probably wouldn't have realized the need for it.


    Format appears to be smart enough to convert numeric text (i.e., stuff
    for which IsNumeric returns TRUE and VarType returns vbString).
    Therefore, no type conversions appears to be needed when calling
    Format.

    That said, Format(x, "00:00") seems to treat the second argument the
    same as "00\:00", so just inserts a colon when it can convert x to a
    number. Then the worksheet function TIMEVALUE applies Excel's date/time
    semantics in which 9:99 would be converted to 9 hours 99 minutes = 10
    hours 39 minutes, so automatically converted to 10:39.

    If you're *ALWAYS* entering times without colons and without AM/PM, so
    always in [h]hmm format where hours could range from 0 or 00 to 24,
    then you'd be *MUCH* better off using simple *TEXT* *PATTERN* tests for
    valid time entries.


    Time.Value = Trim(Time.Value)
    If Not(Time.Value Like "2[0-4][0-5][0-9]" _
    Or Time.Value Like "[01][0-9][0-5][0-9]" _
    Or Time.Value Like "[0-9][0-5][0-9]") Then
    MsgBox "Invalid time entry: " & Time.Value
    Time.Value = ""
    Cancel = True
    End If


+ 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