.Edit
Text1.SetFocus
Case2''giveup
Data1.Recordset.Cance1Update
Data1.Refresh
Case3''save
Data1.Recordset. Update
Data1.Recordset.MoveLast
last=Data1.Recordset("name") ''save the text to last
Data1.Refresh
Case4''delete
Data1.Recordset.Delete
Data1.Refresh
Case5''end
End
End Select
End Sub
3 数据的选项录入、选项增减及选项编辑
有些字段的值域较小且相对固定,但会有一定的变化,如“省市”字段会产生变化,如增设一个省或市,撤县设市等等,程序除提供一个合法的基本的选项框供用户选择外,还应允许用户对这个基本的选项框进行增减或编辑。下面的
程序实现了此功能,用户双击表单则可对选项框进行增减和编辑,完成后再双击表单关闭编辑功能。
先建立一个数据库“c:myrand.mdb”,其表“rank”中加入一个字段“name”;再从工具箱中拖入一个数据控件Data2,一个数据绑定组合框Dbcombol和数据绑定网格控件DBgrid1。属性设置为:(1)Data2控件的:DatabaseName属性设为“c:\myrand.mdb”,RecordSource为“Rank”;(2)Dbcombo控件的:Name设为Dbcombo1,RowSource设为Data2,ListFiele设为“Name”,DataSource设为Data1。DataField设为“Name”;(3)DBgrid控件的:Name设为DBgrid1,将AllowAddNew、AllowDelete、AllowUpdate均设为True, DataSource设为Data2。
程序代码如下:
Option Explicit
Dim last As String
Dim dd As Boolean
Private Sub Form-Activate()
Dim mark As Variant
mark=Data1.Recordset.Bookmark
Data1.Recordset.MoveLast
last=Data1.Recordset("name")
Data1.Recordset.Bookmark=mark
Text1.Visible=True
DBCombol.Visible=False
DBGrid1.Visible=False
End Sub
Private Sub Form-DblClick() ''双击表单打开或关闭选项增减和选项编辑功能
Static dd As Boolean
dd=Not dd ''第一次双击打开编辑功能第二次双击关闭编辑功能
If dd Then
DBGrid1.Visible=True
Else
DBGrid1.Visible=False
Exit Sub
End If
End Sub
Private Sub DBGrid1-DblClick() ’选择当前项后,再双击删除当前记录选项
Data2.Recordset.Delete
DBCombol.Refresh
DBGrid1.Refresh
End Sub
Private Sub DBGrid1-LostFocus()
DBCombol.Refresh ''刷新DBCombol
End Sub
Private Sub Command1-Click(Index As Integer)
Dim i As Integer
Select Case Index
Case0''addnew
Data1.Recordset.Addnew
DBCombol.SetFocus
DBCombol.Visible=True
Text1.Visible=False
Case1''edit
Data1.Recordset.Edit
Text1.SetFocus
DBCombol.Visible=False
Text1.Visible=True
Case2''giveup
Data1.Recordset.Cance1Update
Data1.Refresh
DBCombol.Visible=False
Text1.Visible=True
Case3''save
Data1.Recordset. Update
Data1.Recordset.MoveLast
last=Data1.Recordset("name")
Data1.Refresh
DBCombol.Visible=False
Text1.Visible=True
Case4''delete
Data1.Recordset.Delete
Data1.Refresh
Case5''end
End
End Select
End Sub
Private Sub DBCombol-KeyDown
(KeyCode As Integer,Shift As Integer)
If Shift=2 Then ''按下CTRL-KEY复制上条记录中的同名字段的内容
If Data1.Recordset.EditMode=dbEditInProgress Or
Data1.Recordset.EditMode=dbEditAdd
Then
DBCombol.Text=last
End If
End If
End Sub% Private Sub Text1-KeyDown(KeyCode As Integer,Shift As Integer)
If Shift=2 Then ''按下CTRL-KEY复制上条记录中的同名字段的内容
If Data1.Recordset.EditMode=dbEditInProgress Or
Data1.Recordset.EditMode=dbEditAdd
Then
Text1.Text=last
End If
End If
End Sub
通过上述方法使录入速