选课系统
Dim Con As New ADODB.Connection '建立connenction对象,连接数据库Dim Res As New ADODB.Recordset
Dim Sqln As String, Sqlv As String, Sqlclassname As String
Dim Sql1 As String, Sql2 As String
Dim Sqld As String
Dim i As Long
Dim j As Integer
Private Sub Command1_Click() '添加记录
For i = 0 To Res.Fields.Count - 1
Text1(i).Text = ""
Next i
End Sub
Private Sub Command10_Click() '显示全部记录
Set MSHFlexGrid1.DataSource = Res
End Sub
Private Sub Command2_Click() '删除记录
For i = 0 To Res.Fields.Count - 1
Sqld = Sqld & Res.Fields(i).Name & "='" & Res.Fields(i).Value & "'and "
Next i
Sqld = Left(Sqld, Len(Sqld) - 4)
Con.Execute "DELETE FROM 选课表 WHERE " & Sqld
CloseConn
End Sub
Private Sub Command3_Click() '修改记录
For i = 0 To Res.Fields.Count - 1
Sql1 = Sql1 & Res.Fields(i).Name & "='" & Text1(i).Text & "',"
Sql2 = Sql2 & Res.Fields(i).Name & "='" & Res.Fields(i).Value & "'and "
Next i
Sql1 = Left(Sql1, Len(Sql1) - 1) '返回 Variant (String),其中包含字符串中从左边算起指定数量的字符。
Sql2 = Left(Sql2, Len(Sql2) - 4)
Con.Execute "UPDATE 选课表 SET " & Sql1 & "WHERE " & Sql2
CloseConn
End Sub
Private Sub Command4_Click() '保存记录
If Text1(0).Text = "" Then
MsgBox "姓名不能为空!", , Form1.Caption
Exit Sub
End If
For i = 1 To Res.Fields.Count - 1
Sqln = Sqln & "," & Res.Fields(i).Name
Sqlv = Sqlv & ",'" & Text1(i).Text & "'"
Next i
Con.Execute "INSERT INTO 选课表 (姓名" & Sqln & ") VALUES('" & Text1(0).Text & "'" & Sqlv & ")"
MsgBox "添加完成。", , Form1.Caption
CloseConn
End Sub
Private Sub Command5_Click() '下一条记录
Res.MoveNext
If Res.EOF Then Res.MoveLast
MSHFlexGrid1_Click
End Sub
Private Sub Command6_Click() '上一条记录
Res.MovePrevious
If Res.BOF Then Res.MoveFirst
MSHFlexGrid1_Click
End Sub
Private Sub Command7_Click() '第一条记录
Res.MoveFirst
MSHFlexGrid1_Click
End Sub
Private Sub Command8_Click() '最后一条记录
Res.MoveLast
MSHFlexGrid1_Click
End Sub
Private Sub Command9_Click()
End
End Sub
Private Sub Form_Load() '连接数据库
Con.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=选课表;Data Source=507-S207"
Res.Open "select * from 选课表", Con, adOpenKeyset, adLockOptimistic
CloseConn
For i = 0 To Res.Fields.Count - 1
Text1(i).Text = ""
Text1(i).Text = "*********"
Next i
End Sub
Private Sub CloseConn() '关闭数据库
Res.Close
Res.Open
If Res.RecordCount > 0 Then
MSHFlexGrid1_Click
End If
End Sub
Private Sub MSHFlexGrid1_Click() '在表里点击,在Text上面显示
For i = 0 To Text1.UBound
If Res.Fields(i).Value <> "" Then
Text1(i).Text = Res.Fields(i).Value
Else
Text1(i).Text = ""
End If
Next i
End Sub
为什么我的每个操作只能进行一次啊,能不能通行进行多个操作啊?谁来帮帮我