本文示範在 VB6 中不依賴 Access,直接以程式碼建立 Jet 資料庫(.mdb),從 ASCII 文字檔讀取資料寫入資料表,並以 MSFlexGrid 顯示結果。原為 VB3 範例,已更新為 VB6 + DAO 3.51 相容版本。
Dim nums(30) As Long
Dim names(30) As String * 20
Dim addresses(30) As String * 25
Dim ss_nums(30) As String * 12
Const DB_LONG = 4
Const DB_TEXT = 10
Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"
Private Sub Form_Load()
Me.Show
' Grid1 顯示文字檔資料
Grid1.ColWidth(1) = 1000: Grid1.Col = 1: Grid1.Row = 0: Grid1.Text = "EmpID"
Grid1.ColWidth(2) = 2000: Grid1.Col = 2: Grid1.Row = 0: Grid1.Text = "EmpName"
Grid1.ColWidth(3) = 3000: Grid1.Col = 3: Grid1.Row = 0: Grid1.Text = "EmpAddr"
Grid1.ColWidth(4) = 2000: Grid1.Col = 4: Grid1.Row = 0: Grid1.Text = "EmpSSN"
' Grid2 顯示資料庫資料
Grid2.ColWidth(1) = 1000: Grid2.Col = 1: Grid2.Row = 0: Grid2.Text = "EmployeeID"
Grid2.ColWidth(2) = 2000: Grid2.Col = 2: Grid2.Row = 0: Grid2.Text = "EmployeeName"
Grid2.ColWidth(3) = 3000: Grid2.Col = 3: Grid2.Row = 0: Grid2.Text = "EmployeeAddr"
Grid2.ColWidth(4) = 2000: Grid2.Col = 4: Grid2.Row = 0: Grid2.Text = "EmployeeSSN"
End Sub
Private Sub Command1_Click()
' 產生 30 筆測試資料
For i = 1 To 30
nums(i) = i
names(i) = "JohnDoe#" & Str$(i)
addresses(i) = Str$(i) & "MockingBirdLane"
If i < 9 Then
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)))
Else
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
' 顯示於 Grid1
For i = 1 To 30
Grid1.Col = 1: Grid1.Row = i: Grid1.Text = nums(i)
Grid1.Col = 2: Grid1.Row = i: Grid1.Text = names(i)
Grid1.Col = 3: Grid1.Row = i: Grid1.Text = addresses(i)
Grid1.Col = 4: Grid1.Row = i: Grid1.Text = ss_nums(i)
Next i
End Sub
Private 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, field2 As New Field, field3 As New Field, field4 As New Field
Screen.MousePointer = 11
Set newdb = CreateDatabase("NEWDB.MDB", DB_LANG_GENERAL)
newtd.Name = "Emp_Table"
' 定義欄位
field1.Name = "Emp_ID": field1.Type = DB_LONG: newtd.Fields.Append field1
field2.Name = "Emp_Name": field2.Type = DB_TEXT: field2.Size = 20: newtd.Fields.Append field2
field3.Name = "Emp_Addr": field3.Type = DB_TEXT: field3.Size = 25: newtd.Fields.Append field3
field4.Name = "Emp_SSN": field4.Type = DB_TEXT: field4.Size = 12: newtd.Fields.Append field4
' 主鍵索引
newidx.Name = "Emp_ID_IDX": 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$: newtb("Emp_ID") = Trim$(tmp1$)
Line Input #1, tmp2$: newtb("Emp_Name") = Trim$(tmp2$)
Line Input #1, tmp3$: newtb("Emp_Addr") = Trim$(tmp3$)
Line Input #1, tmp4$: newtb("Emp_SSN") = Trim$(tmp4$)
newtb.Update
Loop
CommitTrans
Close #1
newtb.Close: newdb.Close
Screen.MousePointer = 0
End Sub
Private Sub Command3_Click()
Dim db As Database, t As Recordset, counter As Long
Set db = OpenDatabase("NEWDB.MDB")
Set t = db.OpenRecordset("Emp_Table")
counter = 1
Do Until t.EOF
Grid2.Col = 1: Grid2.Row = counter: Grid2.Text = t(0)
Grid2.Col = 2: Grid2.Row = counter: Grid2.Text = t(1)
Grid2.Col = 3: Grid2.Row = counter: Grid2.Text = t(2)
Grid2.Col = 4: Grid2.Row = counter: Grid2.Text = t(3)
counter = counter + 1
t.MoveNext
Loop
t.Close: db.Close
End Sub
CreateDatabase 建立 .mdb,TableDef/Field/Index 定義結構OpenRecordset 取得可更新的 RecordsetBeginTrans/CommitTrans 交易控制,確保批次寫入一致性Line Input 逐行讀取,Trim$ 去除空白此範例展示 VB6 以 DAO 直接操作 Jet Engine 的完整流程,適合輕量級資料匯入需求。