Solved

Keeping a MsgBox

Posted on 2003-10-26
30
431 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
ID: 9624329
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
ID: 9624335
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
ID: 9624350
What do you mean? "Error MsgBox to stick when the number is too large"
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 8

Expert Comment

by:ampapa
ID: 9624353
Sorry, I'm a little slow.
0
 
LVL 4

Expert Comment

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

Author Comment

by:kalaka
ID: 9624392
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
ID: 9624399
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
ID: 9624433
The MsgBox still doesnt stay open though....
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9624449
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
ID: 9624467
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
ID: 9624572
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
ID: 9624697
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
ID: 9624774
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
ID: 9624950
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
 
LVL 100

Expert Comment

by:mlmcc
ID: 9624980
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
ID: 9625183
                   
                 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
ID: 9625882
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
ID: 9629794
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
ID: 9629909
Which version of VB are you using?
0
 

Author Comment

by:kalaka
ID: 9630006
6... like everyone else here
0
 
LVL 4

Expert Comment

by:brother7
ID: 9630024
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
ID: 9630079
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
ID: 9630558
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
ID: 9630645
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
ID: 9631077
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
ID: 9631116
Very good... I guess sloppy working code is better than clean non-working code :b
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9631340
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
ID: 10198293
PAQed, with points refunded (50)

PashaMod
Community Support Moderator
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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