Skipping parts of the code?

J.Windebank

Registered User.
Local time
Today, 09:36
Joined
Apr 2, 2003
Messages
37
In one of my functions I want to check if a query returns any rows, if so, keep going, but if not, skip a certain piece of code and then continue on.

At the moment, I can get it to keep going if the row exists with:

Code:
ub = rst.RecordCount
If ub <> 0 Then

~ Code to be executed ~

End If

~ More Code Here ~

Now the problem is that if no records are found for my query, the code stops, I am after a way to get it to keep going past the irrelevant code.

Thanks in advance,

Jordan
 
Why not use labels within your routine to move the code to when you want to run a process?

Although what you have posted as an example would appear to do exactly what you described as wanting.
 
OK, iof this "should" work then I am stumped. I do not get any errors, the code simply stops after not finding any valid rows.

Here is the code in entirety for this section.

Code:
   ' Check if Movies required.
     strsql = "SELECT tblMovies.COTRound, tblMovies.DayID, tblMovies.StateID, tblMovies.Movie7, tblMovies.Movie7Name, tblMovies.Movie9, tblMovies.Movie9Name, tblMovies.Movie10, tblMovies.Movie10Name, tblMovies.MovieSBS, tblMovies.MovieSBSName FROM tblMovies WHERE (((tblMovies.COTRound)=COTRound()) AND ((tblMovies.DayID)=DayID()) AND ((tblMovies.StateID)=StateID())) ORDER BY tblMovies.ID DESC;"
     Set rst = dbs.OpenRecordset(strsql)
     rst.MoveLast
     ub = rst.RecordCount
     
     If ub <> 0 Then
   
   ' Prepare for Movies.
     With WordApp.Selection
     
       .Font.Bold = True
       .Font.Underline = wdUnderlineSingle
       .InsertBreak Type:=wdLineBreak
       .TypeText "Sunday Night Movies"
       .InsertBreak Type:=wdLineBreak
       .Font.Bold = False
       .Font.Underline = wdUnderlineNone
          
     End With
   
   ' Query for Movies.
     strsql = "SELECT tblMovies.Movie7, tblMovies.Movie7Name, tblMovies.Movie9, tblMovies.Movie9Name, tblMovies.Movie10, tblMovies.Movie10Name, tblMovies.MovieSBS, tblMovies.MovieSBSName FROM tblMovies WHERE (((tblMovies.COTRound)=COTRound()) AND ((tblMovies.DayID)=DayID()) AND ((tblMovies.StateID)=StateID())) ORDER BY tblMovies.ID DESC;"
     Set rst = dbs.OpenRecordset(strsql)
     rst.MoveLast
     ub = rst.RecordCount

     ReDim arrayPrograms(ub, 8)
     rst.MoveFirst

     For i = 1 To ub
     arrayPrograms(i, 1) = rst(0)
     arrayPrograms(i, 2) = rst(1)
     arrayPrograms(i, 3) = rst(2)
     arrayPrograms(i, 4) = rst(3)
     arrayPrograms(i, 5) = rst(4)
     arrayPrograms(i, 6) = rst(5)
     arrayPrograms(i, 7) = rst(6)
     arrayPrograms(i, 8) = rst(7)
     boo7Movie = rst(0)
     boo9Movie = rst(2)
     boo10Movie = rst(4)
     booSBSMovie = rst(6)

   ' Insert Movies.
     If boo7Movie = True Then
     str7Movie = rst(1)
       With WordApp.Selection
         .TypeText "Watched " & [str7Movie] & " or some other movie..777"
         .InsertBreak Type:=wdLineBreak
       End With
     End If
     
     If boo9Movie = True Then
     str9Movie = rst(3)
       With WordApp.Selection
         .TypeText "Watched " & [str9Movie] & " or some other movie..999"
         .InsertBreak Type:=wdLineBreak
       End With
     End If
     
     If boo10Movie = True Then
     str10Movie = rst(5)
       With WordApp.Selection
         .TypeText "Watched " & [str10Movie] & " or some other movie..000"
         .InsertBreak Type:=wdLineBreak
       End With
     End If
     
     If booSBSMovie = True Then
     strSBSMovie = rst(7)
       With WordApp.Selection
         .TypeText "Watched " & [strSBSMovie] & " or some other movie..888"
         .InsertBreak Type:=wdLineBreak
       End With
     End If
     
     rst.MoveNext
     Next i

     rst.Close
     
     End If

~ More code here that is being missed ~

Is anything standing out as being horribly wrong here?

Thanks for the response,

Jordan
 
Last edited:
J.

Could you post a sample db?

Sometimes it is way easier to single-step through the code
with the debugger.

Wayne
 
OK, did not know about debugging with single stepping, tried it but is only confirming what I already know, when the recordset = 0 the code is jumping to a label named ErrHandler for some reason. The ErrHandler code is for a completely different function.

Anyway.

Attached is the database.

The use of the forms is:

start with frmAddCard, add a four digit code for the round number, for an example use 1690. Set the day to Monday, and leave the state on Vic. This is only because I have that day already partially setup.

Then click Create, and it will open a form called "frmTVCards".

The code in question is the OnClick event for btnCreate.

Thanks for taking the time to look at this.

Regards,

Jordan

TVCards.zip (125kb)
 
Jordan,

Your code is dying when you do the rst.MoveLast with an
empty recordset.

Code:
Set rst = dbs.OpenRecordset(strsql)
If rst.EOF And rst.BOF Then
   '
   ' There are no records
   '
   ' code for no records
Else
   '
   ' There are records
   '
   ' code for some records
   rst.MoveLast
   ub = rst.RecordCount
End If

Wayne
 
Wayne thank you, this works perfectly. I really do appreciate the help you have given me, this was my one major road block still standing in my database application.

Regards,

Jordan
 

Users who are viewing this thread

Back
Top Bottom