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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Time Error '6': Overflow ? 1

Status
Not open for further replies.

JohnVogel

Programmer
Apr 19, 1999
117
US
Hi, I am trying to simply get the free disk space of Drive C, but it seems no matter what method I use I either Get an overflow, or I get 0k. Here's basically what I have:

-------------------Begin Code-------------------------
Private Declare Function GetDiskFreeSpace Lib "kernel32.dll" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long


Function FreeDiskSpace(DriveLetter As String)
Dim SectorsPerCluster As Long
Dim BytesPerSector As Long
Dim FreeClusters As Long
Dim NumberOfClusters As Long
Dim ret As Long
ret = GetDiskFreeSpace(DriveLetter & ":\", SectorsPerCluster, BytesPerSector, FreeClusters, NumberOfClusters)
FreeDiskSpace = BytesPerSector * SectorsPerCluster * FreeClusters
End Function


Private Sub Command1_Click()

MsgBox FreeDiskSpace("C") \ 1024 & " Kb Free Space in Drive C"

End Sub

----------------------End Code------------------

I am using this on Windows XP, and C: drive is FAT32 (don't know if that makes any diff)... if I use Clng() I can get past the Run Time Errror, but then I get a result of 0 kilobytes.

Any help is greatly appreciated :)

John Vogel
john@thecompuwizard.com
 
change the direction of your "\" (backslash) to a "/" forward slash and then change this statement

Code:
FreeDiskSpace = BytesPerSector * SectorsPerCluster * FreeClusters

to

Code:
FreeDiskSpace = CDbl(BytesPerSector) * CDbl(SectorsPerCluster) * CDbl(FreeClusters)

and then declare your function as Double. Like this:

Code:
Public Function FreeDiskSpace(DriveLetter As String) As Double

This should do the trick
 
bjd4jc,

FANTASTIC! Cdbl, eh? Whodathunk it! Well, you for one :-D

Needless to say, it did the trick... though I didn't have to declare the function as a double. Actually I tried that first, and it gave me a message that the expression was too complex, changed it back to Long and now everything's working GREAT!

Can you tell me... why did I need to do that?

John Vogel
john@thecompuwizard.com
 
You were getting an overflow error becuase the result being returned was too large to fit into a Long variable type. See MSDN for the size limits of the different variable types.

VB was implicitly forcing the equation into a memory space the size of Long. And since the result of the error was larger than what Long allows, you were getting an error. So, when you explicitly convert the variables to Double (which has larger precision and which you can not declare them as because of the API call) VB puts the result into a Double size of memory. I am not the best at explaining these things so maybe someone else wants to jump in to explain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top