Solved

Access 2007 VBA Mod Function with two Variables

Posted on 2013-01-14
8
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38774284
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
ID: 38774309
How does value2  fit into the logic that defines the single value returned from the function?
0
 
LVL 57
ID: 38774323
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38774345
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
 
LVL 61

Expert Comment

by:mbizup
ID: 38774354
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
ID: 38774475
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
ID: 38774489
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)
ID: 38774687
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …

730 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