Question : Stop button code if condition is met in Module.

Sorry about the title, wasn't sure how to say it. I have a module that verifies a table has data:
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
Public Sub CheckTable()
    Dim fIsTableEmpty As Boolean
    Dim db As Database
    Dim rs As Recordset
    Set db = DBEngine(0)(0)
    Set rs = db.OpenRecordset("Scrap", dbOpenTable)
    If rs.RecordCount = 0 Then
        fIsTableEmpty = True
        MsgBox "Nothing in the table, Please import Cognos report"
        On Error GoTo Error_Handler
    Dim fdg As FileDialog, vrtSelectedItem As Variant
    Dim strSelectedFile As String
    
    Set fdg = Application.FileDialog(msoFileDialogFilePicker)
    
    With fdg
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls"
        .Filters.Add "Excel 2007", "*.xlsx"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
            strSelectedFile = vrtSelectedItem
        Next vrtSelectedItem
        Else
        End If
'        If Not IsEmpty(strSelectedFile) Then
        If Len(Trim(strSelectedFile) & "") > 0 Then
        Dim StrSQL As String
        StrSQL = "Delete * from Scrap;"
        DoCmd.SetWarnings False
        DoCmd.RunSQL StrSQL
        DoCmd.SetWarnings True
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Scrap", strSelectedFile, True, , False
        Else
        DoCmd.OpenForm "FileNotSelected", acNormal, , , , , False
        End If
        
        

        
    End With
        Set fd = Nothing
Exit_Procedure:
     Exit Sub


I call it from a form button:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Private Sub btnTotal_Click()
        CheckTable
        DoCmd.Hourglass True
        'Turns off the Access warning messages
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "TotalScrapQry"
        DoCmd.Hourglass False
        'Turns the Access warning messages back on
        DoCmd.SetWarnings True
        
        
        DoCmd.OpenForm "ByTotalScrap", acNormal, , , , , False
End Sub


What I want to happen is if the module finds that there is no data to stop running the button code on the form. Instead, it resumes back and continues to finish the button code and open the 'ByTotalScrap' form. That is fine if the user selects an Excel file from within the Module code, but if the users clicks the Cancel button when selecting an Excel file the form is still opened and there is no data. Hope that makes sense. Thanks for any help on this.

Answer : Stop button code if condition is met in Module.

You could change CheckTable() to a function and return True if table has data.  Then your button click code would be something like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
Private Sub btnTotal_Click()
      IF  CheckTable Then
        DoCmd.Hourglass True
        'Turns off the Access warning messages
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "TotalScrapQry"
        DoCmd.Hourglass False
        'Turns the Access warning messages back on
        DoCmd.SetWarnings True
        
        
        DoCmd.OpenForm "ByTotalScrap", acNormal, , , , , False
     End If
End Sub
Random Solutions  
 
programming4us programming4us