+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Dec 2007
    Posts
    130

    Default VBA: Number formatting

    Registered forum members do not see this ad.

    Hey all,

    I appreciate this is more VB than VBA but its an AutoCAD tool i'm making so posting it here too..

    Trying to creating a tool that inserts datum lines in a drawing which is fine, the only issue is the formatting of the attribute to display within the block (see the bold part of the code below). I'm trying to display the datum level as "+0.000" which is fine for most input but if the number has any trailing zeros, they are not displayed. I've searched high and low on the net for this and have tried everything i have found but still no results..

    (All variables in the code have been declared and no errors occur at run-time, just not the correct display:
    ie: "+6.800" displays in the block as "+6.8" and "+8.760" displays as "+8.76")
    Code:
    '*************************************************
    '************ GROUND FLOOR ***************
    '*************************************************
    GROUND:
    ' Check if GROUND is selected..
    If LVLtxtGRND.Text = "" Then
        GoTo FIRST
        Else 'If a value is shown then insert the block..
        LVLvalueGRND = LVLtxtGRND.Text
        ' Draw LINE for level..
        LineGRNDstart(0) = 0: LineGRNDstart(1) = LVLvalueGRND: LineGRNDstart(2) = 0
        LineGRNDend(0) = LINEend: LineGRNDend(1) = LVLvalueGRND: LineGRNDend(2) = 0
        Set LINEgrnd = ThisDrawing.ModelSpace.AddLine(LineGRNDstart, LineGRNDend)
        LINEgrnd.Layer = "X-Levels"
        ' Insert LEVEL Block..
        LVLinsertGRND(0) = 0: LVLinsertGRND(1) = LVLvalueGRND: LVLinsertGRND(2) = 0
        Set LVLblockGRND = ThisDrawing.ModelSpace.InsertBlock(LVLinsertGRND, "Level", 250#, 250#, 250#, 0)
        LVLblockGRND.Layer = "X-Notes"
        
    ' Display level figure on screen..
    LVLvalueGRND = LVLvalueGRND / 1000   'Get it into metres, no mm..
    LVLvalueGRND = Round(LVLvalueGRND, 3)
    LVLvalueGRND = Format(LVLvalueGRND, "0.000")
    
    AttribZ = LVLblockGRND.GetAttributes ' Get Block attributes..
                    
    For CountX = LBound(AttribZ) To UBound(AttribZ)
        Select Case AttribZ(CountX).TagString
        Case "LEVEL"
            AttribZ(CountX).TextString = PlusChar & LVLvalueGRND & " " & " " & DESCcomboGRND.Text
        End Select
    Next CountX
    
    'On Error Resume Next
    '    MsgBox Err.Number & vbCr & Err.Description, vbExclamation, "Error:"
    End If
    '*************************************************
    '************ GROUND FLOOR ***************
    '*************************************************
    Any ideas? This is twisting my melon now grrr
    Cheers,

    PaulRenegade @ hardwiredstudios..
    XP Pro / AutoCAD 2007 : Pentium Core 2 Q6600 Quad Core : ASUS P5N-E SLI Motherboard : 4GB DDR2 RAM : 2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards

  2. #2
    Senior Member
    Using
    not applicable
    Join Date
    Nov 2006
    Location
    Melbourne Australia
    Posts
    185

    Default

    Baffling. Shouldn't need to do this but as a real kludge until something better comes along you could compare the INSTR position of the decimal point with the string's length and append any needed zeroes. If no decimal point append ".000" to the string.

  3. #3
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, Vista and XP Pro
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2013
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    889

    Default

    Quote Originally Posted by hardwired View Post
    Code:
    LVLvalueGRND = Format(LVLvalueGRND, "0.000")
    To get the trailing zeros you need to rewrite the above line so it reads:

    Code:
    LVLvalueGRND = Format(LVLvalueGRND, "0.##0")
    I hope that helps
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  4. #4
    Senior Member
    Using
    not applicable
    Join Date
    Nov 2006
    Location
    Melbourne Australia
    Posts
    185

    Default

    Quote Originally Posted by Tyke View Post
    To get the trailing zeros you need to rewrite the above line so it reads:

    Code:
    LVLvalueGRND = Format(LVLvalueGRND, "0.##0")
    I hope that helps
    If that works we all learn something. I hope someone can explain why it should work, either by example or by arguing from first principles.

  5. #5
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, Vista and XP Pro
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2013
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    889

    Default

    I've used it a lot for this very situation in both VBA and VB .NET and it works fine. It was a real pain in the backside for me until I found the solution try it out and get back to us with your result.
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  6. #6
    Senior Member
    Using
    not applicable
    Join Date
    Nov 2006
    Location
    Melbourne Australia
    Posts
    185

    Default

    It works for sure. Sorry, I was just idly curious as to how you came upon the answer - it's not well documented by MS e.g:
    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

  7. #7
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, Vista and XP Pro
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2013
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    889

    Default

    Glad it worked for you hugha.

    You are right it is not very well documented and I saw a similar link to the one you just posted, after which I just experimented a bit till I got it to work. The # symbol is a standard place holder in Visual Basic, although the documentation says that "0.000" should work, but it never did for me. I have been using it regularly now for around six years. Let's hope that it works for hardwired too

    Thanks for getting back hugha and
    If that works we all learn something.
    that we have all learned a little bit.
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  8. #8
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Dec 2007
    Posts
    130

    Default

    Hey all,

    Thanks for all your replies. I tried the '0.##0' solution, Tyke but still no luck. I think my PC doesn't like to be too negative haha. This is a real head-screwer as it seems to be working for everyone else just not me. I'll give your first solution a go, hugha and see what happens
    Cheers,

    PaulRenegade @ hardwiredstudios..
    XP Pro / AutoCAD 2007 : Pentium Core 2 Q6600 Quad Core : ASUS P5N-E SLI Motherboard : 4GB DDR2 RAM : 2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards

  9. #9
    Super Member Tyke's Avatar
    Computer Details
    Tyke's Computer Details
    Operating System:
    Windows 7 - 64 bit, Vista and XP Pro
    Computer:
    HP Z400 Workstation
    CPU:
    Intel(R) Xeon(R) CPU W3530 @ 2.80GHz
    RAM:
    8 GB
    Graphics:
    Nvidia Quadro 600 1GB DDR3
    Primary Storage:
    2x 500GB HDD RAID 1 and Western Digital 2TB NAS RAID 1
    Secondary Storage:
    Lacie external HDD 2TB Raid Level 1
    Monitor:
    Samsung SyncMaster P2770HD, 2443 and 193T
    Discipline
    Multi-disciplinary
    Tyke's Discipline Details
    Occupation
    Surveyor, programmer, civil engineer
    Discipline
    Multi-disciplinary
    Details
    Surveying and civil engineering. As built utilities surveys and data maintenance in GIS systems. Building surveys, measured and laser scanning. Setting out of all civil works. Control and settlement surveys. Programming in AutoCAD, MicroStation, Geograf and stand alone applications for all uses.
    Using
    Civil 3D 2013
    Join Date
    Jun 2006
    Location
    Saxony, Germany
    Posts
    889

    Default

    I've tried out exactly the code you have on two computers, in AutoCAD 2010 and 2013 in both German and English language versions of AutoCAD, but admittedly only on a German Windows XP operating system. It works fine with your unaltered code and also with my suggestion.

    What OS do you have? 32 bit or 64 bit? It shouldn't make any difference, but check the decimal seperator in your Regional Settings.
    Engage brain before mouth

    "The German who types with a Yorkshire accent" - Dave Broada 2o1o

  10. #10
    Senior Member
    Using
    AutoCAD 2008
    Join Date
    Dec 2007
    Posts
    130

    Default

    Registered forum members do not see this ad.

    Hey Tyke, I've had a look at my regional settings and can't see anything else to change except figures to 3 decimal places but that didn't work anyway..

    I'm on AutoCAD 2010 on an 32-bit XP Pro machine by the way..
    Cheers,

    PaulRenegade @ hardwiredstudios..
    XP Pro / AutoCAD 2007 : Pentium Core 2 Q6600 Quad Core : ASUS P5N-E SLI Motherboard : 4GB DDR2 RAM : 2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards

Similar Threads

  1. Replies: 4
    Last Post: 29th Dec 2010, 06:28 pm
  2. Formatting Help
    By TravC2121 in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 3
    Last Post: 17th Dec 2009, 10:18 pm
  3. Table formatting
    By blatz.boy in forum AutoCAD General
    Replies: 0
    Last Post: 27th Jun 2008, 07:08 pm
  4. Formatting in MTEXT
    By Aries7 in forum AutoCAD Drawing Management & Output
    Replies: 12
    Last Post: 27th Dec 2006, 08:55 pm

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