Link to home
Start Free TrialLog in
Avatar of jbakestull
jbakestull

asked on

Access 2007 VBA Mod Function with two Variables

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
Avatar of mbizup
mbizup
Flag of Kazakhstan image

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

How does value2  fit into the logic that defines the single value returned from the function?
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.
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)
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,"")
Avatar of jbakestull
jbakestull

ASKER

Basically, I need  if value1 = "Test" and value2 ="Male" then result is "Male"

basically an nested if statement.
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.