|
经过两个月的Access学习,对窗体设计中的一些常用方法略有收获,现将其集成在一起,可能对其他初学者有所帮助。
窗体中主要包含了文本框的锁定与解锁;按钮的隐藏与显示;子窗体的隐藏与显示;行列的隐藏与显示;行列的锁定与解锁;记录的自动编码、新增、删除;统计子窗体数据;子窗体记录的条件格式;压缩与修复数据库;自动宏;等相关功能。
欢迎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
[ 本帖最后由 todaynew 于 2008-12-28 18:55 编辑 ] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|