当前位置:首页 > 网络编程 > 数据库 > ACCESS > 如何将文本文件转换为ACCESS数据库

如何将文本文件转换为ACCESS数据库

点击次数:27 次 发布日期:2008-11-21 22:41:26 作者:源代码网
源代码网推荐
本文向你介绍如何不用借助Access,直接在程序中创建一个数据库,然后从标准的ASCII文本文件中读取数据到数据库中。原文是微软知识库中的一篇文章,但当时是针对VB3写的,所以其中的代码有点过时。例如现在DAO中已没有Table对象,代之以Recordset对象。下面是修改后的代码,在VB6中调试通过。

源代码网整理以下 首先在工程中添加对Microsoft DAO 3.51 Library引用。

源代码网整理以下在窗体中添加三个命令按钮和两个MSFlexGrid.

源代码网整理以下按照下表设置和控件的属性:

源代码网整理以下控件 属性 值

源代码网整理以下--------------------------------------------------------------------

源代码网整理以下Command1 Caption "建立文本文件并显示在网格中"

源代码网整理以下Command2 Caption "传输入数据并新建一个数据库"

源代码网整理以下Command3 Caption "显示新数据库中的数据"

源代码网整理以下Grid1 Cols 5

源代码网整理以下Grid1 Rows 35

源代码网整理以下Grid2 Cols 5

源代码网整理以下Grid2 Rows 35

源代码网整理以下将下面的代码添加到窗体的声明部分

源代码网整理以下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"

源代码网整理以下将下面的代码添加到窗体的Load事件中

源代码网整理以下Sub Form_Load ()

源代码网整理以下Show

源代码网整理以下grid1.ColWidth(1) = 1000 "For Emp ID

源代码网整理以下grid1.ColWidth(2) = 2000 "For Emp Name

源代码网整理以下grid1.ColWidth(3) = 3000 "For Emp Addr

源代码网整理以下grid1.ColWidth(4) = 2000 "For Emp SSN

源代码网整理以下grid1.Col = 1

源代码网整理以下grid1.Row = 0

源代码网整理以下grid1.Text = "Emp ID" "Header for Emp ID from text file

源代码网整理以下grid1.Col = 2

源代码网整理以下grid1.Row = 0

源代码网整理以下grid1.Text = "Emp Name" "Header for Emp Name from text file

源代码网整理以下grid1.Col = 3

源代码网整理以下grid1.Row = 0

源代码网整理以下grid1.Text = "Emp Addr" "Header for Emp Addr from text file

源代码网整理以下grid1.Col = 4

源代码网整理以下grid1.Row = 0

源代码网整理以下grid1.Text = "Emp SSN" "Header for Emp SSN from text file

源代码网整理以下grid2.ColWidth(1) = 1000 "For Emp ID

源代码网整理以下grid2.ColWidth(2) = 2000 "For Emp Name

源代码网整理以下grid2.ColWidth(3) = 3000 "For Emp Addr

源代码网整理以下grid2.ColWidth(4) = 2000 "For Emp SSN

源代码网整理以下grid2.Col = 1

源代码网整理以下grid2.Row = 0

源代码网整理以下grid2.Text = "Employee ID" "Header for Emp ID from DB

源代码网整理以下grid2.Col = 2

源代码网整理以下grid2.Row = 0

源代码网整理以下grid2.Text = "Employee Name" "Header for Emp Name from DB

源代码网整理以下grid2.Col = 3

源代码网整理以下grid2.Row = 0

源代码网整理以下grid2.Text = "Employee Addr" "Header for Emp ID from DB

源代码网整理以下grid2.Col = 4

源代码网整理以下grid2.Row = 0

源代码网整理以下grid2.Text = "Employee SSN" "Header for Emp Name from DB

源代码网整理以下End Sub

源代码网整理以下在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 Table

源代码网整理以下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.OpenTable("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$ "Re

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华