I have a function that should return an object of a class. The function looks like this:
Public Function GetPerson(personID As Integer) As clsPerson
Dim sqlAdapter As New clsSQLAdapter
Dim person As New clsPerson
Dim recordset As recordset
query = "SELECT ID, Anrede_ID, Nachname, Vorname FROM Person WHERE Person.ID = " & personID
Set recordset = sqlAdapter.recordset(query)
person.personID = recordset.Fields(0).Value
If (recordset.Fields(1).Value = kAnrede.FRAU) Then
Set person.anrede = kAnrede.FRAU
ElseIf (recordset.Fields(1).Value = kAnrede.HERR) Then
Set person.anrede = kAnrede.HERR
End If
person.nachName = recordset.Fields(2).Value
person.vorName = recordset.Fields(3).Value
Set GetPerson = person
End Function
But when I call the the function from another module, I get an error 91 at the line
person = sqlController.GetPerson(1)
stating: "Object variable or with block variable not set":
Private Sub Button_Click()
Dim sqlController As New clsSQLController
Dim person As New clsPerson
person = sqlController.GetPerson(1)
End Sub
Also when I debug the programm, it tells me that the Type of sqlController.GetPerson(1) is Integer. Is it even possible to do this, or did I miss something?
Okay so I got the answer by myself. It is not that hard if you know it, I guess.
With Public Function GetPerson(personID As Integer) As clsPerson
you seem to only declare the function type as clsPerson, but the function has no instance of clsPerson
.
So you need to create an instance inside the function, like this:
Public Function GetPerson(personID As Integer) As clsPerson
Set GetPerson = New clsPerson '<- This line is the important part
'All the other code
Set GetPerson = person
End Function