Solved

gosub no sub on return

Posted on 2014-02-25
6
278 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 35

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 33

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Closing Comment

by:Karen Schaefer
ID: 39887227
tkx
0
 
LVL 35

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 33

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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 the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

831 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