This article describes how you can create a database directly in your program without using access, and then read the data from a standard ASCII text file into the database. The original is a Microsoft Knowledge Base article, but was written for VB3, so the code is a bit outdated. For example, there is no Table object in the DAO now, instead of a Recordset object. The following is the modified code, which is debugged in VB6.
First, add the Microsoftdao3.51library reference to the project.
Add three command buttons and two MSFlexGrid to the form.
Follow the table settings and properties of the control:
Control property Value
Command1caption "Create a text file and display it in a grid"
Command2caption "Send input data and create a new database"
Command3caption "Show data in a new database"
Grid1cols5
Grid1rows35
Grid2cols5
Grid2rows35
Add the following code to the Declarations section of the form
Dimnums (Aslong)
Dimnames (ASSTRING*20)
Dimaddresses (ASSTRING*25)
Dimss_nums (ASSTRING*12)
Constdb_long=4
constdb_text=10
Constdb_lang_general= "; langid=0x0809; cp=1252; Country=0 "
Add the following code to the form's Load event
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
Add the following code to the Command1_Click event
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$ ()) "-" 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
Add the following code to the Command2_Click event
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
Add the following code to the Command3_Click event
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
above is the three networking for you to retrieve records in VB6 with a SELECT statement, which you hope will help you.