?
Solved

Access 2010 VBA Case Select Function

Posted on 2014-01-22
4
Medium Priority
?
1,017 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
[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
  • 2
4 Comments
 
LVL 10

Assisted Solution

by:Anthony Berenguel
Anthony Berenguel earned 800 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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