Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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:
 
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

Open in new window


I call it from a form button:
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

Open in new window


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.
0
G Scott
Asked:
G Scott
2 Solutions
 
Chris BottomleyCommented:
I think what you need is a global variable in a module which is set to a default value  during button processing pre form open.  If the user processes the form normally then it is set to a 'good' state as part of the processing and if the user cancels it stays in the default.

IN your button code you test teh state of this flag in order to take some action i.e. mode the normal/scrap function.

Make sense?

Chris
0
 
Helen FeddemaCommented:
You could make CheckTable a function instead of a Sub, with a Boolean return value.  Set it to False if the table is empty, and True otherwise.  Then check its value in the btnTotal_Click procedure, so you can exit if the table is empty.
0
 
dwe761Commented:
You could change CheckTable() to a function and return True if table has data.  Then your button click code would be something like this:
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

Open in new window

0
 
G ScottAuthor Commented:
Thanks dwe761. That worked!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now