Results 1 to 3 of 3
  1. #1
    lookingforK is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    31

    Question How to end/exit a sub from its nested sub?

    Hi,

    I am writing/running VBA code through MS Access 2007.

    I have a loop in the master sub. When a condition is met in its nested sub, I want the loop to stop and exit the procedure.

    Just wondering: what's kind of statement or command I can use for this purpose?

    The structure of the code is like:

    Private Sub exp2exall_Click()
    ......
    Do Until rstStores.EOF
    .....
    GenerateReport strReportPath, Trim(rstStores![Nam])
    rstStores.MoveNext
    Loop
    End If
    .....
    End Sub

    Private Sub GenerateReport(ReportPath As String, Nam As String)
    ...


    ' Deal with the issue that cannot find the template file
    If Dir(ReportPath & "\Template File.xlsx ") = "" Then
    MsgBox "[Template File.xlsx] cannot be found under this folder. Please try a different path."
    Kill ReportPath & "\Temp.xls"
    {End/exit sub exp2exall_Click()}
    Exit Sub
    Else
    ' Open a template report to hold the exported info from the temporary Excel sheet
    Set wkbDest = xl.Workbooks.Open(ReportPath & "\Template File.xlsx ")
    End If
    ...
    End Sub

    How to write the code for {End/exit sub exp2exall_Click()}?
    So, if the template file can't be found, the sub exp2exall_Click() and GenerateReport(ReportPath As String, Nam As String) will be ended.

    Thank you in advance.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Are you saying there is more code after the recordset loop that should not execute if template not found?

    Could make the Sub a function and return a value to the Click event.

    So lines in the function would be:
    GenerateReport="Found"
    or
    GenerateReport="NotFound"

    Call the function in Click event:
    strStatus = GenerateReport(strReportPath, Trim(rstStores![Nam]))

    Then code following the loop:
    If strStatus = "Found" Then
    'continue procedure'
    End If
    End Sub

    Is this Sub called by any event other than the Click? If not, could just embed it within the Click event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    lookingforK is offline Advanced Beginner
    Windows 7 32bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    31
    Thank you June7

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exit forms without saving
    By sk88 in forum Access
    Replies: 19
    Last Post: 12-19-2013, 06:04 PM
  2. form exit problem
    By lavi111 in forum Forms
    Replies: 5
    Last Post: 06-23-2012, 07:23 PM
  3. Exit without saving Subform
    By sk88 in forum Access
    Replies: 5
    Last Post: 05-03-2012, 11:55 AM
  4. exit
    By slimjen in forum Forms
    Replies: 4
    Last Post: 10-09-2011, 09:30 AM
  5. Replies: 0
    Last Post: 05-17-2008, 01:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums