Solved

Stop button code if condition is met in Module.

Posted on 2010-08-25
4
420 Views
Last Modified: 2013-11-27
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
Comment
Question by:G Scott
4 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33521434
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
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 100 total points
ID: 33521486
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
 
LVL 10

Accepted Solution

by:
dwe761 earned 400 total points
ID: 33521492
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
 
LVL 1

Author Closing Comment

by:G Scott
ID: 33522362
Thanks dwe761. That worked!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

821 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