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