Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format a Long in MsgBox

Status
Not open for further replies.

Tzokas

Programmer
Mar 2, 2001
24
GR
Hi to everyone!!!
I think this is something easy.
Suppose we have the following code:

Code:
Dim MyNum As Long
MsgBox  "The number is: " & MyNum

Now, i want the "MyNum" to be appeared with 2 decimal places.
Does anybody know how should i format it ?

Thanks
 
First, change it to [single | double | currency] then insert some value into the variable (e.g. MyNum = 22 / 7)
format the variable (e.g. Format(MyNum, ####0.#0) (the Zeroes are necessary to assure SOME integer value and two decimal places)

Thus (Revised)


Dim MyNum As single

[tab] MyNum = 2 * (22 / 7) * 4 ^ 2

[tab]MsgBox "The number is: " & Format(MyNum, ####0.#0)

This level of inquiry would indicate that you are VERY new to VB and probably to programming in general. While many members of Tek-Tips will take the time to answer questions as simple as this, you can find the answer much more quickly just by using the help system whih is supplied with VB. I would also advise the purchase (and use) of one of the many 'desktop' reference manuals. Some of them are in the form of a tutorial, but include sufficient 'indexing' to also serve as a reference.





MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
MichaelRed,
thank you for the reply, but let me do the problem clear.

First, it is not VB but VBA. Second, this is the real code:

Code:
Dim FileLength As Long
dim Message
   Open "C:\MyFile.MDB" For Input As #1 
FileLength = LOF(1) 
Message= "FileSize: " & (FileLength) / 1050624 & " MB"
MsgBox Message
   Close #1    ' Close file.

I can't set the "FileLength" as Single because the LOF returns always Long. Even this could be set as you mentioned above, the Format doesn't work.

Nikos Tzokas
prooptiki@the.forthnet.gr
 

Dim FileLength As Long
Dim Message As String
Dim MyFil As Single
[tab]Open "C:\MyFile.MDB" For Input As #1
[tab]FileLength = LOF(1)
[tab]MyFil = LOF(1) / 1050624
[tab]Message= "FileSize: " & Format(MyFil, #####0.#0") & " MB"
[tab]MsgBox Message
[Tab]Close #1 ' Close file.


Or, somewhat more completly:

Code:
Public Function basFilSize()

    Dim FileLength As Long
    Dim Message As String
    Dim MyFilSize As Single
    Dim MyFileNAme As String
    Dim MyFil As Integer
    Dim MyTitle As String

    MyFilName = CurrentDb.Name
    MyFil = FreeFile

    Open MyFilName For Input As #MyFil
    FileLength = LOF(1)
    MyFilSize = Format(LOF(1) / 1050624, "#####0.#0")
    Message = "File Size: " & MyFilSize & " MB"
    MyTitle = "CurrentDb Size"
    MsgBox Message, vbOKOnly, MyTitle
    Close #MyFil    ' Close file.

End Function


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Thanks MichaelRed,
it now works perfectly


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top