473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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.


Expand|Select|Wrap|Line Numbers
  1. Private Sub WhatYear()
  2. Dim strCriteria As String
  3. Select Case Me.Seasons
  4. Case 2009
  5.     strCriteria = "[ExpDate] Between #09/01/2009# and #09/01/2010#"
  6. Case 2010
  7.     strCriteria = "[ExpDate] Between #09/01/2010# and #09/01/2011#"
  8. Case 2011
  9.     strCriteria = "[ExpDate] Between #09/01/2011# and #09/01/2012#"
  10. End Select
  11.  
  12.    DoCmd.OpenReport "rptWeeklyStatus_Asia_Qry", acViewPreview, , strCriteria
  13.  
Thanks for any and all suggestions
Gil
Aug 11 '10 #1
9 1359
ADezii
8,834 Expert 8TB
  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.
    Expand|Select|Wrap|Line Numbers
    1. Private Function fWhatYear()
    2. Dim strCriteria As String
    3.  
    4. If IsNull(Me.Seasons) Or Not IsNumeric(Me.Seasons) Then Exit Function
    5.  
    6. Select Case Me.Seasons
    7.   Case 2009
    8.     strCriteria = "[ExpDate] Between #09/01/2009# and #09/01/2010#"
    9.   Case 2010
    10.     strCriteria = "[ExpDate] Between #09/01/2010# and #09/01/2011#"
    11.   Case 2011
    12.     strCriteria = "[ExpDate] Between #09/01/2011# and #09/01/2012#"
    13.   Case Else
    14.     MsgBox "No Report exists for the specified Year", vbExclamation, "Invalid Year"
    15.       Exit Function
    16. End Select
    17.  
    18. DoCmd.OpenReport "rptWeeklyStatus_Asia_Qry", acViewPreview, , strCriteria
    19. End Function
  2. To Execute the Function:
    Expand|Select|Wrap|Line Numbers
    1. Call fWhatYear
Aug 11 '10 #2
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.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 Date Range"
Exit Function
End Select
End Function
--------------------------------------------------------------------------------
Private Sub cmdAsiaBillOfLadingCount_Click()
On Error GoTo cmdAsiaBillOfLadingCount_Click_Err

fWhatYear

DoCmd.OpenReport "rptBillOfLadingCount_Asia_Tbl", acViewPreview, , strCriteria

PrintQuestion

cmdAsiaBillOfLadingCount_Click_Exit:
Exit Sub

cmdAsiaBillOfLadingCount_Click_Err:
MsgBox Error$
Resume cmdAsiaBillOfLadingCount_Click_Exit

End Sub

I saw where included the DoCmd.OpenReport "rptWeeklyStatus_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
Aug 11 '10 #3
ADezii
8,834 Expert 8TB
I saw where included the DoCmd.OpenReport "rptWeeklyStatus_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)
Aug 11 '10 #4
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
Aug 11 '10 #5
ADezii
8,834 Expert 8TB
What code are you referring to, Gil?
Aug 11 '10 #6
The "WhatYear" function code.
The changes you suggested did not work.
Aug 11 '10 #7
ADezii
8,834 Expert 8TB
Code has been tested prior to Posting and does work.
Aug 11 '10 #8
Sorry but I'm confused again!
Did you test the code with or without the DoCmd.OpenReport & the Option Group?
Aug 11 '10 #9
ADezii
8,834 Expert 8TB
Move the Declaration for strCriteria from the Function to the Form's Code Module, General Declarations Section.
Expand|Select|Wrap|Line Numbers
  1. 'Should be in the General Declarations Section of the involved Form
  2. Dim strCriteria As String 
Aug 11 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
3
by: linarin | last post by:
#include <iostream> using namespace std; typedef bool (*CallableFunction)(int argc,char* argv); void DefineMyFunction(const char* name,CallableFunction func){ //here do the define action. }...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
4
by: Ali | last post by:
This code below works with M$ VS2005 but fails with g++ 4.1.3. However, the unary operation works with g++ as well. The interval class is at http://xsc.de but everything is declared as it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.