在Visual Basic数据库编程中,应用数据绑定组合框和数据绑定网格等控件实现了数据的选项录入、选项增减与选项编辑,提高了数据录入效率和准确性。
高效准确地录入数据已成为mis系统急待解决的问题,也是衡量mis系统成功的重要标志。本文在Visual Basic数据库编程中应用数据绑定组合框和数据绑定网格等控件实现了数据的选项录入、选项增减与数据编辑,提高了数据录入效率、准确性及灵活性。(
考察mis系统涉及的数据性质、值域范围和变化程度,可以发现在mis系统数据录入中往往出现下列情况
(1)连续录入的几条记录中,同名字段的内容完全相同或基本相同,如省份、职称等;用户逐字录入速度慢易出错,因此应充分利用数据库中的已有数据,设置一个复制键将上条记录中的同名字段的内容复制到当前记录的同名字段中;
(2)有些字段的值域固定,因此程序应提供一个合法的选项框供用户选择来提高速度;
有些字段的值域较小且相对固定,但有一定的变化,如“省市”字段增设一个省或市,撤县设市等等,程序除提供一个合法的基本的选项框供用户选择外,且应允许用户对这个基本的选项框进行增减或编辑。
通过上述方法使录入速度进一步得到提高,使用户的功效达到事半功倍的效果。
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
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
通过上述方法使录入速度进一步得到提高,使用户的功效达到事半功倍的效果。 |