Click to See Complete Forum and Search --> : ADO: primary key not accessable
zysset
Nov 7th, 2000, 04:41 AM
im using an access db within my vb application. there are absolutely no problems to fill in the fields with data, except the autonumbered (long integer) id field, which is always referenced by NULL. i have to read this value to do the relationship to other tables...
hope someone have an idea?
paulw
Nov 7th, 2000, 05:05 AM
I don't entirely understand your q.
You never write a value to an AutoNumber field and it will CERTAINLY never be NULL. How are you trying to get at the value? Some code might help.
Paul.
zysset
Nov 7th, 2000, 06:45 AM
AccessGUI.datHwRS.Recordset.AddNew
For i = 0 To (UBound(hwFields) - 1)
AccessGUI.datHwRS.Recordset.Fields(hwFields(i)).value =_
col.item(hwFields(i))
Next i
AccessGUI.datHwRS.Recordset.UpdateBatch adAffectAll
hwId = AccessGUI.datHwRS.Recordset.Fields("id").value
error happens in the last line - the id field is a null object?!? of course it is not...
Vit
Nov 7th, 2000, 07:21 AM
Make sure that "ID" field is included into recordset's fields collection.
paulw
Nov 7th, 2000, 07:27 AM
There is no code here to omit the autonumber field - you should not assign a value to that field - this may be what is causing you probs.
Try omitting the value update if the field name is "id"
e.g.
AccessGUI.datHwRS.Recordset.AddNew
For i = 0 To (UBound(hwFields) - 1)
If Not(hwFields(i) = "id") Then
AccessGUI.datHwRS.Recordset.Fields(hwFields(i)).value = col.item(hwFields(i))
End If
Next i
AccessGUI.datHwRS.Recordset.UpdateBatch adAffectAll
hwId = AccessGUI.datHwRS.Recordset.Fields("id").value
I am assuming that hwFields is your array of field names...
Cheers,
Paul.
P.S. Your naming conventions are a tad difficult to read
zysset
Nov 7th, 2000, 07:47 AM
first of all sorry my naming conventions...
hwFields is an array of my field names, except the "id" field! -> your if statement is always true!
the problem is, that when i have added a new record, the autonumber field isn't up to date after the updateBatch-call . the autonumber is physically stored in the database but not in my variables. and unfortunately a refresh causes a reset of the record pointer...
the only thing i want, is to get the primary key of my new record!
paulw
Nov 7th, 2000, 08:26 AM
Now I've got you!
You will need to use refresh, but can you not use a bookmark to get back to your current position?
Cheers,
Paul.
zysset
Nov 7th, 2000, 09:39 AM
Originally posted by paulw
Now I've got you!
You will need to use refresh, but can you not use a bookmark to get back to your current position?
i have to connect different tables together using foreignkeys (without gui). and i tried it this way (maybe there are more effective solutions). and yes you're right i tried to bookmark the current position but the bookmark isn't valid after the refresh call...
the code now looks like:
With ado
.Recordset.addNew
For i = 0 To (UBound(fields) - 1)
.Recordset.fields(fields(i)) = col.item(fields(i))
Next i
.Recordset.UpdateBatch adAffectAll
bookmark = .Recordset.bookmark
.Refresh
.Recordset.bookmark = bookmark
MyId = .Recordset.fields("id").value
End With
where:
ado is a ado data control
fields is the array of field names
col is a collection with field values to enter
bookmark is a variant variable
cheers
patrick
paulw
Nov 7th, 2000, 10:01 AM
OK, this one is hacking me off.
If you are adding new, how come the record is not the last record? You could set the recordset to be ORDERed by "id", then that guarantees that the latest record added will be the last. You can then use
.Refresh
.MoveLast
.GiveMeMyValuesYouBastard (This is a user defined method:D)
Let me know why you cannot do this ('cos I bet you thought of it).
Cheers,
Paul.
zysset
Nov 8th, 2000, 01:03 AM
cause the (predefined) database uses a autonumber RANDOM ! field :-( this is why the new entry isn't at the end....
paulw
Nov 8th, 2000, 03:33 AM
I hadn't even realised that you could randomize the autonumber! Can you not change that behaviour?
I checked out ADO last night and the bookmarks should not be destroyed by a refresh. Are you sure the dataset is bookmarkable?
What is the dataset SQL source? Maybe we can tweak that.
Cheers,
Paul
zysset
Nov 8th, 2000, 08:10 AM
Originally posted by paulw
I hadn't even realised that you could randomize the autonumber! Can you not change that behaviour?
I checked out ADO last night and the bookmarks should not be destroyed by a refresh. Are you sure the dataset is bookmarkable?
What is the dataset SQL source? Maybe we can tweak that.
Cheers,
Paul
okay, i've killed all the records of my db, and changed the behaviour to increment. (i hope i can re-import them later)
but now there is a new problem, i can't solve:
Private Function addRecord(ByRef ado As Adodc, ByRef col As Collection, ByRef fields() As String, Optional spec As String) As Variant
'On Error GoTo AddErr
Dim bookmark As Variant
Dim i As Integer
With ado
.Recordset.addNew
For i = 0 To (UBound(fields) - 1)
.Recordset.fields(fields(i)) = col.item(fields(i))
Next i
...
this method is called for each table. first time the method runs good, but second time the argument isn't passed!!!
i can direct call the method by <formname>.<adodc>.recordset.addnew but it isn't working with the passed reference. (nothing)
paulw
Nov 8th, 2000, 08:39 AM
E-Mail me your phone number at paul.whitfield@fortisbank.com and we can talk this through - it sounds odd.
Cheers,
Paul.
P.S. I'll talk you through the re-import as well...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.