Solved

ms access error handling

Posted on 2013-12-11
5
234 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

867 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

21 Experts available now in Live!

Get 1:1 Help Now