Hi again,
Just wondering if there is a way that I can insert a record in between two records in a table using code.
Andrew
5 2491
Hi again,
Just wondering if there is a way that I can insert a record in between two records in a table using code.
Andrew
To the best of my knowledge, the internal structure of Access will not allow Record Insertions between existing Records in a Table. Access will automatically Append Records to the end of the Table. Since in a Table, Access automatically displays Records in a Table in Ascending Order by the Table's Primary Key, you can programmatically add a Record to a Table between existing Primary Key values to attain the desired result. A simple case will illustrate this point. - 'Before Record Insertion
-
ID PK LastName FirstName
-
3 33 Hanks Tom
-
2 123 Flintstone Fred
-
1 345 Kellerman Sally
- 'After Record Insertion with Primary Key ([PK]) = 49
-
ID PK LastName FirstName
-
3 33 Hanks Tom
-
4 49 Melvin Harry
-
2 123 Flintstone Fred
-
1 345 Kellerman Sally
2 Methods for inserting Record(s) via code - Dim MySQL As String
-
-
DoCmd.SetWarnings False
-
MySQL = "INSERT INTO tblTest (ID, PK, LastName, FirstName) Values (4,49,'Melvin', 'Harry')"
-
DoCmd.RunSQL MySQL
-
DoCmd.SetWarnings True
- Dim MyDB As DAO.Database, MyRS As DAO.Recordset
-
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
-
-
With MyRS
-
.AddNew
-
![Id] = 4
-
![PK] = 49
-
![LastName] = "Flintstone"
-
![FirstName] = "Fred"
-
.Update
-
End With
-
-
MyRS.Close
-
Set MyRS = Nothing
-
NeoPa 32,556
Expert Mod 16PB
Hi again,
Just wondering if there is a way that I can insert a record in between two records in a table using code.
Andrew
I expect ADezii's last code snippet should have added Harry Melvin rather than re-adding Freddie Flintstone, but otherwise a very full and useful answer again from (one of) our best VBA experts.
@Andrew, you left us a little short of info if what you're trying to do is add a record to a table whose PK is an autonumber (just a wild guess that that's what you're looking for here). If so then you get into much more complicated territory, but the short answer is that you can't specify a key to add to an autonumber table I'm afraid.
If that's not your problem then you could hardly find a better answer than ADezii's on how to go about doing what you need.
yes, it has to do with an autonumber. No biggie, was just wondering if it could be done. Thanks again
Andrew
NeoPa 32,556
Expert Mod 16PB
No worries Andrew, but you'll find that a little extra preparation in the question will usually reap more appropriate answers and more quickly too.
I'm not trying to preach, just sharing what I've learnt from experience ;)
I expect ADezii's last code snippet should have added Harry Melvin rather than re-adding Freddie Flintstone, but otherwise a very full and useful answer again from (one of) our best VBA experts.
@Andrew, you left us a little short of info if what you're trying to do is add a record to a table whose PK is an autonumber (just a wild guess that that's what you're looking for here). If so then you get into much more complicated territory, but the short answer is that you can't specify a key to add to an autonumber table I'm afraid.
If that's not your problem then you could hardly find a better answer than ADezii's on how to go about doing what you need.
I expect ADezii's last code snippet should have added Harry Melvin rather than re-adding Freddie Flintstone, but otherwise a very full and useful answer again from (one of) our best VBA experts.
Thanks NeoPa, for pointing this out to me. I guess I've been watching too many cartoons of late. (LOL).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: PT |
last post by:
I got a problem. And thats.....
First of all, I got these three tables.
------------------- ------------------ ----------------------
tblPerson tblPersonSoftware ...
|
by: Thomas Bartkus |
last post by:
The meaning of REPLACE INTO is clear to me.
IF the new record presents new key values, then it is inserted as a new
record.
IF the new record has key values that match a pre-existing record, then...
|
by: george |
last post by:
This is like the bug from hell. It is kind of hard to explain, so
please bear with me.
Background Info: SQL Server 7.0, on an NT box, Active Server pages
with Javascript, using ADO objects.
...
|
by: Philip Boonzaaier |
last post by:
I want to be able to generate SQL statements that will go through a list of
data, effectively row by row, enquire on the database if this exists in the
selected table- If it exists, then the colums...
|
by: Bri |
last post by:
Greetings,
I'm having a very strange problem in an AC97 MDB with ODBC Linked tables
to SQL Server 7. The table has an Identity field and a Timestamp field.
The problem is that when a new record...
|
by: Carl |
last post by:
Hi,
I hope someone can share some of their professional advice and help me out
with my embarissing problem concerning an Access INSERT query. I have never
attempted to create a table with...
|
by: authorking |
last post by:
I use the following code to insert a data record in to a datatable of an
access database.But every time I execute the command, there will rise an
exception and the insert operation can't be...
|
by: Mike Hnatt |
last post by:
My goal is to get data from an XML file into a couple of tables in an Access
database. The XML file is a little complex so I need control over what I do
(I can't just read it into a dataset).
...
|
by: Shapper |
last post by:
Hello,
I have created 3 functions to insert, update and delete an Access
database record. The Insert and the Delete code are working fine.
The update is not. I checked and my database has all...
|
by: MLH |
last post by:
Suppose, in a multi-user environment, you
have append query SQL in a VBA procedure
that looks like INSERT INTO MyTable...
and the next line reads MyVar=DMax("","MyTable...
You can never be...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |