Automatically generates multiple records

How can we generate multiple records automatically in ms access. For example I would like to have a form which has two fields- "start voucher no" and "end voucher no" and a generate button. So when i enter start voucher no: 1 and end voucher no: 25 and press generate button, i want to append 25 records in my invoice table with respective invoice numbers.
Please help.
Sagar PrasaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
Hi,

You'll need to provide more information about what you want to achieve, the more precise your question, the better our answer.

As it stand, one possible solution is to use a recordset and a loop:
Dim db As DAO.Database
Set db = currentdb

Dim rs As DAO.Recordset
Set rs = db.tabledefs("myTable").openRecordset

Dim i As Long
For i = Clng(startVoucherNo.Value) To Clng(endVoucherNo.Value)
    rs.addNew
    rs.fields("field1").Value = "myFirstData" & i
    rs.fields("field2").Value = "mySecondData" & i
    rs.update
Next
rs.close
set rs = Nothing
set db = Nothing

Open in new window

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I'm also curious as to why you want to do this. Generally speaking, you'd add an Invoice record, and then multiple detail records based on input from the user, not automatically. If you're trying to setup "recurring billing", or something like that, then generally there's a better way.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeCommented:
I occasionally do this with course databases, where you have a course with a maximum of N students.  Sometimes the users will want to print reports or have records in the table for all seats, whether they are filled or not.  I don't generally endorse doing this, preferring to add each record as it is needed, but when they want this, this is what I do.

I have a table (tbl_Numbers) which exists in every one of my databases.  This table contains a single field (lngNumber) and 10 records (values 0-9).  With this table, I can create a query which will generate as many consecutive numbers as I need, something like:
SELECT Tens.lngNumber * 10 + Ones.lngNumber as lngNumber
FROM tbl_Numbers as Tens, tbl_Numbers as Ones

Open in new window

This generates a recordset with values 0 to 99.  In your case, I would modify this query as follows:
SELECT Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber as lngNumber
FROM tbl_Numbers as Tens, tbl_Numbers as Ones
WHERE Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber <= forms!yourFormName.txtVoucherEnd

Open in new window

That will give you a list of the voucher numbers you are looking for.  Then you simply add an insert clause to that query:
INSERT INTO yourTable (VoucherNumber)
SELECT Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber as lngNumber
FROM tbl_Numbers as Tens, tbl_Numbers as Ones
WHERE Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber <= forms!yourFormName.txtVoucherEnd

Open in new window

Of course, I would have code in the button that would ensure that there are values in txtVoucherStart and txtVoucherEnd, and that the value of txtVoucherEnd > txtVoucherStart.

In the case of my course list, it would look something like:
INSERT INTO tbl_CourseSessionSeats (CourseID, SessionID, SeatNumber)
SELECT forms!myFormName!cboCourseID, Forms!myFormName.cboSessionID, Tens.lngNumber * 10 + Ones.lngNumber
FROM tbl_Numbers as Tens, tbl_Numbers as Ones
WHERE Tens.lngNumber * 10 + Ones.lngNumber <= Forms!myFormName.txtSessionSeats

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Sagar PrasaiAuthor Commented:
@Dale Fye - Thanks for your answer. I tried your method but it doesn't generates sequential numbers. For example, I made a form with just two fields, start and end. I entered 204 for start and 240 for end. Then I used query with the sql command you have given (with proper changes as per my database). I wanted the query to generate- 204, 205, 206,....,238, 239,240. But the query generated- 204,214,224 etc. I have attached the screen shot of the query result as well.The query doesn't generate sequential numbers.
0
Dale FyeCommented:
Add an order by clause in the query:
INSERT INTO yourTable (VoucherNumber)
SELECT Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber as lngNumber
FROM tbl_Numbers as Tens, tbl_Numbers as Ones
WHERE Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber <= forms!yourFormName.txtVoucherEnd
ORDER BY Forms!yourFormName!txtVoucherStart + Tens.lngNumber * 10 + Ones.lngNumber

Open in new window

0
Sagar PrasaiAuthor Commented:
This worked for me. Thank you so much experts.
0
Sagar PrasaiAuthor Commented:
The suggestion provided by Dale Fye helped me achieved my solution for project I was working on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.