473,386 Members | 1,679 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,386 software developers and data experts.

How do I add consecutively numbered records into a table?

lilp32
43
I am trying to add records into an Access 2010 table. Each time, I need to add 10 records (numbered 1-10). All other fields are the same for all 10 records.

The fields I am using are: SampleNumber (autonumber), SampleSite (always numbered 1-10), CollectionDate (date), StudyDay (number), ID (number).

For example, on CollectionDate 3/14/13, we collect 10 samples for ID 20. All samples have the same CollectionDate, StudyDay and ID but are numbered 1-10.

Ideally I would enter each static field plus 1 for SampleSite once and the program would loop through adding 1 to SampleSite until 10 records are created.

I've tried the INSERT INTO operation and have figured out how to copy the rows exactly but need help with the consecutive numbering. I imagine this would be done with a loop?
Mar 14 '13 #1

✓ answered by Seth Schrock

There are two ways you could do this, both involving a loop. Here is the basic framework.
Expand|Select|Wrap|Line Numbers
  1. Dim i as integer
  2.  
  3. For i = 1 to 10
  4.  
  5.  
  6.     'Removed bad code
  7. Next
This will loop through the code 10 times, each time adding 1 to the current value. Now you could either run an INSERT query inside the loop using the value of i to give you your 1 to 10 values. The second method would be to use a recordset and its .AddNew method. I'm not totally sure which one is better, but I would probably go with the second option. Here is a link that tells how to use it: MSDN Recordset.AddNew Method. Let us know if you need help with it.

7 2941
Seth Schrock
2,965 Expert 2GB
There are two ways you could do this, both involving a loop. Here is the basic framework.
Expand|Select|Wrap|Line Numbers
  1. Dim i as integer
  2.  
  3. For i = 1 to 10
  4.  
  5.  
  6.     'Removed bad code
  7. Next
This will loop through the code 10 times, each time adding 1 to the current value. Now you could either run an INSERT query inside the loop using the value of i to give you your 1 to 10 values. The second method would be to use a recordset and its .AddNew method. I'm not totally sure which one is better, but I would probably go with the second option. Here is a link that tells how to use it: MSDN Recordset.AddNew Method. Let us know if you need help with it.
Mar 14 '13 #2
Rabbit
12,516 Expert Mod 8TB
@Seth, you would not want line 6 in there. The for loop will automatically increment the variable.
Mar 14 '13 #3
lilp32
43
Thanks, but I think I am missing how to insert the variables through the SQL query.

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim CollectionDate As Date
  3. Dim StudyDay As Integer
  4. Dim ID As Integer
  5.  
  6. CollectionDate = InputBox("Enter date", "Date", Date)
  7. StudyDay = InputBox("Enter study day")
  8. ID = InputBox("Patient Number")
  9.  
  10. For i = 1 To 10
  11.  
  12. DoCmd.RunSQL "INSERT INTO tblSamples (SampleSite,CollectionDate,StudyDay,ID) VALUES (i, CollectionDate, StudyDay, ID)"
  13.  
  14. Next
  15.  
Mar 14 '13 #4
lilp32
43
I got it to work!!!

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim CollectionDate As Date
  3. Dim StudyDay As Integer
  4. Dim ID As Integer
  5.  
  6. CollectionDate = InputBox("Enter date", "Date", Date)
  7. StudyDay = InputBox("Enter study day")
  8. ID = InputBox("Patient Number")
  9.  
  10. For i = 1 To 10
  11.  
  12. DoCmd.SetWarnings false
  13. DoCmd.RunSQL "INSERT INTO tblSamples (SampleSite,CollectionDate,StudyDay,ID) VALUES ("& i &","& CollectionDate &","& StudyDay &","& ID &")"
  14.  
  15. Next
  16.  
Mar 14 '13 #5
Seth Schrock
2,965 Expert 2GB
Duh. Can't believe I did that. I guess I'm too used to working with Do While loops.
Mar 14 '13 #6
lilp32
43
Now my problem is that the date is not being inserted correctly. I've tried changing the default date to Format(Now(), "m/d/yyyy") or removing it altogether but the date is still being inserted as 12/30/1899 or 12:00:09 AM.
Mar 14 '13 #7
Seth Schrock
2,965 Expert 2GB
Try using the Date() function instead of the Now(). If you continue to have issues with this, you should start a new thread.
Mar 14 '13 #8

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

Similar topics

1
by: Eranut | last post by:
Hi, I am trying to understand if there is a way to send a table of records (table of structs) from pro C to Oracle pl/sql stored procedure. Since Oracle is demanding definition of the *sent*...
9
by: paul | last post by:
Hiya everyone, I have two tables in SQL 2000. I would like to append the contents of TableA to TableB. Table A has around 1.1 Million Records. Table B has around 1 Million Reocords. ...
5
by: Alex | last post by:
Hi, I have two tables , A and B where table B has a foreign key constraint to table A. I want to delete all records in table A that are older than a certain date that are not referenced by...
2
by: Jason | last post by:
I have a table of 650,000 records associated to contracts. Some of the contracts have multiple records but each contract only has one Active Record (there might be several inactive records). ...
6
by: John | last post by:
I've got a single table I need to query to return records that have no "related records." Table dataset example: 1. John, Biology 2. Dave, Math 3. Susan, Biology 4. Betty, Sociology The...
9
by: Dennis Gearon | last post by:
I am designing something that may be the size of yahoo, google, ebay, etc. Just ONE many to many table could possibly have the following characteristics: 3,600,000,000 records each record is...
18
by: shinyo21 | last post by:
Anyone know how to create a SQL or way in Access for deleting few tables' record. I means is delete all the records in specify few tables. I tried to use query to delete the records' table but...
1
by: apprenticewizard | last post by:
Please help....been at this for several days and just can't seem to find a solution. I've got two tables, Emp Info and Labor Records. The form to populate the Labor Records table contains a combo box...
10
by: jambonjamasb | last post by:
Hi all, I am new to this and am looking for some direction I have the ideas, but am having trouble putting it into practice. Any help would be greatly appreciated. I have set up three tables. ...
2
by: Paula Fox | last post by:
Recently, my access database main records table was "cloned" and is now being hosted on a SQL server as a linked table to my existing Access database. Now, the Record search box (in the form) does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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...

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.