Solved

Access 2010 VBA Case Select Function

Posted on 2014-01-22
4
966 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now