Solved

Stop button code if condition is met in Module.

Posted on 2010-08-25
4
416 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now