1 数据库基本录入界面的设计
首先用vb中的数据管理器创建一个access数据库“c:\my.mdb”,在表“worker”中加入一个字段“name”,然后在vb的缺省表单中拖入一个数据控件、一个文本框、一个表签、一个命令按钮组,合理设计界面布局,并设置各个控件的属性,如表1。
加入下列代码即可得到一个数据库基本输入窗口:
private sub command1-click(index as integer)
select case index
case0'addnew
data1.recordset.addnew
text1.setfocus
case1'edit
data1.recordset.edit
text1.setfocus
case2'giveup
data1.recordset.cance1update
data1.refresh
case3'save
data1.recordset. update
data1.refresh
case4'delete
data1.recordset.delete
data1.refresh
case5'end
end
end select
end sub
表一
控件名 | 属性名 | 属性值 |
Data | Data1 | DatabaseName = c:\my.mdb RecordSource = "worker" |
Text | Text1 | Text = "" RecordSource = "Data1" DataField = "Name" |
Label | Label1 | Caption = "姓名" |
CommandButton | Command1 | Index = 0 Caption = "新增" |
CommandButton | Command1 | Index = 1 Caption = "编辑" |
CommandButton | Command1 | Index = 2 Caption = "放弃" |
CommandButton | Command1 | Index = 3 Caption = "保存" |
CommandButton | Command1 | Index = 4 Caption = "删除" |
CommandButton | Command1 | Index = 5 Caption = "退出" |
在连续录入的几条记录中,同名字段的内容完全相同或基本相同,此时若能充分利用 数据库中的已有数据,设置一个复制键将上条记录中的同名字段的内容复制到当前记录的同名字段中,将能大提高数据录入速度。为了便于用户操作,将这一功能赋予ctrl键,用户在录入新记录或编辑原有记录时,只要按下ctrl键,则上条记录中的同名字段的内容就复制到当前记录的同名字段中。程序如下:
Option Explicit
Dim last As String
Private Sub form_Activate()
Dim mark As Variant
mark = Data1.Recordset.Bookmark
Data1.Recordset.MoveLast
last = Data1.Recordset("Name")
Data1.Recordset.Bookmark = mark
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
private sub Command1_Click(index as integer)
select case index
case0'addnew
data1.recordset.addnew
text1.setfocus
case1'edit
data1.recordset.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