Solved

Keeping a MsgBox

Posted on 2003-10-26
30
424 Views
Last Modified: 2012-05-04
Private Sub text1_Change()
  On Error GoTo error1:
    answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Exit Sub
error1:
x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
If x = abort Then Unload Me
If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
If x = Ignore Then Resume Next
  End Sub
  Private Sub text2_Change()
  On Error GoTo error1:
    answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Exit Sub
error1:
x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
If x = abort Then Unload Me
If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
If x = Ignore Then Resume Next
  End Sub

This is an adding program, nothing very complicated but I cant get the Error MsgBox to stick when the number is too large. Any ideas how I can do this or improve my coding?
0
Comment
Question by:kalaka
  • 8
  • 7
  • 2
  • +8
30 Comments
 
LVL 8

Expert Comment

by:fozylet
Comment Utility
can't get what you mean by saying 'get the error msgbox to stick'

You might try using to get an idea of what error is occuring.

MsgBox "Error Occured: " & Err.Description,vbAbortRetryIgnore,"ERROR"

If you want to limit it for any particular datatype, try assigning the text value to a variable before adding.

e.g.

Dim numOne As Integer, numTwo As Integer
numOne = CInt(Text1.Text)
numTwo = CInt(Text2.Text)
answear.caption = Str$(numOne+numTwo) '........ or should it be answer.caption?!!
0
 

Author Comment

by:kalaka
Comment Utility
What I mean by 'get the error msgbox to stick', is I cant get the MsgBox to stay open when the error accurs. The MsgBox Closes as soon as it opens up.
0
 
LVL 8

Expert Comment

by:ampapa
Comment Utility
What do you mean? "Error MsgBox to stick when the number is too large"
0
 
LVL 8

Expert Comment

by:ampapa
Comment Utility
Sorry, I'm a little slow.
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
I think you want to use the VB constants
   vbABORT
   vbRETRY
   vbIGNORE
0
 

Author Comment

by:kalaka
Comment Utility
OK:
1) I know its written answer not answear, that was a tiny mistake

2) I have fixed some inicial mistakes (although I keep getting the same error):
If x = vbAbort Then Unload Me
If x = vbRetry goto error1:
If x = vbIgnore Then Resume Next

3) WHAT I MEAN BY THE MSGBOX TO STICK is that THE MSGBOX DOESNT STAY OPEN, AS SOON AS IT POPS, IT GOES AWAY. In other words, the Message Box doesnt stay there waiting to be clicked, but goes away, closes.
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
Another problem that you have is in the GoTo statement, you should omit the colon, so it should read
   On Error GoTo error1
not
   On Error GoTo error1:
0
 

Author Comment

by:kalaka
Comment Utility
The MsgBox still doesnt stay open though....
0
 
LVL 8

Expert Comment

by:fozylet
Comment Utility
What does the messagebox display when it comes up?!
(if it stay long enuf for you to read it)
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
If I were coding, I would try something like this:

--- Code Start ---

Private Sub Text1_Change()
   On Error GoTo ErrorHandler
    answer.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Exit Sub

ErrorHandler:
   MsgBox(Str(Error.Number) & ": " & Err.Description & chr$(10) & "Try again.", vbOKOnly, "ERROR")
End Sub

--- Code End ---
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You most likely have a typo somewhere that is causing problems in your code.  You may have mistyped the name of one the controls or something silly like that.

Put

Option Explicit

at the top of your form code and run the program so you can see where the problem is probably occurring.

Hope this helps.
0
 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
I have tried your code, author, but found not problem. The message box at my pc stay open. I think this is not the cause on the code. There is not error on the code that cause the box to disapper as soon as it appear.
0
 
LVL 1

Expert Comment

by:smallboyzkh2002
Comment Utility
Let test this code.

'-------------------Start Code-------------------------------'

Option Explicit

Dim x
Dim maxNumber As String


Private Sub Form_Load()
   
    maxNumber = "9999999"
   
End Sub

Private Sub text1_Change()

    If Val(Text1.Text) < Val(maxNumber) Then
        answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Else
        x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
        If x = vbAbort Then Unload Me
        If x = vbRetry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
        If x = vbIgnore Then Exit Sub
    End If
   
End Sub

Private Sub text2_Change()

    If Val(Text2.Text) < Val(maxNumber) Then
        answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Else
        x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
        If x = vbAbort Then Unload Me
        If x = vbRetry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
        If x = vbIgnore Then Exit Sub
    End If
End Sub

'-------------------End Code-------------------------------'

0
 
LVL 2

Expert Comment

by:BUCHAS
Comment Utility
Try adding the vbSystemModal or vbApplicationModal with the parameters:

Private Sub text1_Change()
  On Error GoTo error1:
    answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Exit Sub
error1:
x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore Or vbSystemModal , "ERROR")
If x = abort Then Unload Me
If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
If x = Ignore Then Resume Next
  End Sub
  Private Sub text2_Change()
  On Error GoTo error1:
    answear.Caption = Str$(Val(Text1.Text) + Val(Text2.Text))
    Exit Sub
error1:
x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore Or vbSystemModal, "ERROR")
If x = abort Then Unload Me
If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
If x = Ignore Then Resume Next
  End Sub
0
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!

 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
What number causes it to be too large?

I added your code t a form and put the appropriate text boxes and label on it.  I put in some very large numbers and never got an error. (250 1's)

mlmcc

0
 
LVL 1

Expert Comment

by:smallboyzkh2002
Comment Utility
                   
                 This code if for displaying your message while the number that you input is bigger than Intger type( from -32,768 to 32,767).

If you need your number bigger than integer type you can change this line
    answear.Caption = Str$(CInt(Val(Text1.Text)) + CInt(Val(Text2.Text)))
to
    answear.Caption = Str$(CLng(Val(Text1.Text)) +CLng(Val(Text2.Text)))


*****************************************************************


'-----------------------------------Start Code---------------------------------------'

Private Sub text1_Change()
     
      On Error GoTo error1:
        answear.Caption = Str$(CInt(Val(Text1.Text)) + CInt(Val(Text2.Text)))
        Exit Sub
error1:
    x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
    If x = abort Then Unload Me
    If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
    If x = Ignore Then Resume Next

End Sub

Private Sub text2_Change()
     
      On Error GoTo error1:
        answear.Caption = Str$(CInt(Val(Text1.Text)) + CInt(Val(Text2.Text)))
        Exit Sub
error1:
    x = MsgBox("The number you inputed is too large, or some other error occured", vbAbortRetryIgnore, "ERROR")
    If x = abort Then Unload Me
    If x = retry Then answear.Caption = Str$(Val(Text1.Text) + Val(Text.Text))
    If x = Ignore Then Resume Next

End Sub
'-----------------------------------End Code---------------------------------------'

Hope this one that you need................. :-)
0
 
LVL 3

Expert Comment

by:zapthedingbat
Comment Utility
The cause of your problem might be somthing to do with the error trapping in your code
Debuging code if often easyer if you test for the cause of an error rather than trapping everything.
Silently swallowing all errors means you dont know what the error is being caused by.

Try the following code...

<code language="VisualBasic">
Private Function Addition (n1, n2) As Long
      Dim Q
      Addition = 0
      Do
            'test to see if the inputs are numeric
            If IsNumeric(n1) And IsNumeric(n2) Then
                  Addition = n1 + n2 ' Set the caption to the sum of the 2 values
            Else
                  Q = MsgBox("Input Error", vbAbortRetryIgnore)
                  'exit the loop if user chooses Abort otherwize try again
                  If Q = vbAbort Then Exit Do
            End If
      Loop
End Sub
Private Sub text1_Change()
      answear.Caption = Addition Text1, Text2
End Sub
Private Sub text2_Change()
      answear.Caption = Addition Text1, Text2
End Sub
</code>
0
 

Author Comment

by:kalaka
Comment Utility
Amazingly enough, experts, none of this solutions worked. :-(
I've changed the whole code three times already, adding Dim x, Option Explicit, changing the MaxNumber to an Integer type by adding CInt and I've added vbSystemModal to the MsgBox.
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
Which version of VB are you using?
0
 

Author Comment

by:kalaka
Comment Utility
6... like everyone else here
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
Actually, I'm using VB.NET.  But I'm trying to help you out by thinking in VB6ese.
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
I guess you didn't bother trying out my suggested code, given earlier.  Have you determined what kind of error you are getting?  Is it a variable overflow or a variable undefined error?  Maybe you can write this info to the Console or a Form label, using this as your goto:

error:
   answer.Caption = Str(Error.Number) & ": " & Err.Description
0
 

Author Comment

by:kalaka
Comment Utility
brother7:
I know I'm suposed to get an overflow error, but I dont want it to say "Run-time Error '#' - Overflow" I wnat a MsgBox to appear and give the user the option to Abort, Ignore or Retry. THATS what the error1: label is all about!!
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
I know that you're trying to catch an overflow error.
My suggestion is an intermediate troubleshooting step meant to determine what kind of error is occuring.  Since you spelled answer as 'answear', it's possible that the error is due to an undefined variable.
I think you're trying to jump from problem to solution without performing the troubleshooting steps that are necessary when trying to track down a bug.

You may wish to try this:
   Private Sub text1_Change()
      MsgBox("Testing... 1,2,3", vbAbortRetryIgnore, "MsgBox Test")
   End Sub
to see if the problem is with MsgBox in general, or your particular implementation of it.
0
 

Author Comment

by:kalaka
Comment Utility
OK I have rewriten my code and for some reason it worked:
[new code]
Option Explicit
Dim x
Dim a
Dim b
Dim n
Private Sub form_load()
a = Val(Text1.Text)
b = Val(Text2.Text)
Answear.Text = a + b
End Sub
Private Sub text1_change()
On Error GoTo sampi
form_load
Exit Sub
sampi:
x = MsgBox("The added number is too large, or there was an error of some other kind", vbAbortRetryIgnore, "ERROR")
If x = vbAbort Then Unload Me
If x = vbRetry Then GoTo sampi
If x = vbIgnore Then Resume Next
End Sub
Private Sub text2_change()
On Error GoTo sampi
form_load
Exit Sub
sampi:
x = MsgBox("The added number is too large, or there was an error of some other kind", vbAbortRetryIgnore, "ERROR")
If x = vbAbort Then Unload Me
If x = vbRetry Then GoTo sampi
If x = vbIgnore Then Resume Next
End Sub
[/new code]
Thankyou anyway experts :)
0
 
LVL 4

Expert Comment

by:brother7
Comment Utility
Very good... I guess sloppy working code is better than clean non-working code :b
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
So how does that detect any errors.  Since it is a text box I can enter anything I want.  The Change event won't detect an error nor will the form load since avl will only take the digits on the left.

If I enter 1234 into Text1 and 1234t into Text2  I will only get 2468 an ANSWEAR.  It will not error.

mlmcc
0
 

Accepted Solution

by:
PashaMod earned 0 total points
Comment Utility
PAQed, with points refunded (50)

PashaMod
Community Support Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

8 Experts available now in Live!

Get 1:1 Help Now