运行时错误91:链接VB6和MS访问时未设置对象变量或块变量

时间:2017-07-26 07:33:38

标签: vb6

当我运行给定的原始代码时,会显示以下行中的错误"运行时错误91"

con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"

原始代码

Dim con As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub SUBMIT_Click()
con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"
rs.Open "select DBTB1 from prac1", con, adOpenDynamic, adLockPessimistic

rs.Fields("NUMBER").Value = Text1.Text
rs.Fields("NAME").Value = Text2.Text
rs.Fields("CITY").Value = Text3.Text
MsgBox "data saved!", vbInformation
rs.Update

End Sub

1 个答案:

答案 0 :(得分:0)

您收到错误91,因为您实际上没有创建Connection对象。此外,您将获得与RecordSet相同的错误。我已更新您的代码以使其正常工作:

Private Sub SUBMIT_Click()
   Set con = New ADODB.Connection
   con.Open "Provider=Microsoft.Jet.4.0;Data Source=C:\Documents and Settings\XPMUser\Desktop\New Folder\prac1.mdb; Persist Security Info = False"
   Set rs = New ADODB.Recordset
   rs.Open "select DBTB1 from prac1", con, adOpenDynamic, adLockPessimistic

   rs.AddNew
   rs.fields("NUMBER").value = Text1.Text
   rs.fields("NAME").value = Text2.Text
   rs.fields("CITY").value = Text3.Text
   rs.Update
   MsgBox "data saved!", vbInformation
End Sub

另外,请注意在更新数据库之前添加AddNew