回 帖 发 新 帖 刷新版面

主题:[讨论]紧急求助对象变量或with块变量未设置

搞了半天怎么都看不出哪里错了,帮忙下
Private Sub Command1_Click()
On Error GoTo errh
    If Text1.Text <> "" Then
       If Text2.Text <> "" Then
           If Text3.Text <> "" Then
             If checkclass Then
             Adodc1.Recordset.AddNew
                  Adodc1.Recordset.Fields("课程号").Value = Text1.Text
                  Adodc1.Recordset.Fields("课程名称").Value = Text2.Text
                  Adodc1.Recordset.Fields("授课老师").Value = Text3.Text
              Adodc1.Recordset.Update
               Text1.Text = ""
               Text2.Text = ""
               Text3.Text = ""
               Text1.SetFocus
               MsgBox "添加成功!"
               'Adodc1.Refresh
             Else
                MsgBox "课程重复,请另外输入一门课程!"
                Text1.SetFocus
             End If
           Else
              MsgBox "该项不能为空,请输入授课老师姓名!"
           End If
      Else
         MsgBox "该项不能为空,请输入课程名称!"
      End If
   Else
       MsgBox "该项不能为空,请输入课程号!"
   End If         
   Exit Sub
errh:
    MsgBox Err.Description
End Sub
运行后出现错误“对象变量或with块变量未设置”
错误定位在adodc1.recordset.addnew

adodc1的控件已经添加,也已经和数据库连接,测试成功的,但就是不知道为什么还是有错误,其他模块也是这样写的都没出错。
高手帮忙啊,这个搞不出,下面的就不好做了,先谢了

回复列表 (共2个回复)

沙发

数据库里似乎应该至少有一条记录才可以

板凳

Dim strFile As String
strFile = "your db file path"
[color=red]Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Persist Security Info=False;" & _
        "Data Source='" & strFile & "';" & _
        "Jet OLEDB:DataBase Password='';"
Adodc1.RecordSource = "select * from main"
Adodc1.Refresh
[/color]
Debug.Print Adodc1.Recordset.RecordCount

ADODC一般就这样用来绑定到数据库的
AddNew之前应确保recordSet是可用的(测试recordSet Is Nothing)
另外,使用ADODC还不如直接使用ADODB代码呢。

我来回复

您尚未登录,请登录后再回复。点此登录或注册