473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

insert a record

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
Oct 14 '07 #1
5 2491
ADezii
8,834 Expert 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. 'Before Record Insertion 
  2. ID    PK     LastName     FirstName
  3. 3    33        Hanks       Tom
  4. 2    123    Flintstone    Fred
  5. 1    345    Kellerman    Sally
Expand|Select|Wrap|Line Numbers
  1. 'After Record Insertion with Primary Key ([PK]) = 49
  2. ID    PK      LastName        FirstName
  3. 3    33         Hanks          Tom
  4. 4    49         Melvin          Harry
  5. 2    123     Flintstone       Fred
  6. 1    345     Kellerman       Sally
2 Methods for inserting Record(s) via code
Expand|Select|Wrap|Line Numbers
  1. Dim MySQL As String
  2.  
  3. DoCmd.SetWarnings False
  4.   MySQL = "INSERT INTO tblTest (ID, PK, LastName, FirstName) Values (4,49,'Melvin', 'Harry')"
  5.   DoCmd.RunSQL MySQL
  6. DoCmd.SetWarnings True
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  2.  
  3. Set MyDB = CurrentDb()
  4. Set MyRS = MyDB.OpenRecordset("tblTest", dbOpenDynaset)
  5.  
  6. With MyRS
  7.   .AddNew
  8.     ![Id] = 4
  9.     ![PK] = 49
  10.     ![LastName] = "Flintstone"
  11.     ![FirstName] = "Fred"
  12.   .Update
  13. End With
  14.  
  15. MyRS.Close
  16. Set MyRS = Nothing
  17.  
Oct 14 '07 #2
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.
Oct 14 '07 #3
yes, it has to do with an autonumber. No biggie, was just wondering if it could be done. Thanks again

Andrew
Oct 14 '07 #4
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 ;)
Oct 14 '07 #5
ADezii
8,834 Expert 8TB
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).
Oct 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
1
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...
2
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. ...
16
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...
8
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...
8
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...
4
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...
4
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). ...
3
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
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,...
0
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...
0
agi2029
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,...
0
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...

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.