What does this VB6 error mean?
Run Time Error '91':
Object variable or With block variable not set
I am using VB6
OK here is my code
Private Sub Form_Load()
lblIDNumber.Caption = UserID
With datPersonal.Recordset
.Index = "idxid"
.Seek "=", UserID
LockTextboxes
End With
End Sub
Public Sub LockTextboxes()
With txtDateHired
.Locked = True
End With
With txtBirthday
.Locked = 开发者_JS百科True
End With
With txtGender
.Locked = True
End With
With txtAddress
.Locked = True
End With
With txtContact
.Locked = True
End With
With txtStatus
.Locked = True
End With
With txtPosition
.Locked = True
End With
With txtBasicSalary
.Locked = True
End With
With txtReligion
.Locked = True
End With
End Sub
It usually occurs when you call a method of an object which is not set.
You have not initialized datPersonal.Recordset.
It means you are trying to use a variable, but haven't set the variable to any value. Specfically, the variable references an object rather than a value type.
Usual cause is doing something like Dim obj As SomeClass
rather than Dim obj As New SomeClass
, i.e. failing to set the variable to a reference to an object before using it.
maybe the problem was solved already in another way, but you could try to move the code from form_load to form_activate
You need to initialise classes before they can be used. Assuming datPersonal.Recordset is actually a Recordset class then you will need to do something like the following:
Set datPersonal.Recordset = New ADODB.Recordset
You also need to make sure that you have added a reference to the ActiveX Data Objects in you project.
Have a look at the following:
Add reference to ADO
Using a recordset
From your previous questions I am assuming that the datPersonal refers to a ADO Datacontrol. The connectionstring and selection criteria of the datacontrol can be set at design-time and when the application is started, a connection and recordset is automatically opened.
The fact that you are getting this error indicates either that the connectionstring and selection was not specified or that the following code was executed :
Set datPersonal.Recordset = Nothing
精彩评论