473,769 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get CustomerID of New Record After Recordset.Addne w

Sat I have a customers table with the fields CustomerID and Customer and I use
the recordset.addne w method to add a new record to the table. What is the best
way to get the CustomerID of the new record after recordset.updat e is executed?
Recordset!Custo merID doesn't work because the recordset bookmark is on the first
record. Moving to the last record doesn't work if the recordset is based on a
query and the recordset is sorted.

Thanks!

Harold
Nov 13 '05 #1
2 15577
If this is a DAO recordset, Access sets the LastModified bookmark, so:
rs.Bookmark = rs.LastModified
Debug.Print rs!CustomerID

If this data is in an Access table, the autonumber is assigned as soon as
you start to add the record, so you can even get the number before the
Update if you wish:
rs.AddNew
Debug.Print rs!CustomerID

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Harold" <hm*****@earthl ink.net> wrote in message
news:3v******** **********@news read2.news.atl. earthlink.net.. .
Sat I have a customers table with the fields CustomerID and Customer and I use the recordset.addne w method to add a new record to the table. What is the best way to get the CustomerID of the new record after recordset.updat e is executed? Recordset!Custo merID doesn't work because the recordset bookmark is on the first record. Moving to the last record doesn't work if the recordset is based on a query and the recordset is sorted.

Thanks!

Harold

Nov 13 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If you're using DAO recordsets you can use the .LastModified bookmark to
get to the last record entered. Here is an example code from the Help
article LastModified Property (DAO):

Function AddName(rstTemp As Recordset, _
strFirst As String, strLast As String)

' Adds a new record to a Recordset using the data passed
' by the calling procedure. The new record is then made
' the current record.
With rstTemp
.AddNew
!FirstName = strFirst
!LastName = strLast
.Update
.Bookmark = .LastModified
End With

End Function

After ".Bookmark = .LastModified" you could also do something like this:

.Bookmark = .LastModified
lngCustomerID = !CustomerID

lngCustomerID would be the Customer ID of the last entered record. You
could return the lngCustomerID value as the function's return value.

--
MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQQkjx4echKq OuFEgEQIvqACgv5 pVWgWcM8NRwHUhp 85K7Ck8iqkAn3/Y
ZTnYQlzkbtDXnba L0Le4ZZaz
=z3aa
-----END PGP SIGNATURE-----
Harold wrote:
Sat I have a customers table with the fields CustomerID and Customer and I use
the recordset.addne w method to add a new record to the table. What is the best
way to get the CustomerID of the new record after recordset.updat e is executed?
Recordset!Custo merID doesn't work because the recordset bookmark is on the first
record. Moving to the last record doesn't work if the recordset is based on a
query and the recordset is sorted.


Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
19932
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer MadminRS.CursorType = adOpenKeyset MadminRS.LockType = adLockOptimistic MadminRS.Open "NavBar", objConn, , , adCmdTable MadminRS.AddNew MadminRS("Url") = Request.Form("Website") MadminRS("ParentRecNo") = 0
1
3336
by: Ryan | last post by:
Hello, I have a quick question (I hope). I have a form with a combo box and a multi-selection list box. The list box is based on a query. Users can select values from the cmbobox to add to the list box (items they wish inserted into the table), or they can select items in the list box to delete (from the table). Once they have finished making any changes, they are to click a SAVE button to actually commit the changes to the database....
1
5461
by: | last post by:
I am trying to add a new record to an odbc data base. I am using MS ActiveX library2.8. The syntax is recordset.AddNew (object fieldlist, object values). Object fieldlist and values can be one field/value or can be an array of fields and values. My problem is I do not know how to set up the array for this to work. I try the following: string fn = new string={"Name_of_the_field", "Name_of_the_field"}; string fv=new string =...
5
25462
by: tony010409020622 | last post by:
I just spent 4 months taking a dotnet class where i learned very little. One of the things I did not learn is this: What are the dotnet equivilents of commands such as: Adodc1.Recordset.AddNew Adodc1.Recordset.Update Adodc1.Recordset.MoveFirst Adodc1.Recordset.MoveNext Adodc1.Recordset.Delete
5
6492
by: sudarp | last post by:
hello, I am wirting a function in VBA that adds a new record to an existing database. If i try to add a record with a primary key that already exists in the database, i get an error. I was wondering if there was some way I could make AddNew overwrite any old entry with the same primary key...I was hoping I wouldn't have to iterate through the entire database each time I add an entry. thanks
7
5990
by: michal | last post by:
hi guys, i am using postgres 8.2 and try to add some records to a table with ADODO ... unfortunately i end up with an error when calling the addNew() method. it says that the recordset does not support updating... i do open the connection the following set RS = server.createObject("scripting.dictionary") RS.open "usr", cn, 1, 3, 2
5
2341
by: dkintheuk | last post by:
I have a question regarding how i can refer to the fields that I want to add into my dataset Each of my fields in my rawtext file that I am going to import appears in the form: "FieldName:FieldValue" with a comma between each ietm in a record.
2
2115
by: Kamil | last post by:
Hi. Is it possible to add new record to my recordset which is based on a query? I want to use this recordset only for temp starage of data, and I don't want to update values from this recordset to a table. Code: Q2 = "SELECT T_SMP_RCNR.KEY_ORDER, T_SMP_RCNR.RCNR, T_SMP_RCNR.KEY_RCNR " & _ "FROM T_SMP_RCNR LEFT JOIN Q_MaxOfSMPRCNR ON
0
1680
by: richkid | last post by:
the following code works but if I try to add a second record it erases the first record in the database (using Access DB) ... Me.Adodc1.Recordset.AddNew Me.Adodc1.Recordset.Fields("Account").Value = Me.txtACNum.Text Me.Adodc1.Recordset.Fields("Title").Value = Me.cboTitle.Text Me.Adodc1.Recordset.Fields("First_Name").Value = Me.txtFName.Text Me.Adodc1.Recordset.Fields("Last_Name").Value = Me.txtLName.Text ...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.