Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Without Do Error ??? 1

Status
Not open for further replies.

grobermatic

Technical User
Dec 21, 2002
153
GB
Hi all,

Can you Loop back to a DO from within an IF statement?

I'm getting the "Loop without Do" compile error at in the following code at the red highlighted point...

Code Follows :

================= CODE START ===========================

Option Compare Database
Option Explicit

Sub RunApp()

Dim strRunResponse As String
Dim strResponse As String
Dim strSQL As String

strRunResponse = MsgBox("Do you want to run Suspense Reader?", vbQuestion + vbYesNo, "Run Application?")

If strRunResponse = 6 Then 'YES to RUN

strResponse = MsgBox("Do you want to save a copy of the last suspense reading?" & vbCrLf & vbCrLf & _
"THIS WILL OVERWRITE ALL PREVIOUSLY SAVED READINGS", vbQuestion + vbYesNo, "Save?")

If strResponse = 6 Then 'YES to SAVE

DoCmd.SetWarnings False 'Run queries to save last reading
DoCmd.OpenQuery "qrySaveLastReading"
DoCmd.OpenQuery "qrySuspenseData_DELETE"
DoCmd.SetWarnings True

Else
DoCmd.SetWarnings False 'Run queries to delete data currently in table
DoCmd.OpenQuery "qrySuspenseData_DELETE"
DoCmd.SetWarnings True
End If

MsgBox "The system will now try to connect to the Commercial Sysytem" & vbCrLf & vbCrLf & _
"Please ensure that EXTRA! Mainframe is open and that you are logged in to the Commercial System ", _
vbOKOnly, "Connecting to EXTRA!..."

'========== CONNECT LOOP ===============

Do 'Connect Loop

Extra_Connect 'Run Extra_Connect procedure

If System = "EXTRA! Personal Client" And Sess0 = "Ventura" Then 'Connected

MsgBox "Connected to :" & vbCrLf & vbCrLf & _
"System: " & System & vbCrLf & _
"Session: " & Sess0 & vbCrLf & vbCrLf & _
"Press Ok to continue...", vbOKOnly, "Connected"

Exit Do 'Exit loop if connected

Else 'Not Connected

strResponse = MsgBox("The database was unable to connect to EXTRA! Mainframe" & vbCrLf & vbCrLf & _
"Please check that EXTRA! is running and that you are logged on to the Commercial System " & _
"using your CTRF code." & vbCrLf & vbCrLf & _
"Press Ok to try again or Cancel to stop trying", vbCritical + vbOKCancel, "Unable to Connect!")

If strResponse = 1 Then

Loop 'Keep trying to connect until user clicks Cancel

Else

MsgBox "If you have been unable to connect to EXTRA!, " & _
"Please contact Craig Lomas (Xt: 99999 or email: me.name@mycompany.com) to get your system checked." & vbCrLf & vbCrLf & _
"The database will now close.", vbInformation, "Commercial Suspense Reader"

Application.Quit
Exit Do

End If

End If


'=========== END OF CONNECT LOOP ==================

MsgBox "The database will now try to connect to Commercial Suspense (SUS1)" & _
"Please ensure that the screen is clean (No Half Edited Accounts)", vbOKOnly, "Connecting to SUS1..."

Do 'Loop to connect to suspense

Goto_SUS1

If ExCap("SUSName") = "TRF Payment Suspense" Then

MsgBox "Connected to :" & vbCrLf & vbCrLf & _
"Account Name : Commercial Suspense (SUS1)" & _
"Account Number : " & ExCap("AccNumber"), vbInformation, "Connected"

Else

strResponse = MsgBox(" The database was unable to connect to Commercial Suspense (SUS1)" & vbCrLf & vbCrLf & _
"Please check mainframe is running and that you are logged into the Commercial System. The active screen " & _
"should be one where you can type in a screen name." & vbCrLf & vbCrLf & _
"Press Ok to try again or Cancel to close the application.", vbCritical, "Unable to Connect!")

If strResponse = 1 Then

Loop

Else

MsgBox "If you have been unable to connect to EXTRA! or Commercial Suspense, " & _
"Please contact Craig Lomas (Xt: 99999 or email: me.name@mycompany.com) to get your system checked." & vbCrLf & vbCrLf & _
"The database will now close.", vbInformation, "Commercial Suspense Reader"

Application.Quit
Exit Do

End If

End If


Else
Application.Quit
End If

End Sub

====================== CODE END ========================


Apologies for the long code...

Other posts I have read seem to attribute this error to a missing "END IF" but I think I have covered them all.

If you can't loop from within an if statement how would you get around this ?

Any help appreciated

Thanks


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Please, blue text on blue background is difficult to read.

Just doing the loop part (shortening it a bit). You have missing endifs before the loop statement. All If's "opened" within the loop, must also be "closed" within the loop.

[tt]Do 'Connect Loop
Extra_Connect 'Run Extra_Connect procedure
If System = "EXTRA! Personal Client" And Sess0 = "Ventura" Then 'Connected
MsgBox "Connected to :" & vbCrLf & vbCrLf & _ ...
Exit Do 'Exit loop if connected
Else 'Not Connected
strResponse = MsgBox("The database was unable to connect...
If strResponse = 1 Then
End If ' - missing
End If ' - missing
Loop 'Keep trying to connect until user clicks Cancel[/tt]

Roy-Vidar
 
Hi,

Thanks for your response ... sorry about the blue text thing.

So am I right in saying that you can't loop from within an IF statement?

If so how would you conditionally loop as I want to in my code?

Thanks again

Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Hi again!

<teacher mode>
Usually I use some time with paper and pencil, draw some diagrams to structure the loops and flow of the routine prior to programming. I've found that to decrease the developing time significantly
</teacher mode>;-)

Sometimes I'm not able to do completely structured programming, and end up doing exit <whatever> inbetween, which perhaps might be a workaround for the current challenge.

And as stated above, every Block you start (If, Select Case, With, For, Do...) within another Block, must be closed withing the same Block.

<embarrassed whispering>
VBA also has some support for Goto <Label>, but, most developers avoid that even more than visiting their mother in law
</embarrassed whispering>

[tt]Do 'Connect Loop
Extra_Connect 'Run Extra_Connect procedure
If System = "EXTRA! Personal Client" And Sess0 = "Ventura" Then 'Connected
MsgBox "Connected to :" & vbCrLf & vbCrLf & _ ...
Exit Do 'Exit loop if connected
Else 'Not Connected
strResponse = MsgBox("The database was unable to connect...
If strResponse = 1 Then
Exit Do
End If
End If
Loop 'Keep trying to connect until user clicks Cancel
Select Case strResponse
Case 1
' Didn't want to continue?
Case 7
' Didn't want to run Suspense Reader?
...[/tt]

Roy-Vidar
 
Hi again,

Against your better judgement I decided to have a go at the GoTo <Label> option. I'm please to say that this has worked well as I have been able to use it pretty much the same way as I wanted to use the Do...Loop.

Thanks once again for your guidance ... Heres a Star

I now have to go visit my mother in law... ;-)

Craig

--------------------------------------------------------------------------------------------------------
&quot;Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!&quot; .. Dr E. Brown (1985)
 
As RoyVidar alludes, is that you cannot overlap control structures. For example
[blue][tt]
Do While <Condition>

If <Other Condition> Then

Loop

End If
[/tt][/blue]
is illegal.

Just picking up part of your code, try something like
[tt]
'========== CONNECT LOOP ===============
Dim Connected As Boolean
Connected = False
strResponse = 1
Do Until Connected Or strResponse <> 1

Extra_Connect 'Run Extra_Connect procedure

If System = "EXTRA! Personal Client" And Sess0 = "Ventura" Then 'Connected

MsgBox "Connected to :" & vbCrLf & vbCrLf & _
"System: " & System & vbCrLf & _
"Session: " & Sess0 & vbCrLf & vbCrLf & _
"Press Ok to continue...", vbOKOnly, "Connected"

Connected = True ' Terminates the Loop

Else 'Not Connected

strResponse = MsgBox("The database was unable to connect to EXTRA! Mainframe" & vbCrLf & vbCrLf & _
"Please check that EXTRA! is running and that you are logged on to the Commercial System " & _
"using your CTRF code." & vbCrLf & vbCrLf & _
"Press Ok to try again or Cancel to stop trying", vbCritical + vbOKCancel, "Unable to Connect!")

If strResponse <> 1 Then

MsgBox "If you have been unable to connect to EXTRA!, " & _
"Please contact Craig Lomas (Xt: 99999 or email: me.name@mycompany.com) to get your system checked." & vbCrLf & vbCrLf & _
"The database will now close.", vbInformation, "Commercial Suspense Reader"

Application.Quit

End If

End If

Loop
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top