Alan
Alan

Reputation: 822

object variable not set error - but it is

I'm getting the Object Not Set Error message in the subroutine named ExportTimeSheetsToDatabase().

Try to follow along:

  1. The TimeSheetCollection is a collection of type TimeSheet
  2. First I call the subroutine ReadWeeklyTimeSheets() which sets the TimeSheetCollection object, reads in todays mailitems, and from the values obtained from the body of the mailitem we set the properties in each TimeSheet. I've added a shortened version of the TimeSheet class for clarity.
  3. Next, the subroutine named ExportTimeSheetsToDatabase() is called. It is here in the Debug.print statement that I get the error message on the Item.MondayStart and Item.MondayEnd properties. If I were to include all the other properties Tuesday to Friday, they would all be complaining with the same error message.

I don't understand why I'm getting the error at all! In the subroutine ReadWeeklyTimeSheets(), I'm making calls like: tsheet.MondayStart = MonStart

In the class TimeSheet the property for pMonStart is set with:

Public Property Let MondayStart(Value As TimeFrame)
  Set pMonStart = Value
End Property

So I know that it is being set. Why is it then in the ExportTimeSheetsToDatabase() subroutine I'm getting this error? You'll notice in the subroutine ReadWeeklyTimeSheets() I do a Debug.print statment that calls tsheet.ToString. That prints off just fine all the properties in tsheet. I'm accessing the same TimeSheet object in the collection when I call Debug.print in the ExportTimeSheetsToDatabase() subroutine.

Please advise,

Alan

'Global variable
Public TimeSheetCollection As Collection
...
Sub ReadWeeklyTimeSheets()
  Dim tsheet As TimeSheet
  Dim kvPairs As Collection
  ...
  Set TimeSheetCollection = New Collection
  Dim DefaultTF As TimeFrame
  'set the default TimeFrame
  Set DefaultTF = New TimeFrame
  DefaultTF.Initialize = "00:00"
  ...
  For Each oitem In ItemsToProcess
    If TypeName(oitem) = "MailItem" Then
     Set myMailItem = oitem
     Debug.Print "Subject: " & myMailItem.Subject
     If CheckSubject(myMailItem.Subject, TimeSheetSubjectTitle) Then
       Set kvPairs = GetTimeSheetKeyValuePairs(myMailItem.body)
       'Iterate over the Collection and load up
       'an instance of TimeSheet object
       Set tsheet = New TimeSheet
       For Each Item In kvPairs

         If LCase(Item.Key) = LCase("EmployeeID") Then
           tsheet.EmployeeID = Item.Value
         ElseIf LCase(Item.Key) = LCase("StartDate(DD/MM/YYYY)") Then
            tsheet.StartDate = CDate(Item.Value)
         ElseIf LCase(Item.Key) = LCase("EndDate(DD/MM/YYYY)") Then
            tsheet.EndDate = CDate(Item.Value)
         ElseIf LCase(Item.Key) = LCase("MonStart") Then
            If Item.Value <> "" Then
              Set MonStart = New TimeFrame
              MonStart.Initialize = Item.Value
              tsheet.MondayStart = MonStart  '<<<Calling this sets the object in the TimeSheet
            Else
              tsheet.MondayStart = DefaultTF
            End If
         ElseIf LCase(Item.Key) = LCase("MonEnd") Then
             If Item.Value <> "" Then
             Set MonEnd = New TimeFrame
             MonEnd.Initialize = Item.Value
             tsheet.MondayEnd = MonEnd  '<<<Calling this sets the object in the TimeSheet
         Else
             tsheet.MondayEnd = DefaultTF
         End If
       ElseIf ... 'And so on thru to Friday

       End If
      Next Item

      'Add each Time Sheet object to the TimeSheetCollection
      TimeSheetCollection.Add tsheet

      Debug.Print tsheet.ToString  '<<<The TimeSheet object prints everything just fine
    End If
   End If
 Next oitem     
End Sub

_______________________________

Sub ExportTimeSheetsToDatabase()
  Dim Item As TimeSheet
  Dim strInsertQuery As String

  'Iterate over the collection to obtain each TimeSheet object
  'and insert the data from each as a new record into the database
  For Each Item In TimeSheetCollection
   'THIS DEBUG STATEMENT IS WHERE I GET THE ERROR OBJECT NOT SET
   'ON Item.MondayStart and Item.MondayEnd
   Debug.Print Item.EmployeeID & ", " & Item.StartDate & ", " & Item.EndDate & "," & Item.MondayStart & "," & Item.MondayEnd & ", Toal Hours: " & Item.TotalWeeklyHours
  Next Item

 End Sub
_______________________________
'CLASS MODULE TIMESHEET
Private pEmployeeID As Integer
Private pStartDate As Date
Private pEndDate As Date
Private pMonStart As TimeFrame
Private pMonEnd As TimeFrame
Private pMonBreak As Double
Private pTuesStart As TimeFrame
Private pTuesEnd As TimeFrame
Private pTuesBreak As Double
Private pWedStart As TimeFrame
Private pWedEnd As TimeFrame
Private pWedBreak As Double
Private pThursStart As TimeFrame
Private pThursEnd As TimeFrame
Private pThursBreak As Double
Private pFriStart As TimeFrame
Private pFriEnd As TimeFrame
Private pFriBreak As Double
Public Property Get EmployeeID() As Integer
   EmployeeID = pEmployeeID
End Property
Public Property Let EmployeeID(Value As Integer)
   If Value > 0 Then
       pEmployeeID = Value
   Else
     MsgBox "Employee ID " & Value & " is an incorrect value." & vbCrLf & "Employee ID must be a positive integer"
   End If

End Property
Public Property Get StartDate() As Date
 StartDate = pStartDate
End Property
Public Property Let StartDate(Value As Date)
 pStartDate = Value
End Property
Public Property Get EndDate() As Date
 EndDate = pEndDate
End Property
Public Property Let EndDate(Value As Date)
 pEndDate = Value
End Property
Public Property Get MondayStart() As TimeFrame
 MondayStart = pMonStart
End Property
Public Property Let MondayStart(Value As TimeFrame)
  Set pMonStart = Value
End Property
Public Property Get MondayEnd() As TimeFrame
 MondayEnd = pMonStart
End Property
Public Property Let MondayEnd(Value As TimeFrame)
 Set pMonEnd = Value
End Property
Public Property Get MondayBreak() As Double
  MondayBreak = pMonBreak
End Property

...

Public Property Get ToString() As String
   ToString = "EmployeeID = " & CStr(pEmployeeID) & vbCrLf & _
           "StartDate = " & CStr(pStartDate) & vbCrLf & _
           "EndDate = " & CStr(pEndDate) & vbCrLf & _
           "MondayStart = " & pMonStart.ToString & vbCrLf & _
           "MondayEnd = " & pMonEnd.ToString & vbCrLf & _
           "MondayBreak = " & CStr(pMonBreak) & vbCrLf & _
           "TuesdayStart = " & pTuesStart.ToString & vbCrLf & _
           "TuesdayEnd = " & pTuesEnd.ToString & vbCrLf & _
           "TuesdayBreak = " & CStr(pTuesBreak) & vbCrLf & _
           "WednesdayStart = " & pWedStart.ToString & vbCrLf & _
           "WednesdayEnd = " & pWedEnd.ToString & vbCrLf & _
           "WednesdayBreak = " & CStr(pWedBreak) & vbCrLf & _
           "ThursdayStart = " & pThursStart.ToString & vbCrLf & _
           "ThursdayEnd = " & pThursEnd.ToString & vbCrLf & _
           "ThursdayBreak = " & CStr(pThursBreak) & vbCrLf & _
           "FridayStart = " & pFriStart.ToString & vbCrLf & _
           "FridayEnd = " & pFriEnd.ToString & vbCrLf & _
           "FridayBreak = " & CStr(pFriBreak)
End Property

 __________________________

Upvotes: 0

Views: 715

Answers (1)

Alan
Alan

Reputation: 822

OK. So I went into the Class Module TimeSheet and changed all the Let properties to Set, like so:

Public Property Set MondayStart(ByRef Value As TimeFrame)
  Set pMonStart = Value
End Property

Public Property Set MondayEnd(ByRef Value As TimeFrame)
  Set pMonEnd = Value
End Property

Public Property Set TuesdayStart(ByRef Value As TimeFrame)
  Set pTuesStart = Value
End Property

Public Property Set TuesdayEnd(ByRef Value As TimeFrame)
  Set pTuesEnd = Value
End Property

 ... 'and so on

To prevent another "Object Not Set" error message when assigning a value, I had to again use the Set keyword:

 Set MonStart = New TimeFrame
 MonStart.Initialize = Item.Value
 Set tsheet.MondayStart = MonStart  '<<<Had to use Set here too. Is that normal???

Is that normal? It seems redundant to me that we use the Set keyword in the property declaration and the body of the property, then also have to use it when assigning a value to the property!

Once all the properties are set, and I call the subroutine ExportTimeSheetsToDatabase(), I'm still getting the error message Object Not Set on Item.MondayStart and Item.MondayEnd on the Debug.println statement. The other properties are holding their values like the Item.EmployeeID, Item.StartDate and Item.EndDate. But then they aren't objects. They're just primitive data types. I hope your able to see the problem, cause I can't see the forest for the trees on this one.

Alan

Upvotes: 1

Related Questions