面向初学者的窗体功能设计集成
你或许和我一样总在琢磨能不能在一个窗体中使数据的显示多种多样,呵呵。这个世界时变化多端的,我们的窗体也是这个世界上事物的一种反映,因此窗体也就可以是变化多端的。对于我们这样的Access初学者来说,快速的掌握变化的规律的最好方法就是经常性的做一些总结,由此你便能由此及彼,举一反三,其学习的效率自然提高。经过两月的学习,在下对窗体设计中常用的一些功能进行了一些肤浅的总结,并将其集成在下面Access小程序中。
1、加载时,使用子窗体1,学生表全部记录,锁定主窗体和子窗体记录,配置按钮。
Private Sub Form_Load()
Dim sql As String
Me.子窗体.SourceObject = "子窗体1"
sql = "select * from 学生表"
Me.班级ID.Locked = True
Me.班级名称.Locked = True
Me.子窗体.Form.RecordSource = sql
Me.子窗体.Form.AllowAdditions = False
Me.子窗体.Form.AllowDeletions = False
Me.子窗体.Form.AllowEdits = False
Me.隐藏列.Visible = False
Me.展开列.Visible = False
Me.隐藏行.Visible = False
Me.展开行.Visible = False
End Sub
2、关闭窗体时,压缩并修复数据库。
Private Sub Form_Close()
'压缩和修复数据库
CommandBars("Tools").Controls("数据库实用工具(&D)").Controls("压缩和修复数据库(&C)...").accDoDefaultAction
End Sub
3、双击班级ID或班级名称时,解锁。
Private Sub 班级ID_DblClick(Cancel As Integer)
Me.班级ID.Locked = False
End Sub
Private Sub 班级名称_DblClick(Cancel As Integer)
Me.班级名称.Locked = False
End Sub
4、失去焦点时,锁定班级ID或班级名称。
Private Sub 班级ID_LostFocus()
Me.班级ID.Locked = True
End Sub
Private Sub 班级名称_LostFocus()
Me.班级名称.Locked = True
End Sub
5、单击时,使用字窗体1或子窗体2。
Private Sub 更换窗体1_Click()
Me.子窗体.SourceObject = "子窗体1"
Me.隐藏列.Visible = True
Me.展开列.Visible = True
Me.隐藏行.Visible = True
Me.展开行.Visible = True
Me.新增.Visible = True
Me.删除.Visible = True
End Sub
Private Sub 更换窗体2_Click()
Me.子窗体.SourceObject = "子窗体2"
Me.隐藏列.Visible = False
Me.展开列.Visible = False
Me.隐藏行.Visible = False
Me.展开行.Visible = False
Me.新增.Visible = False
Me.删除.Visible = False
End Sub
6、单击时,显示或者隐藏子窗体。
Private Sub 隐藏窗体_Click()
Me.子窗体.Visible = False
End Sub
Private Sub 显示窗体_Click()
Me.子窗体.Visible = True
End Sub
7、单击时,显示或隐藏按钮。
Private Sub 隐藏按钮_Click()
Me.隐藏列.Visible = False
Me.展开列.Visible = False
Me.隐藏行.Visible = False
Me.展开行.Visible = False
End Sub
Private Sub 显示按钮_Click()
Me.隐藏列.Visible = True
Me.展开列.Visible = True
Me.隐藏行.Visible = True
Me.展开行.Visible = True
End Sub
8、单击时,自动编号并新增记录。
Private Sub 新增_Click()
Dim sql As String
DoCmd.SetWarnings no
sql = "INSERT INTO 学生表 ( 班级ID, 学生ID ) "
sql = sql + "SELECT Right(Forms!主窗体!班级ID,1) AS 班级ID, IIf(Max([学生ID]) Is Null,Forms!主窗体!班级ID & '01',Format(Max([学生ID])+1,'000')) AS ID "
sql = sql + "FROM 学生表 WHERE 学生表.班级ID=Forms!主窗体!班级ID;"
DoCmd.RunSQL sql
Me.子窗体.Requery
End Sub
9、单击时,删除指定记录。
Private Sub 删除_Click()
DoCmd.SetWarnings no
DoCmd.RunSQL "DELETE * FROM 学生表 WHERE 删除=Yes;"
Me.子窗体.Requery
End Sub
10、单击时,锁定、解锁、刷新子窗体。
Private Sub 锁定_Click()
Me.子窗体.Form.AllowAdditions = False
Me.子窗体.Form.AllowDeletions = False
Me.子窗体.Form.AllowEdits = False
End Sub
Private Sub 解锁_Click()
Me.子窗体.Form.AllowAdditions = True
Me.子窗体.Form.AllowDeletions = True
Me.子窗体.Form.AllowEdits = True
End Sub
Private Sub 刷新_Click()
Me.子窗体.Requery
End Sub
11、单击时,隐藏或展开列。
Private Sub 隐藏列_Click()
Me.子窗体.Form.家庭住址.ColumnHidden = True
Me.子窗体.Form.联系电话.ColumnHidden = True
End Sub
Private Sub 展开列_Click()
Me.子窗体.Form.家庭住址.ColumnHidden = False
Me.子窗体.Form.联系电话.ColumnHidden = False
End Sub
12、单击时,隐藏或展开行。
Private Sub 隐藏行_Click()
Dim sql As String
sql = "select * from 学生表 where isnull(姓名)"
Me.子窗体.Form.RecordSource = sql
Me.子窗体.Requery
End Sub
Private Sub 展开行_Click()
Dim sql As String
sql = "select * from 学生表"
Me.子窗体.Form.RecordSource = sql
Me.子窗体.Requery
End Sub