?
Solved

gosub no sub on return

Posted on 2014-02-25
6
Medium Priority
?
288 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 38

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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:Karen Schaefer
ID: 39887227
tkx
0
 
LVL 38

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

762 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