本文向你介紹如何不用借助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
以上是三聯網為您介紹的在VB6中用Select語句檢索記錄,希望對您有所幫助。