Re: Formatting numbers

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 12/02/04


Date: Thu, 2 Dec 2004 01:45:23 -0500


> >What's the easiest way to format a number so that the result is n
> >characters wide, has m decimal places displayed (with rounding), and
> >has a comma every 3 places on the left?
> >
> >The FormatNumber$ function comes close. It allows me to specify the
> >number of decimal places and control the thousands delimiters, but I
> >have no control over the total field width. If only I could add one
> >parameter:
> >
> > FormatNumber$(nData,n,m)
> >
> >The Format$ function does it all, but I have to generate a format
> >mask. I'd like simple numeric parameters n and m.
>
> Ooopps. My mistake. Format$ does not do it all unless I want leading
> zeroes. I forgot that
>
> Format$(123,"#####")
>
> returns a string of length 3, not 5.

Does this function do what you want?

Function FieldFormat(Value As Variant, _
                     DecimalPlaces As Long, _
                     FieldSize As Long) As String
  Dim DecimalPoint As String
  If DecimalPlaces > 0 Then DecimalPoint = "."
  FieldFormat = Format$(Format$(Value, "#,###" & _
                        DecimalPoint & _
                        String$(DecimalPlaces, "#")), _
                        String$(FieldSize, "@"))
End Function

Rick - MVP



Relevant Pages

  • Re: time calculation problem [OT]
    ... But Al's previous formula though it didnot work gave me the format i wanted. ... In the former I was using the DTPicker control, which seemed to allow me to use two controls on the same underlying field, and somehow managed to avoid problems when I edited either: ... which appears as a "general" date/time format when clicked. ... I've wondered about using an event handler to intercept the attempt to edit in order to present a popup form with separate date and time text boxes which are then combined correctly when this popup form is closed. ...
    (microsoft.public.access.gettingstarted)
  • Re: time calculation problem [OT]
    ... But Al's previous formula though it didnot work gave me the format i wanted. ... In the former I was using the DTPicker control, which seemed to allow me to use two controls on the same underlying field, and somehow managed to avoid problems when I edited either: ... I've wondered about using an event handler to intercept the attempt to edit in order to present a popup form with separate date and time text boxes which are then combined correctly when this popup form is closed. ... What I wanted was to avoid users having to edit a date/time in "General ...
    (microsoft.public.access.gettingstarted)
  • Re: time calculation problem [OT]
    ... But Al's previous formula though it didnot work gave me the format i wanted. ... In the former I was using the DTPicker control, which seemed to allow me to use two controls on the same underlying field, and somehow managed to avoid problems when I edited either: ... I've wondered about using an event handler to intercept the attempt to edit in order to present a popup form with separate date and time text boxes which are then combined correctly when this popup form is closed. ... What I wanted was to avoid users having to edit a date/time in "General ...
    (microsoft.public.access.gettingstarted)
  • Re: time calculation problem [OT]
    ... But Al's previous formula though it didnot work gave me the format i wanted. ... In the former I was using the DTPicker control, which seemed to allow me to use two controls on the same underlying field, and somehow managed to avoid problems when I edited either: ... I've wondered about using an event handler to intercept the attempt to edit in order to present a popup form with separate date and time text boxes which are then combined correctly when this popup form is closed. ... What I wanted was to avoid users having to edit a date/time in "General ...
    (microsoft.public.access.gettingstarted)
  • Re: time calculation problem
    ... It works better, in most situations, to have the date and time in one field and use functions such as DateValue and TimeValue to get separate date or time. ... Or Format to display a specific part of the dateTime value. ... In the former I was using the DTPicker control, which seemed to allow me to use two controls on the same underlying field, and somehow managed to avoid problems when I edited either: ... I've wondered about using an event handler to intercept the attempt to edit in order to present a popup form with separate date and time text boxes which are then combined correctly when this popup form is closed. ...
    (microsoft.public.access.gettingstarted)