当前位置:主页>VB教程>

在VB6中用Select语句检索记录

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

  首先在工程中添加对MicrosoftDAO3.51Library引用。 字串2

  在窗体中添加三个命令按钮和两个MSFlexGrid. 字串2

  按照下表设置和控件的属性:

字串6

  控件属性值

字串6

  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

字串5

  ConstDB_TEXT=10

字串1

  ConstDB_LANG_GENERAL=";LANGID=0x0809;CP=1252;COUNTRY=0"

字串7

  将下面的代码添加到窗体的Load事件中 字串1

  SubForm_Load()

字串1

  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

字串4

  grid1.Row=0

字串1

  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

字串9

  grid1.Row=0

字串1

  grid1.Text="EmpSSN"'HeaderforEmpSSNfromtextfile

字串3

  grid2.ColWidth(1)=1000'ForEmpID 字串7

  grid2.ColWidth(2)=2000'ForEmpName 字串3

  grid2.ColWidth(3)=3000'ForEmpAddr

字串6

  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

字串3

  grid2.Row=0 字串2

  grid2.Text="EmployeeAddr"'HeaderforEmpIDfromDB 字串3

  grid2.Col=4

字串5

  grid2.Row=0

字串3

  grid2.Text="EmployeeSSN"'HeaderforEmpNamefromDB 字串3

  EndSub 字串2


  在Command1_Click事件中加入下面的代码

  SubCommand1_Click() 字串9

  Fori=1To30 字串3

  nums(i)=i

字串4

  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)))

字串8

  Else 字串6

  '*Enterthefollowingtwolinesasone,singleline: 字串6

  ss_nums(i)=Trim$(Trim$(Str$(999)) "-" Trim$(Str$(88)) "-" Trim$(Str$(7777))) 字串9

  EndIf 字串9

  Nexti

字串6

  Open"Testdata.DAT"ForOutputAs#1

字串3

  Forj=1To30

字串9

  Print#1,nums(j) 字串6

  Print#1,names(j)

字串6

  Print#1,addresses(j)

字串4

  Print#1,ss_nums(j) 字串1

  Nextj 字串2

  Close#1 字串1

  Fori=1To30'Displayresultsfromtextfile 字串6

  grid1.Col=1

字串5

  grid1.Row=i

字串8

  grid1.Text=nums(i)'LoadEmpIDs

字串4

  grid1.Col=2 字串2

  grid1.Row=i 字串2

  grid1.Text=names(i)'LoadEmpNames

字串2

  grid1.Col=3

字串7

  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

字串7

  EndSub

字串2

  在Command2_Click事件中加入下面的代码 字串5

  SubCommand2_Click() 字串6

  DimnewdbAsDatabase 字串2

  DimnewtbAsRecordset

字串9

  DimnewtdAsNewtabledef

字串1

  DimnewidxAsNewIndex

字串2

  Dimfield1AsNewfield'ForEmpnums 字串5

  Dimfield2AsNewfield'ForEmpnames 字串2

  Dimfield3AsNewfield'ForEmpaddresses 字串8

  Dimfield4AsNewfield'ForEmpss_nums

字串8

  screen.MousePointer=11'Displaythetimetobuild

字串7

   字串6

  Setnewdb=CreateDatabase("NEWDB.MDB",DB_LANG_GENERAL)

字串2

   字串1

  newtd.Name="Emp_Table"'*Newtablename

字串4

  field1.Name="Emp_ID"'*HoldsEmployeeIDnums()

字串4

  field1.Type=DB_LONG

字串2

  newtd.Fields.Appendfield1

字串5

  field2.Name="Emp_Name"'*HoldsEmpnames()

字串6

  field2.Type=DB_TEXT

字串1

  field2.Size=20 字串1

  newtd.Fields.Appendfield2 字串5

  field3.Name="Emp_Addr"'*HoldsEmployeeaddr()

字串4

  field3.Type=DB_TEXT 字串8

  field3.Size=25 字串9

  newtd.Fields.Appendfield3 字串9

  field4.Name="Emp_SSN"'*Holdsempss_nums()

字串2

  field4.Type=DB_TEXT

字串7

  field4.Size=12

字串8

  newtd.Fields.Appendfield4 字串9

  newidx.Name="Emp_ID_IDX"'*Youhavetohaveanindex

字串9

  newidx.Fields="Emp_ID"

字串7

  newidx.Primary=True

字串2

  newtd.Indexes.Appendnewidx

字串3

  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

字串4

  newtb("Emp_Addr")=Trim$(tmp3$)'Placeinfield3 字串8

  newtb("Emp_SSN")=Trim$(tmp4$)'Placeinfield4

字串2

  newtb.Update'Savetotable 字串1

  Loop

字串9

  CommitTrans

字串5

  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

字串9

  grid2.Col=1

字串9

  grid2.Row=counter 字串2

  grid2.Text=t(0)'LoadEmpID

字串9

  grid2.Col=2

字串4

  grid2.Row=counter

字串8

  grid2.Text=t(1)'LoadEmpName

字串9

  grid2.Col=3

字串6

  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

字串2

  db.Close

字串8

  EndSub

字串4

-> 字串1