vb所有代码Vb核心代码设计
一:输入界面与代码(以班级表为例)
Dim Conn As New ADODB.Connection '连接变量
Dim SqlString As String '查询字符串
Dim Connstring As String '连接字符串
Dim RS1 As New ADODB.Recordset '记录集变量
Dim bm '定义一个变量用来存放添加记录时原来记录的位置
Dim tj As Boolean
Private Sub Command10_Click()
Form4.Hide
Form3.Show
End Sub
Private Sub Command1_Click() '移到首记录
RS1.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = True
Command4.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command2_Click() '前移
RS1.MovePrevious
If RS1.BOF Then
RS1.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
End If
Command3.Enabled = True
Command4.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command3_Click() '后移
RS1.MoveNext
If RS1.EOF Then
RS1.MoveLast
Command3.Enabled = False
Command4.Enabled = False
End If
Command1.Enabled = True
Command2.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command4_Click() '移到末记录
RS1.MoveLast
Command3.Enabled = False
Command4.Enabled = False
Command1.Enabled = True
Command2.Enabled = True
Call DispValue '显示当前记录的值
End Sub
Private Sub Command5_Click() '删除记录
If (MsgBox("真的要删除该记录吗?", vbQuestion + vbYesNo, "删除提示") = vbYes) Then
RS1.Delete '删除
RS1.MoveNext
If RS1.EOF Then '如果删除的是末记录
RS1.MoveLast
End If
End If
Call DispValue '显示当前记录的值
End Sub
Private Sub Command6_Click() '添加记录
bm = RS1.Bookmark '用书签记下当前记录位置
RS1.AddNew '调用AddNew方法增加新记录
Call EditStatus '置按钮和文本框状态
Call EmptyText '清空文本框
tj = True '是添加新记录
End Sub
Private Sub Command7_Click() '修改记录
Call EditStatus '置按钮和文本框状态
tj = False '不是添加记录,是修改记录
End Sub
Private Sub Command8_Click() '确定操作
Call SetValue '给各字段赋值
RS1.Update '调用Update方法保存
Call OkStatus '置按钮和文本框状态
MsgBox ("保存成功") '给出保存成功提示
End Sub
Private Sub Command9_Click() '取消操作
If (MsgBox("真的要取消修改吗?", vbQuestion + vbYesNo)) = vbYes Then
RS1.CancelUpdate
If tj Then '如果是增加新记录
RS1.Bookmark = bm '把记录指针移动到原来的位置
End If
Call DispValue '显示当前记录的值
Call OkStatus '置按钮和文本框状态
End If
End Sub
Private Sub Form_Load()
Connstring = "Driver={SQL Server};Server=PC2012110412ewr.;Database=GXPK;" _
& "uid =sa;pwd=t123" 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串
'设置连接字符串
With Conn
.ConnectionString = Connstring '给ConnectionString属性赋值
.ConnectionTimeout = 10
.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接
End With
MsgBox ("连接成功")
RS1.CursorType = adOpenDynamic
RS1.CursorLocation = adUseClient '游标服务的位置、默认值adUseServer。游标用于:控制记录定位、控制其他用户对数据库所作的更改的可视性、控制数据可更新性
RS1.LockType = adLockOptimistic
RS1.Open "班级信息表", Conn, 3, 3
If RS1.EOF And RS1.BOF Then '如果表为空
MsgBox "表为空,请添加记录"
Else
Call DispValue '显示当前记录的值
End If
Call TextDisable '使文本框不可用
Command8.Enabled = False
Command9.Enabled = False
End Sub
Private Sub DispValue()
Text1.Text = RS1.Fields("班级编号")
If IsNull(Trim(RS1.Fields("班级名称"))) Then '解决课程名字段值为空移指针出错(无效使用NULL)的问题,其他字段类推。
Text2.Text = ""
Else
Text2.Text = RS1.Fields("班级名称")
End If
Text5.Text = RS1.Fields("所属系")
Text7.Text = RS1.Fields("学生人数")
End Sub
Private Sub SetValue()
RS1.Fields("班级编号") = Text1.Text
RS1.Fields("班级名称") = Text2.Text
RS1.Fields("所属系称") = Text5.Text
RS1.Fields("学生人数") = Text7.Text
End Sub
Private Sub TextEnable() '使所有的文本框可用
Text1.Enabled = True
Text2.Enabled = True
Text5.Enabled = True
Text7.Enabled = True
End Sub
Private Sub TextDisable() '使所有的文本框不可用
Text1.Enabled = False
Text2.Enabled = False
Text5.Enabled = False
Text7.Enabled = False
End Sub
Private Sub EditStatus() '设置在编辑或添加记录时的各控件状态
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = True
Command9.Enabled = True
Call TextEnable
End Sub
Private Sub EmptyText() '清空文本框
Text1.Text = ""
Text2.Text = ""
Text5.Text = ""
Text7.Text = ""
End Sub
Private Sub OkStatus() '设置不在编辑或添加记录时的各控件状态
Command1.Enabled = True
Command2.Enabled = True
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论