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

Add a defined number of records to a table

NV
Hi !
I don't know if it's possible but I need to create in a table as many
records as a number entered in a form. (Example: In a form I write 4
and it creates 4 new records in a table).
In each of those records I need to paste a code of 3 leters and the
year (Example: In the same form I write in the field code ABC, and in
the field year 2006, and it enters those values to each of the 4 new
records).

Does anybody know how this can be done ?
Thank you all in advance

Nuno

Jan 3 '06 #1
8 4275
Red
You could use something like this:

For X = 1 to NumberEntered
Saverecords()
Next X
Private sub Saverecords()
dim rs as recordsource
set rs = currentdb.openrecordset("mytable")
rs.addnew
rs("field1") = me.myfield.value
etc etc etc
rs.update
end sub

Jan 3 '06 #2
No that's dreadful, you're opening and closing a table recordset X times.
Your objective with any data work should be:-
Get in
Do the work
Get out

.... as fast as you can.

Something like:-

Dim rs as dao.recordset
Dim intX as integer

set rs = currentdb.openrecordset("SELECT * FROM mytable WHERE 1=0")

For intX = 1 to NumberEntered
With rs
.addnew
.Fields("field1") = me.mycontrol.value
.update
End With
Next intX
rs.close
set rs = nothing

OR

Dim db as DAO.Database

Set db = currentdb

For intX = 1 to NumberEntered
db.Execute "INSERT INTO mytable(field1) VALUES(" & me.mycontrol.value &
")"
Next intX
set db = nothing

--
Terry Kreft

"Red" <do**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
You could use something like this:

For X = 1 to NumberEntered
Saverecords()
Next X
Private sub Saverecords()
dim rs as recordsource
set rs = currentdb.openrecordset("mytable")
rs.addnew
rs("field1") = me.myfield.value
etc etc etc
rs.update
end sub

Jan 3 '06 #3
On the other hand, I once had a performance problem using an ADO
recordset to insert 15000 rows into a table, switched to doing
individual INSERT queries in a loop, and dropped the run time from 15
minutes to a mintue and 10 seconds. I was not closing and reopening the
Connection object, however (equivalent to opening/closing the Database
obejct in this example).

Terry Kreft wrote:
No that's dreadful, you're opening and closing a table recordset X times.
Your objective with any data work should be:-
Get in
Do the work
Get out

... as fast as you can.

Something like:-

Dim rs as dao.recordset
Dim intX as integer

set rs = currentdb.openrecordset("SELECT * FROM mytable WHERE 1=0")

For intX = 1 to NumberEntered
With rs
.addnew
.Fields("field1") = me.mycontrol.value
.update
End With
Next intX
rs.close
set rs = nothing

OR

Dim db as DAO.Database

Set db = currentdb

For intX = 1 to NumberEntered
db.Execute "INSERT INTO mytable(field1) VALUES(" & me.mycontrol.value &
")"
Next intX
set db = nothing

Jan 3 '06 #4
Ten thousand new records in 14 seconds.

Sub AddRecords()
Dim r As ADODB.Recordset
Dim z As Long
Debug.Print Now() '2006-01-03 18:31:52
Set r = New ADODB.Recordset
With r
.CursorLocation = adUseClient
.Open _
"SELECT Field1, Field2, Field3 FROM Test WHERE False", _
CurrentProject.Connection, _
adOpenStatic, _
adLockBatchOptimistic
For z = 0 To 9999
r.AddNew Array(0, 1, 2), Array(z, Chr$(z Mod 256), Now())
Next z
.UpdateBatch
End With
Debug.Print Now() ' 2006-01-03 18:32:06
End Sub

Jan 3 '06 #5
131072 Records in three seconds (as per recommendation by Tom van
Stiphout).

Sub AddRecordsDAO()
Dim z As Long
DBEngine(0)(0).Execute "DELETE * FROM Test"
Debug.Print Now() '2006-01-03 21:20:57
With DBEngine(0)(0)
.Execute "INSERT INTO Test (Field1, Field2, Field3) VALUES
(Null, Null, Null)"
For z = 0 To 16
.Execute "INSERT INTO Test SELECT Field1, Field2, Field3
FROM Test"
Next z
End With
Debug.Print Now() '2006-01-03 21:21:00
End Su

Jan 4 '06 #6
NV
Hello !

Thank you all for your help, but as MS Access is prety new to me I
don't know how to enter those procedures into a form and make it read
the number I enter in a text box and consequently insert the
correspondent number of records.

Jan 4 '06 #7
As you're aware, that's slightly different though, personally I would go
with the INSERT statements first anyway as a first stab and then worry about
it if I was having speed issues. I tend not to use inserts/updates on
recordsets if I can avoid it. This is helped by the fact that I mainly work
unbound which gives you much greater flexibility in the way you work.

The point of my post (as I'm also aware you realise) was to point out that
the opening of the connection and creating the recordset X times was the
problem I saw not the iterative insert, you can't get away from iterating
the insert.
--
Terry Kreft

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:yp******************************@comcast.com. ..
On the other hand, I once had a performance problem using an ADO recordset
to insert 15000 rows into a table, switched to doing individual INSERT
queries in a loop, and dropped the run time from 15 minutes to a mintue
and 10 seconds. I was not closing and reopening the Connection object,
however (equivalent to opening/closing the Database obejct in this
example).

Terry Kreft wrote:
No that's dreadful, you're opening and closing a table recordset X times.
Your objective with any data work should be:-
Get in
Do the work
Get out

... as fast as you can.

Something like:-

Dim rs as dao.recordset
Dim intX as integer

set rs = currentdb.openrecordset("SELECT * FROM mytable WHERE 1=0")

For intX = 1 to NumberEntered
With rs
.addnew
.Fields("field1") = me.mycontrol.value
.update
End With
Next intX
rs.close
set rs = nothing

OR

Dim db as DAO.Database

Set db = currentdb

For intX = 1 to NumberEntered
db.Execute "INSERT INTO mytable(field1) VALUES(" & me.mycontrol.value
& ")"
Next intX
set db = nothing

Jan 4 '06 #8
On 3 Jan 2006 15:36:08 -0800, "Lyle Fairfield" <ly***********@aim.com>
wrote:
Ten thousand new records in 14 seconds.

Sub AddRecords()
Dim r As ADODB.Recordset
Dim z As Long
Debug.Print Now() '2006-01-03 18:31:52
Set r = New ADODB.Recordset
With r
.CursorLocation = adUseClient
.Open _
"SELECT Field1, Field2, Field3 FROM Test WHERE False", _
CurrentProject.Connection, _
adOpenStatic, _
adLockBatchOptimistic
For z = 0 To 9999
r.AddNew Array(0, 1, 2), Array(z, Chr$(z Mod 256), Now())
Next z
.UpdateBatch
End With
Debug.Print Now() ' 2006-01-03 18:32:06
End Sub


Given the same data is being inserted in each record, a SQL INSERT
can be quicker than using loops where there is some other table with a
large number of records.

eg INSERT INTO DestinationTable ( Period, Code ) Select Top 10000
2006, 'ABC' FROM LargeTable

P
Jan 4 '06 #9

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

Similar topics

1
by: Bryan Zash | last post by:
When querying a bit field, I am encountering a problem with MS SQL Server returning a larger number of records for a table than the actual number of records that exist within that table. For...
3
by: teddysnips | last post by:
In the script below is the DDL to create some tables and a UDF. What I'm interested in is the UDF at the end. Specifically, these few lines: --CLOSE OTRate --DEALLOCATE OTRate ELSE --...
8
by: Brenda J. | last post by:
Hello all I'm sure this is a relatively simple query to create, but I can't seem to get my head around it. Here is an example of the two tables I am using: tblAccountTypes ...
16
by: usenet | last post by:
I want to be able to use the record numbers of a sub-form, are they available anyhow in VB (Access 2003)? I want to use the sub-form record number as *part* of the primary key for the table...
2
by: Vadim | last post by:
Hi! I imported some table (about 1500 records) using "LOAD DATA LOCAL INFILE..." (command line console). No warnings, no skipped, no deletes - all the recored are written to be imported. However,...
7
by: john | last post by:
I have a form with 1 table. I would like to create a field in my form that shows the number of records (of the whole table) that have one particular value. Is that possible? I fiddled with a...
8
by: King | last post by:
Hi I have following MS Acess query Here is the query ID Name Prgm ID Client ID Date Start Time End Time Minutes C4 Trisha TIP DEK0703 7 /7 /2006...
12
by: M G Henry | last post by:
I have a tabbed form that contains 12 different "pages" and when I try and run the form I get the error message too many fields defined --- which I believe is the 255 field limit in the record...
5
by: slickdock | last post by:
I need to break my query into 3 groups: First 60 records (records 1-60) Next 60 records (records 61-121) Next 60 records (records 122-182) Of course I could use top values 60 for the first...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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.