Solved

ms access error handling

Posted on 2013-12-11
5
237 Views
Last Modified: 2014-03-18
I keep getting a runtime error of 94 when I make the first select in the code below. How  do I create a error handle to capture null value or blank values.

Private Function calculateTotal(v1, v2)
    Dim iTier1 As Integer  ' number in control name for units sold controls - Tier1, Tier2, ...
    Dim iTier2 As Integer
    Dim cGSAR As Currency ' total per line
    Dim i As Integer 'counter for lines considered

THIS IS WHERE I BELIEVE I GET THE ERROR - I am making the first selection from combox.
 
    iTier1 = Right(v1, 1) ' gives 1 for US1, 2 for US2 and so on
    iTier2 = Right(v2, 1)
    cGSAR = 0
    For i = iTier1 To iTier2
        cGSAR = cGSAR + Me.Controls("Tier" & i)
    Next
   
    calculateTotal = cGSAR
End Function
0
Comment
Question by:deefel
  • 2
  • 2
5 Comments
 
LVL 84
ID: 39710840
You can use the Nz function:

If Nz(v1, "") <> "" Then
    iTier1 = Right(v1, 1) ' gives 1 for US1, 2 for US2 and so on
End If
If Nz(v2, "") <> "" Then
    iTier2 = Right(v2, 1)
End If
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 39710915
You have a number of ways.

One option would be:
'First, set iTier1 to a default value - empty, 0, whatever 
iTier1 = "0" 'Or something else (including "") since it's otherwise not valid.

'Then test if v1 meets the criteria necessary to assign a different value to iTier1 - 
'leave the default if not, otherwise, make iTier1 the new value.
If IsNull(v1) = False Then
   If Len(v1) > 0 Then
      If IsNumeric(v1) = True Then
         iTier1 = Right(v1, 1) ' gives 1 for US1, 2 for US2 and so on
      End If
   End If
End If

Open in new window

0
 

Author Comment

by:deefel
ID: 39712309
I tried the code, but still got the error message ~ it could be I put the code in th wrong place.  I have attached a sample database and a screenshot.  When in the database select from the drop down for Tier1/PL 1a value ~ this is where the error happens
example-1.zip
screen-shot.xlsx
0
 

Author Comment

by:deefel
ID: 39713543
Could I please get help with runtime error.  see example-1 database.  thanks
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 300 total points
ID: 39716728
You can't run CalculateTotal if either incoming value is NULL, so just check for that before you get started with the code:

Private Function calculateTotal(v1, v2)

    If Nz(v1, "") = "" Or Nz(v2, "") = "" Then
        Exit Function
    End If


    Dim iTier1 As Integer  ' number in control name for units sold controls - Tier1, Tier2, ...
    Dim iTier2 As Integer
    Dim cGSAR  As Currency    ' total per line
    Dim i      As Integer    'counter for lines considered


    iTier1 = Right(v1, 1)    ' gives 1 for US1, 2 for US2 and so on
    iTier2 = Right(v2, 1)
    cGSAR = 0
    For i = iTier1 To iTier2
        cGSAR = cGSAR + Me.Controls("Tier" & i)
    Next

    calculateTotal = cGSAR
End Function

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

830 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