|
-
September 11th, 2001, 12:45 PM
#1
Adding Records to a DataBase
I am trying to add recored to a Access DataBase using ADO and am having trouble trying to get the Record ID of a newly created record.
My Field in the Table is named "ID" with autoincrement YES. Here is the simple code to create a new record which works.
Mainline routine calls OPenDB then AddnewRCD. the last statement in addnewRCD is supposed to return the unique "ID" field geenerated by the .Addnew but always returns a 2.
What stupid thing am I doing wrong?
' OPen the database
public Function OpenDB(DBName as string)
on error GoTo openerror
set DB = new Connection
set rs = new Recordset
DB.CursorLocation = adUseClient
DB.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBName & ";"
rs.Open "select * from DBRecords Order by ID", DB, adOpenStatic, adLockOptimistic
mvarDBName = DBName ' save for others to sue
mvarDBOpened = true ' Indicate DB opened
OpenDB = 1 ' no errors detected
Exit Function
'
'
Add a new record
public Function AddNewRCD()
rs.AddNew ' Add a new record to the dataBase
' set some default values in the Fields
rs.Fields("Left") = 1000
rs.Fields("Top") = 2000
rs.Fields("Width") = 3000
rs.Fields("Height") = 4000
rs.Fields("Text") = ""
rs.Update ' Update the record
rs.Requery ' reload to get record ID
AddNewRCD = rs.Fields("ID") ' Return Record number
End Function
John G
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|