Solved

Access 2007 VBA Mod Function with two Variables

Posted on 2013-01-14
8
734 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

770 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