本文向你介绍如何不用借助Access,直接在程序中创建一个数据库,然后从标准的ASCII文本文件中读取数据到数据库中。原文是微软知识库中的一篇文章,但当时是针对VB3写的,所以其中的代码有点过时。例如现在DAO中已没有Table对象,代之以Recordset对象。下面是修改后的代码,在VB6中调试通过。
首先在工程中添加对MicrosoftDAO3.51Library引用。
在窗体中添加三个命令按钮和两个MSFlexGrid.
按照下表设置和控件的属性:
控件属性值
Command1Caption"建立文本文件并显示在网格中"
Command2Caption"传输入数据并新建一个数据库"
Command3Caption"显示新数据库中的数据"
Grid1Cols5
Grid1Rows35
Grid2Cols5
Grid2Rows35
将下面的代码添加到窗体的声明部分
Dimnums(30)AsLong
Dimnames(30)AsString*20
Dimaddresses(30)AsString*25
Dimss_nums(30)AsString*12
ConstDB_LONG=4
ConstDB_TEXT=10
ConstDB_LANG_GENERAL=";LANGID=0x0809;CP=1252;COUNTRY=0"
将下面的代码添加到窗体的Load事件中
SubForm_Load()
Show
grid1.ColWidth(1)=1000'ForEmpID
grid1.ColWidth(2)=2000'ForEmpName
grid1.ColWidth(3)=3000'ForEmpAddr
grid1.ColWidth(4)=2000'ForEmpSSN
grid1.Col=1
grid1.Row=0
grid1.Text="EmpID"'HeaderforEmpIDfromtextfile
grid1.Col=2
grid1.Row=0
grid1.Text="EmpName"'HeaderforEmpNamefromtextfile
grid1.Col=3
grid1.Row=0
grid1.Text="EmpAddr"'HeaderforEmpAddrfromtextfile
grid1.Col=4
grid1.Row=0
grid1.Text="EmpSSN"'HeaderforEmpSSNfromtextfile
grid2.ColWidth(1)=1000'ForEmpID
grid2.ColWidth(2)=2000'ForEmpName
grid2.ColWidth(3)=3000'ForEmpAddr
grid2.ColWidth(4)=2000'ForEmpSSN
grid2.Col=1
grid2.Row=0
grid2.Text="EmployeeID"'HeaderforEmpIDfromDB
grid2.Col=2
grid2.Row=0
grid2.Text="EmployeeName"'HeaderforEmpNamefromDB
grid2.Col=3
grid2.Row=0
grid2.Text="EmployeeAddr"'HeaderforEmpIDfromDB
grid2.Col=4
grid2.Row=0
grid2.Text="EmployeeSSN"'HeaderforEmpNamefromDB
EndSub
在Command1_Click事件中加入下面的代码
SubCommand1_Click()
Fori=1To30
nums(i)=i
names(i)="JohnDoe#" Str$(i)
addresses(i)=Str$(i) "MockingBirdLane"
Ifi<9Then
'*Enterthefollowingfourlinesasone,singleline:
ss_nums(i)=Trim$(Str$(i) Trim$(Str$(i)) Trim$(Str$(i)) "-" Trim$(Str$(i 1)) Trim$(Str$(i 1)) "-" Trim$(Str$(i))
Trim$(Str$(i)) Trim$(Str$(i)) Trim$(Str$(i)))
Else
'*Enterthefollowingtwolinesasone,singleline:
ss_nums(i)=Trim$(Trim$(Str$(999)) "-" Trim$(Str$(88)) "-" Trim$(Str$(7777)))
EndIf
Nexti
Open"Testdata.DAT"ForOutputAs#1
Forj=1To30
Print#1,nums(j)
Print#1,names(j)
Print#1,addresses(j)
Print#1,ss_nums(j)
Nextj
Close#1
Fori=1To30'Displayresultsfromtextfile
grid1.Col=1
grid1.Row=i
grid1.Text=nums(i)'LoadEmpIDs
grid1.Col=2
grid1.Row=i
grid1.Text=names(i)'LoadEmpNames
grid1.Col=3
grid1.Row=i
grid1.Text=addresses(i)'LoadEmpAddrs
grid1.Col=4
grid1.Row=i
grid1.Text=ss_nums(i)'LoadEmpSSNs
Nexti
EndSub
在Command2_Click事件中加入下面的代码
SubCommand2_Click()
DimnewdbAsDatabase
DimnewtbAsRecordset
DimnewtdAsNewtabledef
DimnewidxAsNewIndex
Dimfield1AsNewfield'ForEmpnums
Dimfield2AsNewfield'ForEmpnames
Dimfield3AsNewfield'ForEmpaddresses
Dimfield4AsNewfield'ForEmpss_nums
screen.MousePointer=11'Displaythetimetobuild
Setnewdb=CreateDatabase("NEWDB.MDB",DB_LANG_GENERAL)
newtd.Name="Emp_Table"'*Newtablename
field1.Name="Emp_ID"'*HoldsEmployeeIDnums()
field1.Type=DB_LONG
newtd.Fields.Appendfield1
field2.Name="Emp_Name"'*HoldsEmpnames()
field2.Type=DB_TEXT
field2.Size=20
newtd.Fields.Appendfield2
field3.Name="Emp_Addr"'*HoldsEmployeeaddr()
field3.Type=DB_TEXT
field3.Size=25
newtd.Fields.Appendfield3
field4.Name="Emp_SSN"'*Holdsempss_nums()
field4.Type=DB_TEXT
field4.Size=12
newtd.Fields.Appendfield4
newidx.Name="Emp_ID_IDX"'*Youhavetohaveanindex
newidx.Fields="Emp_ID"
newidx.Primary=True
newtd.Indexes.Appendnewidx
newdb.TableDefs.Appendnewtd
Setnewtb=newdb.OpenRecordset("Emp_Table")
Open"Testdata.dat"ForInputAs#1
BeginTrans
DoWhileNot(EOF(1))
newtb.AddNew
LineInput#1,tmp1$'Retrieveempl_id
LineInput#1,tmp2$'Retrieveempl_name
LineInput#1,tmp3$'Retrieveempl_addr
LineInput#1,tmp4$
newtb("Emp_ID")=Trim$(tmp1$)'Placeinfield1
newtb("Emp_Name")=Trim$(tmp2$)'Placeinfield2
newtb("Emp_Addr")=Trim$(tmp3$)'Placeinfield3
newtb("Emp_SSN")=Trim$(tmp4$)'Placeinfield4
newtb.Update'Savetotable
Loop
CommitTrans
Close#1'Closetextfile
newtb.Close'CloseDB'stable
newdb.Close'CloseDB
screen.MousePointer=0'Setbacktoshowdone
EndSub
在Command3_Click事件中加入下面的代码
SubCommand3_Click()
DimdbAsDatabase
DimtAsRecordset
Dimcounter
Setdb=OpenDatabase("NEWDB.MDB")
Sett=db.OpenRecordset("Emp_Table")
counter=1'StartcounteratRow=1
DoUntilt.EOF
grid2.Col=1
grid2.Row=counter
grid2.Text=t(0)'LoadEmpID
grid2.Col=2
grid2.Row=counter
grid2.Text=t(1)'LoadEmpName
grid2.Col=3
grid2.Row=counter
grid2.Text=t(2)'LoadEmpAddr
grid2.Col=4
grid2.Row=counter
grid2.Text=t(3)'LoadEmpSSN
counter=counter 1
t.MoveNext
Loop
t.Close
db.Close
EndSub