Error - Expected Function or Varible

I am trying to get an object to work.  It goes out and gets the directory size.  Here the code it have so far.  Someone Please help me.   This is my first object I have created, I have no idea what the problem is...Thanks

Public spath As Class1
Public dtotal As Class1

Private Sub CmdGetSize_Click()
      Dim DirectorySize As Class1
      Dim spath As String
      Dim dtotal As Double
      dtotal = 0
      spath = Text1.Text
      dtotal = 0
      dtotal = 0
      total = DirectorySize.DirSize(spath, dtotal)
      Text1.Text = DirectorySize
End Sub

Here is Model1 code

Public dtotal As Double
Public spath As String

Here is Class1 code

Public spath As String
Public dtotal As Double

Public Sub DirSize(spath As String, dtotal As Double)

       Dim sDir          As String
       Dim colDir        As New Collection
       Dim nI            As Integer
       Dim dLocalTotal   As Double

       If Right(spath, 1) <> "\" Then spath = spath & "\"
       dLocalTotal = 0
       ' *** Get all the files
       sDir = Dir(spath & "*.*", vbArchive + vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
       Do While sDir <> ""
          ' *** If it is a directory, ignore it
          If (GetAttr(spath & sDir) And vbDirectory) <> vbDirectory Then
             dLocalTotal = dLocalTotal + FileLen(spath + sDir)
             If (Left(sDir, 1) <> ".") Then colDir.Add sDir
          End If
          sDir = Dir()   ' *** Get next entry

       ' *** Get all the directorie
       For nI = 1 To colDir.Count
          Call DirSize(spath & colDir(nI), dLocalTotal)
       Set colDir = Nothing
       dtotal = dtotal + dLocalTotal
       DirSize = dLocalTotal

    End Sub

Who is Participating?
schildConnect With a Mentor Commented:
You have two problem over here
a major and a minor one

The major is:
Insted of lines:
Public spath As Class1
Public dtotal As Class1

you must put those:
Public spath As New Class1
Public dtotal As New Class1
This will sole you the run time error that you got.
The "New" for a object instance in needed, exaclly as you doen with "Collection" object.

The minor problem is that ambiguase declaration of variables:
Public spath As String
Public dtotal As Double
Public Sub DirSize(spath As String, dtotal As Double)

The result is that inside "DirSize" subrutine, the function recognize only the local variable and not the globals.

Good luck with your first Object
This is a duplicate question.

You should delete it.
You are incorrect.
By using a public variable in a form, it becomes a property of that form.  This removes the ambiguity.

Also, as I noted in the answer to the question that this is a duplicate of, the problem is that DirSize is declared as a sub and should be declared as a function.

As this is a duplicate question, you should consider deleting it and save yourself some points.
strongdAuthor Commented:
Thanks.....It is working finally......
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.