Help - Loop within a loop code

T

Tom

Appreciate some assistance with some code please.

On form I have a combo box which will list a number of companies. The data
source for that combo box is a table wherein 1 field is called Company and a
2nd field called GroupNo. The afterupdate code for the combo box will lookup
will lookup the GroupNo [the number of departments for that company e.g.
5].

What I want to achieve is that after selecting the company, the client will
click on a print button, a input box will pop up and ask the number of
copies required. The user will input a number e.g. 3 and what would happen
then is that for each of the 5 departments 3 copies of a report would be
printed.

The code I have been working on is:


Dim strMsg As String, intStyle As String, strTitle As String, DgDef As
String, Response As String
Dim stDocName As String, strRecRef As String
Dim Default As Integer, intCheck As Integer, intCounter As Integer,
intCopies As Integer, intValue As Integer
Dim intGroupNo As Integer, intCheckGroupNo As Integer, intGroupCounter As
Integer, intCom As Double


strMsg = "Do you want to print a REMINDER to " &
Forms!frmConatctHistory![Client] & " for this months Management Fees?"
intStyle = vbYesNo + vbQuestion + vbDefaultButton2
strTitle = DBTitle

Response = MsgBox(strMsg, intStyle, strTitle)

If Response = vbYes Then

strMsg = "Enter copies required"
strTitle = "Print copies"

Default = "3"
intValue = InputBox(strMsg, strTitle, Default)
Forms!frmOwnerHistory![Copies] = intValue
intCopies = intValue

Me![GroupNo] = DCount("LanGroup", "qryChkLanGroup")

' MsgBox Me![GroupNo] & " A"

If Me![GroupNo] > "1" Then intGroupNo = Me![GroupNo]


GoTo PrintGroupNo

End If


Me![Park].SetFocus

PrintGroupNo:

Do
Do While intGroupNo > 0
intGroupNo = intGroupNo - 1

GoTo PrintReport

Loop
Loop Until intGroupNo = 0

If intGroupNo = 0 Then

Exit Sub

End If

PrintReport:

If intCopies >= "0" Then
intCheck = -1: intCounter = intCopies

Do
Do While intCounter < 99
intCounter = intCounter - 1
Me![Copies] = intCounter

stDocName = "rptManagementReport_Multiple"

DoCmd.OpenReport stDocName, acPreview 'Normal

If intCopies = 0 Then

intCheck = 0
Exit Do
GoTo PrintGroupNo

End If

Loop

Loop Until intCheck = 0

End If

Where I am having problems is that whilst the code goes through the 1st loop
it just continually loops in the 2nd instead of going back to the 1st loop,
getting the groupno, then going to printreport to print the required number
and then back to groupno again and repeating until for each group number the
required number of reports are printed.

Or is there a better way to achieve this?

TIA

Tom
 
D

Dorian

'GoTo PrintReport'
will never return to the place where it was run.
You need to replace this with a 'Call' to a procedure.
Make your PrintReport logic enclosed in a procedure that can be called
whenever needed.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".


Tom said:
Appreciate some assistance with some code please.

On form I have a combo box which will list a number of companies. The data
source for that combo box is a table wherein 1 field is called Company and a
2nd field called GroupNo. The afterupdate code for the combo box will lookup
will lookup the GroupNo [the number of departments for that company e.g.
5].

What I want to achieve is that after selecting the company, the client will
click on a print button, a input box will pop up and ask the number of
copies required. The user will input a number e.g. 3 and what would happen
then is that for each of the 5 departments 3 copies of a report would be
printed.

The code I have been working on is:


Dim strMsg As String, intStyle As String, strTitle As String, DgDef As
String, Response As String
Dim stDocName As String, strRecRef As String
Dim Default As Integer, intCheck As Integer, intCounter As Integer,
intCopies As Integer, intValue As Integer
Dim intGroupNo As Integer, intCheckGroupNo As Integer, intGroupCounter As
Integer, intCom As Double


strMsg = "Do you want to print a REMINDER to " &
Forms!frmConatctHistory![Client] & " for this months Management Fees?"
intStyle = vbYesNo + vbQuestion + vbDefaultButton2
strTitle = DBTitle

Response = MsgBox(strMsg, intStyle, strTitle)

If Response = vbYes Then

strMsg = "Enter copies required"
strTitle = "Print copies"

Default = "3"
intValue = InputBox(strMsg, strTitle, Default)
Forms!frmOwnerHistory![Copies] = intValue
intCopies = intValue

Me![GroupNo] = DCount("LanGroup", "qryChkLanGroup")

' MsgBox Me![GroupNo] & " A"

If Me![GroupNo] > "1" Then intGroupNo = Me![GroupNo]


GoTo PrintGroupNo

End If


Me![Park].SetFocus

PrintGroupNo:

Do
Do While intGroupNo > 0
intGroupNo = intGroupNo - 1

GoTo PrintReport

Loop
Loop Until intGroupNo = 0

If intGroupNo = 0 Then

Exit Sub

End If

PrintReport:

If intCopies >= "0" Then
intCheck = -1: intCounter = intCopies

Do
Do While intCounter < 99
intCounter = intCounter - 1
Me![Copies] = intCounter

stDocName = "rptManagementReport_Multiple"

DoCmd.OpenReport stDocName, acPreview 'Normal

If intCopies = 0 Then

intCheck = 0
Exit Do
GoTo PrintGroupNo

End If

Loop

Loop Until intCheck = 0

End If

Where I am having problems is that whilst the code goes through the 1st loop
it just continually loops in the 2nd instead of going back to the 1st loop,
getting the groupno, then going to printreport to print the required number
and then back to groupno again and repeating until for each group number the
required number of reports are printed.

Or is there a better way to achieve this?

TIA

Tom
 
T

Tom

Thanks for that Dorian - a case of not seeing the wood for the trees - will
try that out.

Many thanks

Tom


Dorian said:
'GoTo PrintReport'
will never return to the place where it was run.
You need to replace this with a 'Call' to a procedure.
Make your PrintReport logic enclosed in a procedure that can be called
whenever needed.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and
they
eat for a lifetime".


Tom said:
Appreciate some assistance with some code please.

On form I have a combo box which will list a number of companies. The
data
source for that combo box is a table wherein 1 field is called Company
and a
2nd field called GroupNo. The afterupdate code for the combo box will
lookup
will lookup the GroupNo [the number of departments for that company e.g.
5].

What I want to achieve is that after selecting the company, the client
will
click on a print button, a input box will pop up and ask the number of
copies required. The user will input a number e.g. 3 and what would
happen
then is that for each of the 5 departments 3 copies of a report would be
printed.

The code I have been working on is:


Dim strMsg As String, intStyle As String, strTitle As String, DgDef
As
String, Response As String
Dim stDocName As String, strRecRef As String
Dim Default As Integer, intCheck As Integer, intCounter As Integer,
intCopies As Integer, intValue As Integer
Dim intGroupNo As Integer, intCheckGroupNo As Integer, intGroupCounter
As
Integer, intCom As Double


strMsg = "Do you want to print a REMINDER to " &
Forms!frmConatctHistory![Client] & " for this months Management Fees?"
intStyle = vbYesNo + vbQuestion + vbDefaultButton2
strTitle = DBTitle

Response = MsgBox(strMsg, intStyle, strTitle)

If Response = vbYes Then

strMsg = "Enter copies required"
strTitle = "Print copies"

Default = "3"
intValue = InputBox(strMsg, strTitle, Default)
Forms!frmOwnerHistory![Copies] = intValue
intCopies = intValue

Me![GroupNo] = DCount("LanGroup", "qryChkLanGroup")

' MsgBox Me![GroupNo] & " A"

If Me![GroupNo] > "1" Then intGroupNo = Me![GroupNo]


GoTo PrintGroupNo

End If


Me![Park].SetFocus

PrintGroupNo:

Do
Do While intGroupNo > 0
intGroupNo = intGroupNo - 1

GoTo PrintReport

Loop
Loop Until intGroupNo = 0

If intGroupNo = 0 Then

Exit Sub

End If

PrintReport:

If intCopies >= "0" Then
intCheck = -1: intCounter = intCopies

Do
Do While intCounter < 99
intCounter = intCounter - 1
Me![Copies] = intCounter

stDocName = "rptManagementReport_Multiple"

DoCmd.OpenReport stDocName, acPreview 'Normal

If intCopies = 0 Then

intCheck = 0
Exit Do
GoTo PrintGroupNo

End If

Loop

Loop Until intCheck = 0

End If

Where I am having problems is that whilst the code goes through the 1st
loop
it just continually loops in the 2nd instead of going back to the 1st
loop,
getting the groupno, then going to printreport to print the required
number
and then back to groupno again and repeating until for each group number
the
required number of reports are printed.

Or is there a better way to achieve this?

TIA

Tom
 
T

Tom

Tried that and get the same results as in my original code - it does not
exit the PrintReport procedure and go back to the 1st loop.

Tom

Tom
Tom said:
Thanks for that Dorian - a case of not seeing the wood for the trees -
will try that out.

Many thanks

Tom


Dorian said:
'GoTo PrintReport'
will never return to the place where it was run.
You need to replace this with a 'Call' to a procedure.
Make your PrintReport logic enclosed in a procedure that can be called
whenever needed.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and
they
eat for a lifetime".


Tom said:
Appreciate some assistance with some code please.

On form I have a combo box which will list a number of companies. The
data
source for that combo box is a table wherein 1 field is called Company
and a
2nd field called GroupNo. The afterupdate code for the combo box will
lookup
will lookup the GroupNo [the number of departments for that company
e.g.
5].

What I want to achieve is that after selecting the company, the client
will
click on a print button, a input box will pop up and ask the number of
copies required. The user will input a number e.g. 3 and what would
happen
then is that for each of the 5 departments 3 copies of a report would be
printed.

The code I have been working on is:


Dim strMsg As String, intStyle As String, strTitle As String, DgDef
As
String, Response As String
Dim stDocName As String, strRecRef As String
Dim Default As Integer, intCheck As Integer, intCounter As Integer,
intCopies As Integer, intValue As Integer
Dim intGroupNo As Integer, intCheckGroupNo As Integer,
intGroupCounter As
Integer, intCom As Double


strMsg = "Do you want to print a REMINDER to " &
Forms!frmConatctHistory![Client] & " for this months Management Fees?"
intStyle = vbYesNo + vbQuestion + vbDefaultButton2
strTitle = DBTitle

Response = MsgBox(strMsg, intStyle, strTitle)

If Response = vbYes Then

strMsg = "Enter copies required"
strTitle = "Print copies"

Default = "3"
intValue = InputBox(strMsg, strTitle, Default)
Forms!frmOwnerHistory![Copies] = intValue
intCopies = intValue

Me![GroupNo] = DCount("LanGroup", "qryChkLanGroup")

' MsgBox Me![GroupNo] & " A"

If Me![GroupNo] > "1" Then intGroupNo = Me![GroupNo]


GoTo PrintGroupNo

End If


Me![Park].SetFocus

PrintGroupNo:

Do
Do While intGroupNo > 0
intGroupNo = intGroupNo - 1

GoTo PrintReport

Loop
Loop Until intGroupNo = 0

If intGroupNo = 0 Then

Exit Sub

End If

PrintReport:

If intCopies >= "0" Then
intCheck = -1: intCounter = intCopies

Do
Do While intCounter < 99
intCounter = intCounter - 1
Me![Copies] = intCounter

stDocName = "rptManagementReport_Multiple"

DoCmd.OpenReport stDocName, acPreview 'Normal

If intCopies = 0 Then

intCheck = 0
Exit Do
GoTo PrintGroupNo

End If

Loop

Loop Until intCheck = 0

End If

Where I am having problems is that whilst the code goes through the 1st
loop
it just continually loops in the 2nd instead of going back to the 1st
loop,
getting the groupno, then going to printreport to print the required
number
and then back to groupno again and repeating until for each group number
the
required number of reports are printed.

Or is there a better way to achieve this?

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

Top