?
Solved

Access 2007 VBA Mod Function with two Variables

Posted on 2013-01-14
8
Medium Priority
?
748 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 58
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 48

Expert Comment

by:Dale Fye
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 2000 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 48

Expert Comment

by:Dale Fye
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…
Suggested Courses

764 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