Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Mar 2010
    Posts
    88

    Unanswered: Puzzled by time format in Text Box

    Hi all

    I have some forms with a Text Box to enter a time. I'm only interested in hours and minutes. The Format is set to "Short Time" and the input mask to "00:00;0;_"

    In most cases, where the default value of the time is null, this works great: you type in the time and it just works.

    However, in some cases I preload the text box with the time part of "Now()" as this is a reasonable default. But if the user wants to change it they can.

    My problem is that when I get the time part of Now() I get seconds as well (e.g. 14:35:27). It is all loaded into the text box and as soon as you try to change it you get into trouble with the input mask.

    Yes, you can delete the S part but this is unnecessarily fiddly for my users (who are not computer literate, so I am trying very hard to make this as easy to use and foolproof as possible).

    Can anyone suggest some workarounds here?

    TIA.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    The "Short Time" format include the seconds in it, so it's normal to have them when you extract the value from the Now() function. You can define your own format and input mask if the predefined ones do not fit your needs.
    Have a nice day!

  3. #3
    Join Date
    Mar 2010
    Posts
    88
    Thanks for the response, but I think I need more guidance. According to Access Help, Short Time format is defined as hours and minutes only, which is what I want. So the format for the control is set to Short Time (if you try putting hh:nn it just gets changed back to "Short Time"), and the time is initially displayed in that format. But as soon as you click in the box to change something, the seconds appear and, as I say, you are in trouble with the input mask. So how do I stop the seconds appearing when I click in the box?

    TIA

    PS - I should probably add that this is an Unbound control...
    Last edited by CeejayDBF; 07-13-10 at 22:11. Reason: clarification

  4. #4
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Set your default value to =time(), set the format to Short time and set your format to 00:00;0;#.

    Time is the time of the system clock.

  5. #5
    Join Date
    Mar 2010
    Posts
    88
    Quote Originally Posted by Poppa Smurf View Post
    Set your default value to =time(), set the format to Short time and set your format to 00:00;0;#.

    Time is the time of the system clock.
    First, thanks for reminding me of the existence of Time() - I'd quite forgotten and was making my life more complicated than I needed to! Not relevant to this problem though.

    My format was already Short Time and I assume you meant the Input Mask should be 00:00;0;# ... well apart from the # instead of _ at the end that's exactly what I have (I did just try # in case there was some magic ... but no!).

    So I've done all that and still have the problem. If you tab into the control it stays in the Short Time format of HH:NN and you can type a new value if you want. But if I click into it, the seconds get shown as well...

    More ideas, please?

    Thanks

    PS: I should say that the problem only appears when you click into the control as the first thing you do. If you tab to the control, type something in, then click in another control, then click in this one - it stays in HH:NN format and all is well. But if I restart the form, and click to the control, the seconds appear again...
    Last edited by CeejayDBF; 07-14-10 at 06:03. Reason: clarification

  6. #6
    Join Date
    Mar 2010
    Posts
    88
    Well, here is a partial answer. If I set the input mask to "00:00C99;0;_" then at least the extended version, with seconds included, is valid against the input mask, while the short version is also valid as the seconds part is optional.

    It't not pretty, though. Any other ideas?

  7. #7
    Join Date
    Mar 2010
    Posts
    88
    Coming back to this old thread of mine ... I let it go previously, but I'm now revisiting this application and trying to tidy up some loose ends, of which this is one. I googled for helpful threads and found my own!! Access 2007.

    So let me put the problem a different way...

    On a form, I have two unbound text boxes (StartTime and EndTime). Both have the format "Short Time" and both have the input mask "00:00;0;_", and both are initially blank.

    When the user enters the StartTime box they see a time in the format hh:nn.

    I have AfterUpdate code which adds an hour to that and puts that value in EndTime, as a default value for the user. As expected, it appears there as hh:nn

    Now, if the user clicks into the StartTime box then they are editing a field in the format hh:nn and all is well - they type whatever they want.

    BUT ... if they click into the EndTime box, the field is suddenly expanded to hh:nn:ss and the users are getting confused... not least because if they just change the minutes value and don't delete the seconds, Access throws out their entry as being badly formatted!

    Even more confusing: if they do manage to enter a legal time in EndTime, and then click back into it to change it again, it stays as hh:nn

    It appears that the hh:nn:ss is popping up because a calculated value has been put there??

    I'd really like to find a way of making sure that the seconds don't keep popping up, and that my users can just enter hh:nn.

    Any clues?

  8. #8
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Set the format of the Start and End times box to Short Time.

  9. #9
    Join Date
    Mar 2010
    Posts
    88
    Quote Originally Posted by Poppa Smurf View Post
    Set the format of the Start and End times box to Short Time.
    That's what they are already. This is why I'm puzzled....

  10. #10
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Setting a format on a textbox won't change its Value property, only the way it is displayed. And when the control has the focus it's its Text property that you see.

    You could try this:

    1. Unbind the textbox (let's call it Text_Time and let's suppose that it was bound to a field named MyTime)).

    2. In the Current event handler of the form, use this:
    Code:
    Private Sub Form_Current()
    
        Me.Text_Time.Value = Format(Me!MyTime, "hh:nn")
        
    End Sub
    While in the AfterUpdate event handler of the textbox, use:
    Code:
    Private Sub Text_Time_AfterUpdate()
    
        Me!MyTime = IIf(Isnull(Me.Text_Time.Value, Null, CDate(Me.Text_Time.Value))
        
    End Sub

    3. As an alternative method, you also could change the RecordSource of the form and use a query where Mytime would be returned as
    Code:
    SELECT .... Format(MyTime, "hh:nn") AS MyFormattedTime... FROM...
    You could then bind Text_Time to MyFormattedTime but it would be read-only.
    Have a nice day!

  11. #11
    Join Date
    Mar 2010
    Posts
    88
    Thank you for taking the time to respond, but I don't think you've quite understood my problem.

    The text boxes are already unbound - I pick up the values much later, when the user clicks an "OK" button: then I do a lot of checking and if everything is in order I copy these values (among others) to where they have to go.

    What is confusing me is that, even though I have set the format as "Short Time", the "EndTime" box will show a value such as "13:26:00" if you click into it ... but only if that value was generated by my code adding an hour to the value of "StartTime" ("12:26" in this example.

    If I successfully edit the value in "EndTime" to, say "14:00", click somewhere else and then back into EndTime, I will see "14:00".

    Why is it sometimes showing the seconds and sometimes not?

  12. #12
    Join Date
    Oct 2009
    Posts
    204
    Have you tried setting the On Click to format the field as hh:nn right after it updates the time? Or putting it in On Dirty?

  13. #13
    Join Date
    Jun 2007
    Location
    Maitland NSW,Australia
    Posts
    388
    Have a look at this example it is 2007 bversion, I have this also in 2003 if you require it.

    In the After Update event for the start time I have thiscode
    Sub start_time_BeforeUpdate(Cancel As Integer)
    Me!finish_time = DateAdd("h", 1, Me!start_time)
    End Sub

    Both text boxes are set to Short Date.
    The mask for both fields are 00:00;0;#
    Attached Files Attached Files

  14. #14
    Join Date
    Mar 2010
    Posts
    88
    OK, Seriously weird ....

    Poppa Smurf: I downloaded your db (thanks!) and of course it works fine. So now I try to track down the difference between yours and mine! One change I made was to the way the hour is added: yours is elegant, mine slightly less so but it ought to work:
    end_time = start_time + #01:00:00 AM#

    At this point your test DB started showing the same problem as mine, with the seconds appearing in end_time when you click into it! Strange, I think, but never mind: I apply your method of adding an hour to my db and of course it makes no difference, I still have my problem.

    I go back to your db, reverse the change, it goes back to normal - put it back again and it stays normal! Now nothing I can do to your test db will break it - but I am certain that I saw the problem at one point. I even tried retracing my exact steps from the time I opened up the ZIP file, but to no avail.

    This is the kind of irrational behaviour that is sometimes fixed by a quick Compact and Repair, but no...

    So now I turn to chris07tibgs - no I hadn't tried that, but now I have. Now I can see the format flash briefly to, say, 12:34:00 and then back to 12:34.

    This is a lot better and, although I still have no idea why this should be necessary, I'll take this as being good enough.

    BTW, in case anyone is thinking that there is something broken in this particular form and I should just rebuild it - I have three different forms all showing similar behaviour!

    Anyway, I think I'll just take Chris' suggestion and live with a little flash for this time.

    Thanks for all the support.

  15. #15
    Join Date
    Mar 2010
    Posts
    88
    Just to add to that in case anyone has any ideas of where this is coming from ... I consistently have the problem whenever I have a text box, formatted as Short Time, where I fill the value with VBA code.

    If the box is left empty, or filled by being bound to a table element, or filled by having a value typed in, then it's ok - the next click into the box will show something like "12:34". But if the box is set by a code statement, then the next click in will show something like "12:34:00".

    It's as if the Format property is being amended by the VBA code that is writing a value....... even though Poppa Smurf's example shows that not all DBs behave this way!

Posting Permissions

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