Solved

ms access error handling

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

Highfive Gives IT Their Time Back

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now