PC Review


Reply
Thread Tools Rate Thread

Loop within Loop

 
 
Tom
Guest
Posts: n/a
 
      16th Dec 2006
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


 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      16th Dec 2006
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/Ap****ueBadWord.html
for more details.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Tom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Tom
Guest
Posts: n/a
 
      16th Dec 2006
Many thanks Doug for your input. Will experiement with both suggestions.

Tom
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
news:(E-Mail Removed)...
> 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/Ap****ueBadWord.html for more details.
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Tom" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      17th Dec 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Many thanks Doug for your input. Will experiement with both suggestions.
>
> Tom
> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
> news:(E-Mail Removed)...
>> 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/Ap****ueBadWord.html for more details.
>>
>> --
>> Doug Steele, Microsoft Access MVP
>> http://I.Am/DougSteele
>> (no private e-mails, please)
>>
>>
>> "Tom" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>

>>
>>

>
>



 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      17th Dec 2006
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
http://I.Am/DougSteele
(no private e-mails, please)


"Tom" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Many thanks Doug for your input. Will experiement with both suggestions.
>>
>> Tom
>> "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
>> news:(E-Mail Removed)...
>>> 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/Ap****ueBadWord.html for more details.
>>>
>>> --
>>> Doug Steele, Microsoft Access MVP
>>> http://I.Am/DougSteele
>>> (no private e-mails, please)
>>>
>>>
>>> "Tom" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create a For loop within a For loop? =?Utf-8?B?TGlua2luZyB0byBzcGVjaWZpYyBjZWxscyBpbiBw Microsoft Excel Programming 2 24th Jan 2005 09:05 AM
loop within a loop, sort of Dap Microsoft Access Form Coding 1 12th Apr 2004 05:47 PM
for...next loop within a while loop. Chieko Kuroda Microsoft Access Form Coding 1 2nd Feb 2004 08:34 PM
Worksheet_Change - loop within a loop =?Utf-8?B?Ymdt?= Microsoft Excel Programming 1 19th Jan 2004 02:27 PM
Infinite Loop Infinite Loop Infinite Loop Infinite Loop... Donna Windows XP Setup 0 8th Dec 2003 11:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:23 AM.