Converting to a Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gchichester
    New Member
    • Aug 2010
    • 7

    Converting to a Function

    I'm using this Select Case code in conjunction with a Option Group on a multi-tab form with about 40 run report buttons. The code works fine when included in the Click Event code of a button. I would like to convert it to a function for obvious reasons. I have tried a few variation of a function with no success. What is the best approach for converting a procedure to a function.


    Code:
    Private Sub WhatYear()
    Dim strCriteria As String
    Select Case Me.Seasons
    Case 2009
        strCriteria = "[ExpDate] Between #09/01/2009# and #09/01/2010#"
    Case 2010
        strCriteria = "[ExpDate] Between #09/01/2010# and #09/01/2011#"
    Case 2011
        strCriteria = "[ExpDate] Between #09/01/2011# and #09/01/2012#"
    End Select
          
       DoCmd.OpenReport "rptWeeklyStatus_Asia_Qry", acViewPreview, , strCriteria
    Thanks for any and all suggestions
    Gil
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. Copy-N-Paste the following Code into the Declarations Section of the Form's Code Module. This must be the Form that contains the [Seasons] and [Expdate] Fields, and the Function should be declared Privately. Notice a couple of minor additions to the Code also.
      Code:
      Private Function fWhatYear()
      Dim strCriteria As String
      
      If IsNull(Me.Seasons) Or Not IsNumeric(Me.Seasons) Then Exit Function
      
      Select Case Me.Seasons
        Case 2009
          strCriteria = "[ExpDate] Between #09/01/2009# and #09/01/2010#"
        Case 2010
          strCriteria = "[ExpDate] Between #09/01/2010# and #09/01/2011#"
        Case 2011
          strCriteria = "[ExpDate] Between #09/01/2011# and #09/01/2012#"
        Case Else
          MsgBox "No Report exists for the specified Year", vbExclamation, "Invalid Year"
            Exit Function
      End Select
      
      DoCmd.OpenReport "rptWeeklyStatus_Asia_Qry", acViewPreview, , strCriteria
      End Function
    2. To Execute the Function:
      Code:
      Call fWhatYear

    Comment

    • gchichester
      New Member
      • Aug 2010
      • 7

      #3
      ADezii,
      Thanks for the prompt response.
      I added your changed code as a private function of the form code module. Selecting a different year still has no effect on the date range of the report.


      [Code]
      Private Function fWhatYear()
      Dim strCriteria As String

      If IsNull(Me.Seaso ns) Then Exit Function

      Select Case Me.Seasons
      Case 2009
      strCriteria = "[ExpDate] Between #09/01/2009# and #09/01/2010#"
      Case 2010
      strCriteria = "[ExpDate] Between #09/01/2010# and #09/01/2011#"
      Case 2011
      strCriteria = "[ExpDate] Between #09/01/2011# and #09/01/2012#"
      Case Else
      MsgBox "No Report exists for the specified Year", vbExclamation, "Invalid Date Range"
      Exit Function
      End Select
      End Function
      --------------------------------------------------------------------------------
      Private Sub cmdAsiaBillOfLa dingCount_Click ()
      On Error GoTo cmdAsiaBillOfLa dingCount_Click _Err

      fWhatYear

      DoCmd.OpenRepor t "rptBillOfLadin gCount_Asia_Tbl ", acViewPreview, , strCriteria

      PrintQuestion

      cmdAsiaBillOfLa dingCount_Click _Exit:
      Exit Sub

      cmdAsiaBillOfLa dingCount_Click _Err:
      MsgBox Error$
      Resume cmdAsiaBillOfLa dingCount_Click _Exit

      End Sub

      I saw where included the DoCmd.OpenRepor t "rptWeeklyStatu s_Asia_Qry", acViewPreview, , strCriteria in the function, Was that your intension?
      It's part of Click Event code, I included it as a reference to how srtCriteria is being used. Sorry for a confusion.
      Gil

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I saw where included the DoCmd.OpenRepor t "rptWeeklyStatu s_Asia_Qry", acViewPreview, , strCriteria in the function, Was that your intension?
        My fault, thought that it may be part of the overall Code, thus the inclusion. Also forgot about the Option Group, that's what happens as you get older ! (LOL)

        Comment

        • gchichester
          New Member
          • Aug 2010
          • 7

          #5
          No problem, I deal with the same problem.
          So, Do you have any other suggestions on how to convert
          this code to a function? Or should I just add it to each button?

          Gil

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            What code are you referring to, Gil?

            Comment

            • gchichester
              New Member
              • Aug 2010
              • 7

              #7
              The "WhatYear" function code.
              The changes you suggested did not work.

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Code has been tested prior to Posting and does work.

                Comment

                • gchichester
                  New Member
                  • Aug 2010
                  • 7

                  #9
                  Sorry but I'm confused again!
                  Did you test the code with or without the DoCmd.OpenRepor t & the Option Group?

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Move the Declaration for strCriteria from the Function to the Form's Code Module, General Declarations Section.
                    Code:
                    'Should be in the General Declarations Section of the involved Form
                    Dim strCriteria As String

                    Comment

                    Working...