Solved

gosub no sub on return

Posted on 2014-02-25
6
273 Views
Last Modified: 2014-02-25
I have code that needs to repeat the same task, so I thought I would try a GoSub/Return code, however when I am looping thru a recordset that calls the gosub it seems to get no Return found when it gets to the last record in the recordset.

what am I missing?

            Select Case MsgBox("Sorry unable to process request until Unpublished Invoice(s) have been processed/printed." _
                & vbCrLf & "" _
                & vbCrLf & "Do you wish to print UnPublished Invoices?" _
                , vbOK Or vbCritical Or vbDefaultButton1, "Invoice Needs to be Printed")
            Case vbOK
                rs.MoveFirst
                gInvID = rs.Fields("IDinvoice")
                    Do Until rs.EOF
                        GoSub MyPrint
                        rs.MoveNext
                    Loop
            Case vbCancel
                DoCmd.RunCommand acCmdUndo
                
            End Select
            
        End If
    End If
    
MyPrint:

        DoCmd.OpenReport "rptAPCodingSlip", acViewNormal, "IDInvoice = " & Chr(39) & gInvID & Chr(39) & ""
        DoCmd.OpenReport "rptInvoiceActivity_Slip", acViewNormal, "ContractNo = " & Chr(34) & gContractID & Chr(34) & ""
        DoCmd.RunSQL "Update tblinvoice Set RecordLock = -1 where IDInvoice = " & gInvID & ""

Return

End Sub

Open in new window

0
Comment
Question by:Karen Schaefer
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
   End If
 Exit Sub   
MyPrint:

Open in new window


You need an Exit Sub in front of MyPrint otherwise you just fall into that code when the loop ends.
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
Why a GoSub?

Couldn't you just put the 3 lines of code where the GoSub is?
  Case vbOK
                rs.MoveFirst
                gInvID = rs.Fields("IDinvoice")
                    Do Until rs.EOF
                         DoCmd.OpenReport "rptAPCodingSlip", acViewNormal, "IDInvoice = " & Chr(39) & gInvID & Chr(39) & ""
                        DoCmd.OpenReport "rptInvoiceActivity_Slip", acViewNormal, "ContractNo = " & Chr(34) & gContractID & Chr(34) & ""
                        DoCmd.RunSQL "Update tblinvoice Set RecordLock = -1 where IDInvoice = " & gInvID & ""
                        rs.MoveNext
                    Loop

Open in new window

0
 

Author Comment

by:Karen Schaefer
Comment Utility
thats what I ended up doing I was trying to be more streamlined since I use those 3 lines of code multiple times within the entire code.

Thanks for the input.

K
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Closing Comment

by:Karen Schaefer
Comment Utility
tkx
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
So, you really didn't want to know what was wrong with the original code?

The use of GoSubs lets you streamline the logic flow so it isn't bogged down in details.  It is also useful if you need to use the code in multiple situations.  I would never embed the code if I had to use it multiple times as you said you did so I completely disagree with your decision to accept imnorie's work around.
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
If you are going to repeat those lines of code and you don't want to embed them you could put them in a separate sub/function and call that, passing any required parameters/arguments.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now