Solved

Access 2007 VBA Mod Function with two Variables

Posted on 2013-01-14
8
732 Views
Last Modified: 2013-01-28
Code below is setup to use one variable. Is there a way to modify the function to use two variables to get a result?

For example, I would like to use value1 ="Value" and value2 = "Another Value" to return one value..
 
Sorry for the confusion, best way I can describe it.

Public Function ProgramType(value1)
If value1 & "" = "" Then ProgramType = "Missing": Exit Function
Select Case value1
    Case Is = "VOAGO Men's Shelter(64)"
     ProgramType = "Male"
    Case Is = "YMCA Overflow(227)"
     ProgramType = "Male"
    Case Is = "LSS - FM Faith on 8th(52)"
    ProgramType = "Male"
    Case Is = "LSS - FM Faith on 6th(43)"
     ProgramType = "Male"
     Case Is = "YWCA Family Center(69)"
    ProgramType = "Family"
        Case Is = "HFF - Family Shelter(51)"
    ProgramType = "Family"
        Case Is = "VOAGO Family Services(67)"
    ProgramType = "Family"
       Case Is = "LSS - FM Nancy's Place(45)"
    ProgramType = "Female"
        Case Is = "SE - FOH Rebecca's Place(48)"
    ProgramType = "Female"
     Case Is = "SE - FOH Men's Shelter(47)"
    ProgramType = "Male"
    Case Is = value1
    ProgramType = "Female"
End Select
   
End Function
0
Comment
Question by:jbakestull
8 Comments
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
To add another variable to the function, you would add it to the list of parameters in your declaration... but it is not clear at all what you want to do with that second variable.

This is just an example:

Public Function ProgramType(value1, Value2)
If value1 & "" = "" Then ProgramType = "Missing": Exit Function
Select Case value1
    Case Is = "VOAGO Men's Shelter(64)"
     ProgramType = "Male"
    Case Is = "YMCA Overflow(227)"
     ProgramType = "Male"
    Case Is = "LSS - FM Faith on 8th(52)"
    ProgramType = "Male"
    Case Is = "LSS - FM Faith on 6th(43)"
     ProgramType = "Male"
     Case Is = "YWCA Family Center(69)"
    ProgramType = "Family"
        Case Is = "HFF - Family Shelter(51)"
    ProgramType = "Family"
        Case Is = "VOAGO Family Services(67)"
    ProgramType = "Family"
       Case Is = "LSS - FM Nancy's Place(45)"
    ProgramType = "Female"
        Case Is = "SE - FOH Rebecca's Place(48)"
    ProgramType = "Female"
     Case Is = "SE - FOH Men's Shelter(47)"
    ProgramType = "Male"
    Case Is = value1
    ProgramType = "Female"
End Select

' Do something with Value2 here

   
End Function 

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
How does value2  fit into the logic that defines the single value returned from the function?
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Just to add a bit: when you setup arguments, you also need to indicate a type.  If you don't, it's automaticaly a type of variant, which can hold a NULL.  So this:

Public Function ProgramType(value1, Value2)

  accepts two variants, and returns a variant.  In contrast, you might do it like this:

Public Function ProgramType(value1 as string, Value2 as string) as boolean

  Now meaning you need to pass in two strings and will get back a boolean value.  You should as a good habit always specify the data type you want (even with the variant) and be as specific as possible as to the type (a variant can accept anything).

 So your definition would be this:

Public Function ProgramType(value1 as string, Value2 as string) as string

  Assuming value2 is going to be a string as well.

HTH,
Jim.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Yes, you could do that but your number of tests will NxM (where N is the number of values for Value1 and M is the number of values for Value2).  Andif you change the values you have to rewrite the code.

Instead, I would create a table with 3 columns (Value1, Value2, Result)

Then you could use a DLOOKUP() function, something like:

strCriteria = "[Field1] = " & chr$(34) & Value1 & chr$(34) & " AND " _
                  & "[Field2] = " & chr$(34) & Value2 & chr$(34)
fnProgramType = DLOOKUP("Result", "LookupTable", strCriteria)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 61

Expert Comment

by:mbizup
Comment Utility
In addition to Jim's comments on data typing... if you do define a parameter as a string or other datatype instead of the default variant, your function will not be able to accept nulls (such as blank textboxes), and will error if you attempt to use it like that.


So if you define your arguments as strings, you would probably also have to change the way you call the function to handle nulls.

as an example:

ProgramType NZ(Me.Textbox1,""), NZ(Me.Textbox2,"")
0
 

Author Comment

by:jbakestull
Comment Utility
Basically, I need  if value1 = "Test" and value2 ="Male" then result is "Male"

basically an nested if statement.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
Comment Utility
Maybe this  (I've added jim's suggested type declarations)?

Public Function ProgramType(value1 AS string, Value2 as string) as string
If value1 & "" = "" Then ProgramType = "Missing": Exit Function
Select Case value1
    Case Is = "VOAGO Men's Shelter(64)"
     ProgramType = "Male"
    Case Is = "YMCA Overflow(227)"
     ProgramType = "Male"
    Case Is = "LSS - FM Faith on 8th(52)"
    ProgramType = "Male"
    Case Is = "LSS - FM Faith on 6th(43)"
     ProgramType = "Male"
     Case Is = "YWCA Family Center(69)"
    ProgramType = "Family"
        Case Is = "HFF - Family Shelter(51)"
    ProgramType = "Family"
        Case Is = "VOAGO Family Services(67)"
    ProgramType = "Family"
       Case Is = "LSS - FM Nancy's Place(45)"
    ProgramType = "Female"
        Case Is = "SE - FOH Rebecca's Place(48)"
    ProgramType = "Female"
     Case Is = "SE - FOH Men's Shelter(47)"
    ProgramType = "Male"
    Case Is = value1
    ProgramType = "Female"
End Select

if value1 = "Test" and value2 ="Male" then ProgramType = "Male"
   
End Function 

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
I strongly recommend you consider the option recommended here.

This option is extensible (you can change the range of acceptable values for each field, and the results of the combinations of both at will) and will involve no code changes if the acceptable values of the two options ever change.

Additionally, you could simply use SELECT statements against the two fields of this table to populate the combo boxes which you could use as the source of Value1 and Value2.
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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

14 Experts available now in Live!

Get 1:1 Help Now