Solved

Access 2010 VBA Case Select Function

Posted on 2014-01-22
4
980 Views
Last Modified: 2014-01-22
Hi Experts,

I tried to create a function which utilizes a number scale to convert a value to a mapped number as follows:

Function IGMap()
Dim IG As Double
Dim Map As Double
 
     Select Case IG
        Case Is < 16
            Map = 15
        Case Is >= 16 And IG < 18
            Map = 17
        Case Is >= 18 And IG < 20
            Map = 19
        Case Is >= 20 And IG < 21.5
            Map = 21
        Case Is >= 21.5 And IG < 23.5
            Map = 22
        Case Is >= 23.5 And IG < 26
            Map = 25
        Case Is >= 26 And IG < 28.5
            Map = 27
        Case Is >= 28.5 And IG < 35
            Map = 30
        Case Is >= 33 And IG < 50
            Map = 40
        Case Is >= 50 And IG < 62.5
            Map = 60
        Case Is >= 62.5 And IG < 67.5
            Map = 65
        Case Is >= 67.5 And IG < 71.5
            Map = 70
        Case Is >= 71.5 And IG < 74
            Map = 73
        Case Is >= 73 And IG < 76
            Map = 75
        Case Is >= 76 And IG < 78.5
            Map = 77
        Case Is >= 78.5 And IG < 81.5
            Map = 80
        Case Is >= 81.5 And IG < 84
            Map = 83
        Case Is >= 84 And IG < 86
            Map = 85
        Case Is >= 86 And IG < 88.5
            Map = 87
        Case Is >= 88.5 And IG < 92.5
            Map = 90
        Case Is >= 92.5 And IG < 96.5
            Map = 95
        Case Is >= 96.5 And IG < 98.5
            Map = 98
        Case Else
            Map = 99
        End Select
    IGMap = Map

End Function

When I use IGMap() in the criteria of a query, the query will run, but will return no values.  If I try instead to input a field value such as:  Test:  IGMap([field]) I get the error message 'The expression you entered has a function containing the wrong number of arguments.'

Any help on what I am doing wrong would be appreciated.

Thanks!
0
Comment
Question by:grmcra
  • 2
4 Comments
 
LVL 10

Assisted Solution

by:Anthony Berenguel
Anthony Berenguel earned 200 total points
ID: 39800544
Your function header Function IGMap() has no parameters(arguments), however you're trying to call the function with [field] as your parameter. Try this
Function IGMap(byval IG as double) as double
'Dim IG As Double
Dim Map As Double
 
     Select Case IG
        Case Is < 16
            Map = 15
        Case Is >= 16 And IG < 18
            Map = 17
        Case Is >= 18 And IG < 20
            Map = 19
        Case Is >= 20 And IG < 21.5
            Map = 21
        Case Is >= 21.5 And IG < 23.5
            Map = 22
        Case Is >= 23.5 And IG < 26
            Map = 25
        Case Is >= 26 And IG < 28.5
            Map = 27
        Case Is >= 28.5 And IG < 35
            Map = 30
        Case Is >= 33 And IG < 50
            Map = 40
        Case Is >= 50 And IG < 62.5
            Map = 60
        Case Is >= 62.5 And IG < 67.5
            Map = 65
        Case Is >= 67.5 And IG < 71.5
            Map = 70
        Case Is >= 71.5 And IG < 74
            Map = 73
        Case Is >= 73 And IG < 76
            Map = 75
        Case Is >= 76 And IG < 78.5
            Map = 77
        Case Is >= 78.5 And IG < 81.5
            Map = 80
        Case Is >= 81.5 And IG < 84
            Map = 83
        Case Is >= 84 And IG < 86
            Map = 85
        Case Is >= 86 And IG < 88.5
            Map = 87
        Case Is >= 88.5 And IG < 92.5
            Map = 90
        Case Is >= 92.5 And IG < 96.5
            Map = 95
        Case Is >= 96.5 And IG < 98.5
            Map = 98
        Case Else
            Map = 99
        End Select
    IGMap = Map

End Function

Open in new window

0
 

Author Comment

by:grmcra
ID: 39800604
Great, thanks...only remaining problem is that when I try Test:  IGMap([field]) it returns 17 for all of my values.

For example if the [field] value is 95.6 the returned value should be 95 (not 17), a [field]value of 81.25 should return 80 (not 17), etc.....
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 300 total points
ID: 39800635
You don't need the comparisons in the CASE statements. The SELECT does this be default. For example, if I pass in 22, then my SELECT statement evaluates the first CASE, which is "Case IS < 16". The value passed in (22) is not less than 16, so it goes to the next. 22 is not less that 18, so it goes to the next, and so on until it hits CASE IS <23.5 - so by default, the value passed in is LESS THAN 23.5, but GREATER THAN the last CASE statement (which was 21.5).

When it confirms one of the CASE statement evaluates correctly, no other CASE statements are evaluated, and the code skips to the END SELECT.

Also, depending on how you want to do the comparisons, you may need to do CASE IS <=XX. If you use the code as is, a value of 26 would produce a Map of 27, since 26 is not LESS THAN 26, but it is LESS THAN 28.5.

Function IGMap(ByVal IG As Double) As Double
'Dim IG As Double
Dim Map As Double
 
     Select Case IG
        Case Is < 16
            Map = 15
        Case Is < 18
            Map = 17
        Case Is < 20
            Map = 19
        Case Is < 21.5
            Map = 21
        Case Is < 23.5
            Map = 22
        Case Is < 26
            Map = 25
        Case Is < 28.5
            Map = 27
        Case Is < 35
            Map = 30
        Case Is < 50
            Map = 40
        Case Is < 62.5
            Map = 60
        Case Is < 67.5
            Map = 65
        Case Is < 71.5
            Map = 70
        Case Is < 74
            Map = 73
        Case Is < 76
            Map = 75
        Case Is < 78.5
            Map = 77
        Case Is < 81.5
            Map = 80
        Case Is < 84
            Map = 83
        Case Is < 86
            Map = 85
        Case Is < 88.5
            Map = 87
        Case Is < 92.5
            Map = 90
        Case Is < 96.5
            Map = 95
        Case Is < 98.5
            Map = 98
        Case Else
            Map = 99
        End Select
    IGMap = Map

End Function

Open in new window

0
 

Author Closing Comment

by:grmcra
ID: 39800651
Thanks so much for the explanation, it works great!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programmer 14 48
Can not open the Access Help ? 3 53
Access Update Query 1 20
How to get the closest date in a query in Access 2010 8 22
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

776 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