Article ID: 150418
Article Last Modified on 6/28/2004
Option Explicit
Dim rs1 As Recordset
Dim rs2 As Recordset
Dim db As Database
Dim td As TableDef
Dim fl As Field
Private Sub Command1_Click()
Dim iFields As Integer, iRecords As Integer
' Create the database.
Set db = CreateDatabase("C:\test.mdb", dbLangGeneral)
Set td = db.CreateTableDef("Table1")
'Now that the database is created, add fields to the database
For iFields = 1 To 5 'The last number can be changed.
Set fl = td.CreateField("Field " & CStr(iFields), dbInteger)
td.Fields.Append fl
Next iFields
db.TableDefs.Append td
' Now that the database has fields, add records through a
' recordset.
Set rs1 = db.OpenRecordset("Table1", dbOpenTable)
For iRecords = 1 To 10 ' For each row
rs1.AddNew ' add a new record.
For iFields = 1 To 5 ' For each field in the record
rs1("Field " & CStr(iFields)) = iFields ' add a number.
Next iFields
rs1.Update
Next iRecords
' Close both the recordset and database.
rs1.Close
db.Close
' Populate the DBGrid control with the contents of the Recordset.
Set db = OpenDatabase("C:\test.mdb")
Set rs1 = db.OpenRecordset("Select * from Table1")
Set Data1.Recordset = rs1
Command1.Visible = False
End Sub
Private Sub Form_Load()
If Dir("C:\test.mdb") = "" Then
Command1.Caption = "Create Database"
Command1.Visible = True
End If
End Sub
Additional query words: 4.00 vb4win vb4all
Keywords: kbcode KB150418