Solved

gosub no sub on return

Posted on 2014-02-25
6
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:PatHartman
ID: 39887174
   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 34

Accepted Solution

by:
Norie earned 500 total points
ID: 39887184
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
ID: 39887224
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Closing Comment

by:Karen Schaefer
ID: 39887227
tkx
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 39887362
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 34

Expert Comment

by:Norie
ID: 39887514
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

688 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