向数据库写入数据提示操作符丢失?
相关代码如下:
Private Sub Form_Activate()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\data.mdb "
conn.Open
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "course ", conn, 1, 1
Set rsCourse = New ADODB.Recordset '设置链接授课信息
rsCourse.CursorLocation = adUseClient
rsCourse.Open "course ", conn, 2, 2
Set DataGrid1.DataSource = rsCourse
DataGrid1.Refresh
Set DataGrid2.DataSource = rsCourse
DataGrid2.Refresh
Set rsteachers = New ADODB.Recordset '连接教师信息
rsteachers.CursorLocation = adUseClient
rsteachers.Open "teachers ", conn, 2, 2
Set txtName.RowSource = rsteachers
txtName.ListField = "姓名 "
Set rsKC = New ADODB.Recordset '连接课程
rsKC.CursorLocation = adUseClient
rsKC.Open "xsKC ", conn, 1, 1
Set txtKC.RowSource = rsKC
txtKC.ListField = "课程名称 "
Set rsZY = New ADODB.Recordset '设置链接的专业
rsZY.CursorLocation = adUseClient
rsZY.Open "xsZY ", conn, 1, 1
Set txtZY.RowSource = rsZY
txtZY.ListField = "专业名称 "
Set txtzhuanye.RowSource = rsZY
txtzhuanye.ListField = "专业名称 "
Set TextJSXM.DataSource = rsCourse '绑定字段
Set TextSSZY.DataSource = rsCourse
Set TextKCM.DataSource = rsCourse
TextJSXM.DataField = "教师姓名 "
TextSSZY.DataField = "所属专业 "
TextJSXM.DataField = "课程名称 "
End Sub
---------------------------
Sub inputcourse_info() '添加授课信息到库
Dim txtSQL As String
txtSQL = "select * from course where 教师姓名= ' " & Trim(txtName.Text) & " ' "
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open txtSQL, conn, 1, 1
If rs.EOF = False Then
MsgBox "数据库已经存在教师姓名为: " & txtName.Text & "的教师授课信息,请重新输入! ", vbOKOnly, "警告 "
txtName.Text = " "
txtKC.Text = " "
txtZY.Text = " "
Else
rsCourse.AddNew
rsCourse.Fields(0) = txtName.Text
rsCourse.Fields(1) = txtKC.Text
rsCourse.Fields(2) = txtZY.Text
End If
rsCourse.Update
DataGrid1.Refresh
DataGrid2.Refresh
MsgBox "教师授课信息已经添加成功! ", vbOKOnly + vbInformation, "提示 "
txtName.Text = " "
txtKC.Text = " "
txtZY.Text = " "
End Sub
-------------------------
不知道那里的错误
[解决办法]
Else
rsCourse.AddNew '---这个没定义,rsCourse定义在Form_Activate(),你在窗体最开始加上Option Explicit
rsCourse.Fields(0) = txtName.Text
rsCourse.Fields(1) = txtKC.Text
rsCourse.Fields(2) = txtZY.Text
End If
rsCourse.Update
DataGrid1.Refresh
DataGrid2.Refresh