运行时出现错误:对象变量或with块变量未设置
怎么改啊
请高手帮我看看 谢拉(这是学籍管理系统中添加新课程的窗口)
Dim rs As New ADODB.Recordset
Dim SD As Boolean
Private Function checkclass() As Boolean '检测添加的课程是否重复
Set rs = New ADODB.Recordset
Set rs = cn.Execute("SELECT 课程号 FROM 课程 WHERE 课程号='" & Trim(Text1.Text) & "'")
If rs.EOF Then
rs.Close
checkclass = True
Exit Function
End If
checkclass = False
rs.Close
Exit Function
End Function
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
Private Sub Command15_Click()
Unload Me
End Sub
Private Sub Command2_Click()
On Error GoTo errh
If MsgBox("你的操作将会删除当前的纪录,你确信吗?", vbOKCancel, "警告") = vbOK Then
If Text1.Text <> "" Then
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM 课程 WHERE 课程号='" & Trim(Text1.Text) & "' ", cn, adOpenDynamic, adLockOptimistic, adCmdText
If rs.EOF Then
rs.Close
MsgBox "没有该课程,无法删除!", vbOKOnly + vbExclamation, "警告"
Text1.Text = ""
Exit Sub
End If
rs.Delete
rs.Update
rs.Close
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
MsgBox "删除成功!"
Else
MsgBox "该项不能为空,请输入课程号!"
End If
End If
Exit Sub