这个 VB6 错误是什么意思?
运行时错误'91':
对象变量或With块变量未设置
我正在使用VB6
OK这是我的代码
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 = 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
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 = 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您调用未设置的对象的方法时,通常会发生这种情况。
It usually occurs when you call a method of an object which is not set.
您尚未初始化 datPersonal.Recordset。
You have not initialized datPersonal.Recordset.
这意味着您正在尝试使用变量,但尚未将该变量设置为任何值。具体来说,变量引用对象而不是值类型。
通常的原因是执行诸如
Dim obj As SomeClass
而不是Dim obj As New SomeClass
之类的操作,即在使用变量之前未能将变量设置为对对象的引用。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 thanDim obj As New SomeClass
, i.e. failing to set the variable to a reference to an object before using it.也许问题已经以另一种方式解决了,但是您可以尝试将代码从 form_load 移至 form_activate
maybe the problem was solved already in another way, but you could try to move the code from form_load to form_activate
您需要先初始化类,然后才能使用它们。假设 datPersonal.Recordset 实际上是一个 Recordset 类,那么您需要执行如下操作:
您还需要确保已添加对项目中 ActiveX 数据对象的引用。
查看以下内容:
添加对 ADO 的引用
使用记录集
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:
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
根据您之前的问题,我假设 datPersonal 指的是 ADO Datacontrol。
数据控件的连接字符串和选择标准可以在设计时设置,并且当应用程序启动时,会自动打开连接和记录集。
您收到此错误的事实表明未指定连接字符串和选择或者执行了以下代码:
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 :