Results 1 to 4 of 4

Thread: FormatNumber function in VBA???

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54

    FormatNumber function in VBA???

    I need to set the length of a variable based on user input in Access VBA. I'm using the function FormatNumber to do that in VB 6.0. The formatnumber function works like this:

    FormatNumber(1.1, 5) = 1.100
    FormatNumber(2.45, 3) = 2.5
    etc.

    Is there a similar function in VBA that performs the same task???

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Access (and all Office VBA) has something similar to NumberFormat
    in VB6. Its NumberFormat.

    What version of Access are you running? Maybe its an earlier
    version that doesnt have it for some reason? But it should be
    there in all versions.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    54
    I'm running Access 97 and it doesn't work. ??? It's no problem. I found another way to accomplish the same task:

    dim numlength as integer
    dim numstring as string

    do while len(numstr) < numlength
    numstring = numstring & "0"
    loop

    dim thenum as double
    thenum = 1.1

    thenum = format(thenum, "0." & numstring)

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Ok, dont have 97 anymore so couldnt test. Only have 2000, 2002,
    2003. Its in all of those. You know that having the var as a double, it will not round like the NumberFormat function does?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim thenum As Double
    5.     thenum = 2.45
    6.     thenum = Format(thenum, "0.0000") '=2.4500 and not 2.5000
    7. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width