在Command1_Click事件中加入下面的代碼
Sub Command1_Click ()
For i% = 1 To 30
nums(i%) = i%
names(i%) = "John Doe # " + Str$(i%)
addresses(i%) = Str$(i%) + " Mocking Bird Lane"
If i% < 9 Then
'* Enter the following four lines as one, single line:
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
'* Enter the following two lines as one, single line:
ss_nums(i%) = Trim$(Trim$(Str$(999)) + "-" + Trim$(Str$(88))+ "-" + Trim$(Str$(7777)))
End If
Next i%
Open "Testdata.DAT" For Output As #1
For j% = 1 To 30
Print #1, nums(j%)
Print #1, names(j%)
Print #1, addresses(j%)
Print #1, ss_nums(j%)
Next j%
Close #1
For i% = 1 To 30 'Display results from text file
grid1.Col = 1
grid1.Row = i%
grid1.Text = nums(i%) 'Load Emp IDs
grid1.Col = 2
grid1.Row = i%
grid1.Text = names(i%) 'Load Emp Names
grid1.Col = 3
grid1.Row = i%
grid1.Text = addresses(i%) 'Load Emp Addrs
grid1.Col = 4
grid1.Row = i%
grid1.Text = ss_nums(i%) 'Load Emp SSNs
Next i%
End Sub
在Command2_Click事件中加入下面的代碼
Sub Command2_Click ()
Dim newdb As Database
Dim newtb As Recordset
Dim newtd As New tabledef
Dim newidx As New Index
Dim field1 As New field 'For Emp nums
Dim field2 As New field 'For Emp names
Dim field3 As New field 'For Emp addresses
Dim field4 As New field 'For Emp ss_nums
screen.MousePointer = 11 'Display the time to build
Set newdb = CreateDatabase("NEWDB.MDB", DB_LANG_GENERAL)
newtd.Name = "Emp_Table" '* New table name
field1.Name = "Emp_ID" '* Holds Employee ID nums()
field1.Type = DB_LONG
newtd.Fields.Append field1
field2.Name = "Emp_Name" '* Holds Emp names()
field2.Type = DB_TEXT
field2.Size = 20
newtd.Fields.Append field2
field3.Name = "Emp_Addr" '* Holds Employee addr()
field3.Type = DB_TEXT
field3.Size = 25
newtd.Fields.Append field3
field4.Name = "Emp_SSN" '* Holds emp ss_nums()
field4.Type = DB_TEXT
field4.Size = 12
newtd.Fields.Append field4
newidx.Name = "Emp_ID_IDX" '* You have to have an index
newidx.Fields = "Emp_ID"
newidx.Primary = True
newtd.Indexes.Append newidx
newdb.TableDefs.Append newtd
Set newtb = newdb.OpenRecordset("Emp_Table")
Open "Testdata.dat" For Input As #1
BeginTrans
Do While Not (EOF(1))
newtb.AddNew
Line Input #1, tmp1$ 'Retrieve empl_id
Line Input #1, tmp2$ 'Retrieve empl_name
Line Input #1, tmp3$ 'Retrieve empl_addr
Line Input #1, tmp4$
newtb("Emp_ID") = Trim$(tmp1$) 'Place in field1
newtb("Emp_Name") = Trim$(tmp2$) 'Place in field2
newtb("Emp_Addr") = Trim$(tmp3$) 'Place in field3
newtb("Emp_SSN") = Trim$(tmp4$) 'Place in field4
newtb.Update 'Save to table
Loop
CommitTrans
Close #1 'Close text file
newtb.Close 'Close DB's table
newdb.Close 'Close DB
screen.MousePointer = 0 'Set back to show done
End Sub
在Command3_Click事件中加入下面的代碼
Sub Command3_Click ()
Dim db As Database
Dim t As Recordset
Dim counter%
Set db = OpenDatabase("NEWDB.MDB")
Set t = db.OpenRecordset("Emp_Table")
counter% = 1 'Start counter at Row=1
Do Until t.EOF
grid2.Col = 1
grid2.Row = counter%
grid2.Text = t(0) 'Load Emp ID
grid2.Col = 2
grid2.Row = counter%
grid2.Text = t(1) 'Load Emp Name
grid2.Col = 3
grid2.Row = counter%
grid2.Text = t(2) 'Load Emp Addr
grid2.Col = 4
grid2.Row = counter%
grid2.Text = t(3) 'Load Emp SSN
counter% = counter% + 1
t.MoveNext
Loop
t.Close
db.Close
End Sub