Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097

    =Format([Hr],"hh:nnAM/PM") doesn't work when Hr is a string

    =Format([Hr],"hh:nnAM/PM") doesn't work when Hr is a string. All the string values you see in the left column of the screenshot format as "12:00AM", whereas they should format as you see in the right column of the screenshot.



    I could save Hr as Long seconds like the fractional portion of a Date format but I have the idea that it's simpler than that?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you CAN use strings, but you cant use time format.
    [timefield] MUST be numeric.


    =format([timefield],"hh:nn AMPM")

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    BTW, I do have code that will to the formatting, like you see in the right column, but I'm thinking that the Format function can be used to simplify the job.

  4. #4
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Timevalue function may be of use here to convert [hr] to a time.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What screen shot?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    How is your "timefield" different from my "Hr", i.e., both are strings?

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Good grief!

    Click image for larger version. 

Name:	002.jpg 
Views:	12 
Size:	42.2 KB 
ID:	31423

  8. #8
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    I get a #Type error with =Format(TimeValue([Hr]),"hh:nnAMPM")

    BTW, I'm using the expression you see above as the Format property of a text box.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Can't use in Format property, use in ControlSource property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    if your time is a string (0930 for 9:30 AM, 21:30 for 9:30 PM) you can do

    Format(CDate(Left([hr],2) & ":" & Right([hr],2)),"hh:nnAM/PM")

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Oops! I meant ControlSource, not Format.............. had "Format" on the brain!

  12. #12
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    That works. I'll have to add =IIF(isnull([Hr],null,Format(CDate(Left([hr],2) & ":" & Right([hr],2)),"hh:nnAM/PM")) so the text box formats properly when Hr is null.

  13. #13
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Just needed to add the ")" on the IsNull function.

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    It should still error because all parts of the IIf() must be able to evaluate, even the part that is not returned. This is different from an IIf() used in SQL statement.

    So either do the IIf() calc in query or try:

    =IIF(isnull([Hr],null,Format(CDate(Left(Nz([hr],"0000"),2) & ":" & Right(Nz([hr],"0000"),2)),"hh:nnAM/PM"))

    Whoops, just tested your expression in textbox and it does not error. It is in VBA that the 'all parts must evaluate' applies.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Here's the form the produces "Hr". Works on the same principle as Allen Browne's Calendar form.

    Click image for larger version. 

Name:	003.jpg 
Views:	7 
Size:	32.7 KB 
ID:	31426

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 05-28-2015, 03:24 PM
  2. Replies: 3
    Last Post: 12-23-2014, 01:00 AM
  3. Replies: 2
    Last Post: 12-04-2012, 01:03 AM
  4. Replies: 1
    Last Post: 04-05-2012, 08:26 AM
  5. Replies: 7
    Last Post: 01-19-2011, 10:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums