Loop within Loop

T

Tom

Hi

I have a select query, qrySelectMembers based on a table tblMembers wherein
1 field is named MemberID.

Within that query I am able select members by different criteria.

A second table, tblLog has 1 field named MemberID, 1 field named date & 1
field named Notes

What I want to achieve is to loop through qrySelectMembers and for each
MemberID to enter that MemberID, today's date, and in the Notes field the
name of a document [which has been selected from a combo box on the current
form]

A code example on how to achieve this would be very much appreciated.

TIA

Tom
 
D

Douglas J. Steele

You should be able to use a simple INSERT INTO query:

Dim strSQL As String

strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"SELECT MemberID, " & Format(Date(), "#\mm\/dd\/yyyy\#") & _
", '" & Me.MyComboBox " & "' FROM qrySelectMembers"
CurrentDb.Execute strSQL, dbFailOnError

Remember that given a choice between a simple SQL statement and a VBA loop,
the SQL statement will almost always be more efficient.

For the sake of completeness, though, I'll show a couple of ways in which
you could use VBA. One is to create a recordset based on qrySelectMembers,
loop through it, and run an INSERT INTO statement for each record:

Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr = dbCurr.OpenRecordset("qrySelectMembers")
Do Until rsCurr.EOF
strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"VALUES(" & rsCurr!MemberID & ", " & _
Format(Date(), "\#mm\/dd\/yyyy\#") & ", '" & _
Me.MyComboBox & ")"
dbCurr.Execute strSQL, dbFailOnError
rsCurr.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Another would open a recordset based on tblLog and add recordset to that
recordset:

Dim dbCurr As DAO.Database
Dim rsCurr1 As DAO.Recordset
Dim rsCurr2 As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr1 = dbCurr.OpenRecordset("qrySelectMembers")

strSQL = "SELECT MemberID, LogDate, LogNote FROM tblLog " & _
"WHERE False"
Set rsCurr2 = dbCurr.OpenRecordset(strSQL)

Do Until rsCurr1.EOF
rsCurr2.AddNew
rsCurr2!MemberID = rsCurr1!MemberID
rsCurr2!LogDate = Date()
rsCurr2!LogNote = Me.MyComboBox
rsCurr2!Update
rsCurr1.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Note that I renamed two of the fields in tblLog in my sample code. Both Date
and Note are reserved words, and shouldn't be used for your own purposes.
See what Allen Browne has at http://www.allenbrowne.com/AppIssueBadWord.html
for more details.
 
T

Tom

Many thanks Doug for your input. Will experiement with both suggestions.

Tom
Douglas J. Steele said:
You should be able to use a simple INSERT INTO query:

Dim strSQL As String

strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"SELECT MemberID, " & Format(Date(), "#\mm\/dd\/yyyy\#") & _
", '" & Me.MyComboBox " & "' FROM qrySelectMembers"
CurrentDb.Execute strSQL, dbFailOnError

Remember that given a choice between a simple SQL statement and a VBA
loop, the SQL statement will almost always be more efficient.

For the sake of completeness, though, I'll show a couple of ways in which
you could use VBA. One is to create a recordset based on qrySelectMembers,
loop through it, and run an INSERT INTO statement for each record:

Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr = dbCurr.OpenRecordset("qrySelectMembers")
Do Until rsCurr.EOF
strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"VALUES(" & rsCurr!MemberID & ", " & _
Format(Date(), "\#mm\/dd\/yyyy\#") & ", '" & _
Me.MyComboBox & ")"
dbCurr.Execute strSQL, dbFailOnError
rsCurr.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Another would open a recordset based on tblLog and add recordset to that
recordset:

Dim dbCurr As DAO.Database
Dim rsCurr1 As DAO.Recordset
Dim rsCurr2 As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr1 = dbCurr.OpenRecordset("qrySelectMembers")

strSQL = "SELECT MemberID, LogDate, LogNote FROM tblLog " & _
"WHERE False"
Set rsCurr2 = dbCurr.OpenRecordset(strSQL)

Do Until rsCurr1.EOF
rsCurr2.AddNew
rsCurr2!MemberID = rsCurr1!MemberID
rsCurr2!LogDate = Date()
rsCurr2!LogNote = Me.MyComboBox
rsCurr2!Update
rsCurr1.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Note that I renamed two of the fields in tblLog in my sample code. Both
Date and Note are reserved words, and shouldn't be used for your own
purposes. See what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html for more details.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Tom said:
Hi

I have a select query, qrySelectMembers based on a table tblMembers
wherein 1 field is named MemberID.

Within that query I am able select members by different criteria.

A second table, tblLog has 1 field named MemberID, 1 field named date & 1
field named Notes

What I want to achieve is to loop through qrySelectMembers and for each
MemberID to enter that MemberID, today's date, and in the Notes field the
name of a document [which has been selected from a combo box on the
current form]

A code example on how to achieve this would be very much appreciated.

TIA

Tom
 
T

Tom

Doug

Used your 3rd option but had to change
rsCurr2!Update

to

rsCurr2.Update

for it to work

Many thanks for your help

Tom
Tom said:
Many thanks Doug for your input. Will experiement with both suggestions.

Tom
Douglas J. Steele said:
You should be able to use a simple INSERT INTO query:

Dim strSQL As String

strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"SELECT MemberID, " & Format(Date(), "#\mm\/dd\/yyyy\#") & _
", '" & Me.MyComboBox " & "' FROM qrySelectMembers"
CurrentDb.Execute strSQL, dbFailOnError

Remember that given a choice between a simple SQL statement and a VBA
loop, the SQL statement will almost always be more efficient.

For the sake of completeness, though, I'll show a couple of ways in which
you could use VBA. One is to create a recordset based on
qrySelectMembers, loop through it, and run an INSERT INTO statement for
each record:

Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr = dbCurr.OpenRecordset("qrySelectMembers")
Do Until rsCurr.EOF
strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"VALUES(" & rsCurr!MemberID & ", " & _
Format(Date(), "\#mm\/dd\/yyyy\#") & ", '" & _
Me.MyComboBox & ")"
dbCurr.Execute strSQL, dbFailOnError
rsCurr.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Another would open a recordset based on tblLog and add recordset to that
recordset:

Dim dbCurr As DAO.Database
Dim rsCurr1 As DAO.Recordset
Dim rsCurr2 As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr1 = dbCurr.OpenRecordset("qrySelectMembers")

strSQL = "SELECT MemberID, LogDate, LogNote FROM tblLog " & _
"WHERE False"
Set rsCurr2 = dbCurr.OpenRecordset(strSQL)

Do Until rsCurr1.EOF
rsCurr2.AddNew
rsCurr2!MemberID = rsCurr1!MemberID
rsCurr2!LogDate = Date()
rsCurr2!LogNote = Me.MyComboBox
rsCurr2!Update
rsCurr1.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Note that I renamed two of the fields in tblLog in my sample code. Both
Date and Note are reserved words, and shouldn't be used for your own
purposes. See what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html for more details.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Tom said:
Hi

I have a select query, qrySelectMembers based on a table tblMembers
wherein 1 field is named MemberID.

Within that query I am able select members by different criteria.

A second table, tblLog has 1 field named MemberID, 1 field named date &
1 field named Notes

What I want to achieve is to loop through qrySelectMembers and for each
MemberID to enter that MemberID, today's date, and in the Notes field
the name of a document [which has been selected from a combo box on the
current form]

A code example on how to achieve this would be very much appreciated.

TIA

Tom
 
D

Douglas J. Steele

Sorry, yes, that was a typo on my part.

I still say, though, that you'd be far better off using straight SQL, and
not looping.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Tom said:
Doug

Used your 3rd option but had to change
rsCurr2!Update

to

rsCurr2.Update

for it to work

Many thanks for your help

Tom
Tom said:
Many thanks Doug for your input. Will experiement with both suggestions.

Tom
Douglas J. Steele said:
You should be able to use a simple INSERT INTO query:

Dim strSQL As String

strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"SELECT MemberID, " & Format(Date(), "#\mm\/dd\/yyyy\#") & _
", '" & Me.MyComboBox " & "' FROM qrySelectMembers"
CurrentDb.Execute strSQL, dbFailOnError

Remember that given a choice between a simple SQL statement and a VBA
loop, the SQL statement will almost always be more efficient.

For the sake of completeness, though, I'll show a couple of ways in
which you could use VBA. One is to create a recordset based on
qrySelectMembers, loop through it, and run an INSERT INTO statement for
each record:

Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr = dbCurr.OpenRecordset("qrySelectMembers")
Do Until rsCurr.EOF
strSQL = "INSERT INTO tblLog (MemberID, LogDate, LogNote) " & _
"VALUES(" & rsCurr!MemberID & ", " & _
Format(Date(), "\#mm\/dd\/yyyy\#") & ", '" & _
Me.MyComboBox & ")"
dbCurr.Execute strSQL, dbFailOnError
rsCurr.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Another would open a recordset based on tblLog and add recordset to that
recordset:

Dim dbCurr As DAO.Database
Dim rsCurr1 As DAO.Recordset
Dim rsCurr2 As DAO.Recordset
Dim strSQL As String

Set dbCurr = CurrentDb()
Set rsCurr1 = dbCurr.OpenRecordset("qrySelectMembers")

strSQL = "SELECT MemberID, LogDate, LogNote FROM tblLog " & _
"WHERE False"
Set rsCurr2 = dbCurr.OpenRecordset(strSQL)

Do Until rsCurr1.EOF
rsCurr2.AddNew
rsCurr2!MemberID = rsCurr1!MemberID
rsCurr2!LogDate = Date()
rsCurr2!LogNote = Me.MyComboBox
rsCurr2!Update
rsCurr1.MoveNext
Loop

rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing

Note that I renamed two of the fields in tblLog in my sample code. Both
Date and Note are reserved words, and shouldn't be used for your own
purposes. See what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html for more details.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Hi

I have a select query, qrySelectMembers based on a table tblMembers
wherein 1 field is named MemberID.

Within that query I am able select members by different criteria.

A second table, tblLog has 1 field named MemberID, 1 field named date &
1 field named Notes

What I want to achieve is to loop through qrySelectMembers and for each
MemberID to enter that MemberID, today's date, and in the Notes field
the name of a document [which has been selected from a combo box on the
current form]

A code example on how to achieve this would be very much appreciated.

TIA

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Adding New Record with Time Stamp 1
Pls help, my VBA does not work 3
Join Query 2
Populate form from list box selection 4
Listbox question 1
Help - Loop within a loop code 3
Append query help 2
need help with Dsum 1

Top