ADOX/ADO quirk when creating a primary key
From: Rob Nicholson (informed_at_community.nospam)
Date: 11/26/04
- Next message: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Previous message: DevelopIT: "Re: Peculiar slow jpg download from website with adodb.stream over ssl on IE"
- Next in thread: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Reply: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Reply: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 26 Nov 2004 14:18:49 -0000
This is one reason why I hate ADO/ADOX :-) The enclosed code is written in
Excel 2000 VBA and carries out the following:
o Delete existing Jet MDB file
o Create new Jet MDB file using ADOX
o Add a table to the new Jet database called People with two columns: ID &
People
o Add a primary key to the ID field
o Write 10 records into the database
o Read them back
o Print out the record count using SELECT COUNT(*)
The print of the record count returns 0 but there are obviously 10 records
in the recordset (as they print out). Comment out the line that adds the
primary key and it works as expected. So the primary key is causing a
problem.
Even more bizarre - open the database that has been created this way in
Access 2000 and the table is there fine, but no records are visible.
However, if you add a record and run a query similar to the one shown below
then it returns 11 records - the 10 "hidden" ones plus the one added in
Access.
All very bizarre!
Cheers, Rob.
----cut here----
Option Explicit
Sub Test()
' Kill existing database.
On Error Resume Next
Kill "c:\new.mdb"
On Error GoTo 0
' Create new database.
Dim Catalog As New ADOX.Catalog
Catalog.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"
' Create "People" table with two columns: ID & Name
Dim Table As New ADOX.Table
Table.Name = "People"
Table.Columns.Append "ID", adInteger
Table.Columns.Append "Name", adVarWChar, 50
Table.Keys.Append "PrimaryKey", adKeyPrimary, "ID" ' COMMENT THIS LINE OUT
AND RECORD COUNT WORKS!
Catalog.Tables.Append Table
' Open ADO connection to database.
Dim cn As New ADODB.Connection
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.ConnectionString = "Data Source=c:\new.mdb"
cn.Open
' Populate some records.
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open "People", cn, , adLockOptimistic
Dim i As Long
For i = 1 To 10
rst.AddNew
rst!ID = i
rst!Name = "Fred " & Format(i, "00")
rst.Update
Next
rst.Close
' Display records.
Set rst = New ADODB.Recordset
rst.Open "Select * From People", cn
While Not rst.EOF
Debug.Print rst!Name
rst.MoveNext
Wend
' Display count.
Set rst = New ADODB.Recordset
rst.Open "Select Count(*) As NumRecords From People", cn
Debug.Print rst!NumRecords & " records"
End Sub
----cut here----
- Next message: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Previous message: DevelopIT: "Re: Peculiar slow jpg download from website with adodb.stream over ssl on IE"
- Next in thread: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Reply: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Reply: Rob Nicholson: "Re: ADOX/ADO quirk when creating a primary key"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|