Solved

Access VBA Adding multiple records to table

Posted on 2016-11-03
5
25 Views
Last Modified: 2016-11-03
Hello,
I would like to be able to add multiple records to a given table, based on a value on a user form (MyCopies).  The below code works, but just for a adding a single record.   Perhaps I am approaching it the wrong way?

Private Sub AddInstalls_Click()
Dim MyCopies
Dim MyID
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tblInstallsAndAuths")

Let MyID = Forms!frmAddNewSoftware!SoftwareID
Let MyCopies = Forms!frmAddNewSoftware!NumCopies

With rs
  .AddNew
    .Fields("SoftwareID") = MyID

  .Update
End With

rs.Close
Set rs = Nothing
End Sub

Thanks!
0
Comment
Question by:Not2ruthless
  • 3
  • 2
5 Comments
 

Author Comment

by:Not2ruthless
ID: 41873097
Let me try to clarify:  If the form field [NumCopies] says, 5, for example,  I would like the above exact steps to repeat 5 times.  Thanks!
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 41873102
you have to iterate thru the recordset

Private Sub AddInstalls_Click()
 Dim MyCopies
 Dim MyID
 Dim rs As DAO.Recordset
 Set rs = CurrentDb.OpenRecordset("tblInstallsAndAuths")

 Let MyID = Forms!frmAddNewSoftware!SoftwareID
 Let MyCopies = Forms!frmAddNewSoftware!NumCopies

Do until rs.eof
 With rs
   .AddNew
     .Fields("SoftwareID") = MyID

   .Update
 End With
rs.movenext
loop

 rs.Close
 Set rs = Nothing
 End Sub

Open in new window

0
 

Author Comment

by:Not2ruthless
ID: 41873125
That's cool!  Is there anyway, of just "iterating", for instance what the variable "MyCopies" states.  For instance, the form states 5 numcopies.  (The above added 4,000+ times) ?
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 41873129
Private Sub AddInstalls_Click()
 Dim MyCopies, j as integer
 Dim MyID
 Dim rs As DAO.Recordset
 Set rs = CurrentDb.OpenRecordset("tblInstallsAndAuths")

 Let MyID = Forms!frmAddNewSoftware!SoftwareID
 Let MyCopies = Forms!frmAddNewSoftware!NumCopies

'Do until rs.eof

for j=1 to  MyCopies
 With rs
   .AddNew
     .Fields("SoftwareID") = MyID

   .Update
 End With

next

'rs.movenext
'loop

 rs.Close
 Set rs = Nothing
 End Sub
0
 

Author Closing Comment

by:Not2ruthless
ID: 41873140
Thanks again, I am happy...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now