Error messages


Posted by Brad on December 21, 2001 2:26 PM

How do you code error messages? I need an error message to pop up when a user has entered in wrong data in a text box. IS that possible? Also how do I clear a picture box after something has been drawn into it. I want to redraw everytime new data is entered and do it all with the code under a command button. I've heard about a Cls method? Please help! If anyone can do it it's you guys.
thanks

Posted by Mark O'Brien on December 21, 2001 5:17 PM

Gart

Here's some more code. Been Xmas shopping etc, else I'd have posted earlier. Also, I can answer the cls question because I've got VB at home, which I'll do first. Whatever the name of your picture box is, all you need to do the clear the contents (a triangle I assume) is:

Me.Picture1.Cls

Okay, now the biggie, the error message, I have assumed that the error is for the a, b and c variables from before. This code will send an error message if one of the inputs in the textboxes is not numeric.

Public Sub Test()
' Declare Variables
Dim TxtBox As TextBox
Dim ErrTxt As String
Dim ErrTitle As String
Dim i As Integer

' Check if the value is numeric (we declared these as single variables before I believe)

For i = 1 To 3
Set TxtBox = Me.Controls("Text" & i)

If Not CheckNumber(TxtBox) Then
ErrTxt = "Please enter a numeric value in text box." & vbNewLine & _
"The following is not a number:" & TxtBox.Text
ErrTitle = "Stupid User Error"
ErrorMessage ErrTxt, ErrTitle
End If

Next

End Sub

Public Function CheckNumber(variable As Variant) As Boolean
CheckNumber = IsNumeric(variable)
End Function

Public Sub ErrorMessage(ErrTxt As String, ErrTitle As String)
' Put this as a separate routine and you can use it from anywhere in the program
MsgBox ErrTxt, vbInformation, ErrTitle
End Sub


I'll let you figure out how the code works, got to keep educational value in this somehow.

Posted by gart on December 21, 2001 6:41 PM

Mark,
I appreciate the great help. On the Me.picTriangle.Cls code where do I put that? I need it to clear it after I've drawn the triangle once and want to draw it again. Do I have to do a Do/While loop?
Also the code for the error message is great but I only need it to pop up when one side is entered that is greater than the sum of the other two. I think it's much simpler. I'm not worrying about IsNumeric.
Thanks Mark...you're the man.
Gart ' Check if the value is numeric (we declared these as single variables before I believe) For i = 1 To 3 Set TxtBox = Me.Controls("Text" & i) If Not CheckNumber(TxtBox) Then ErrTxt = "Please enter a numeric value in text box." & vbNewLine & _ "The following is not a number:" & TxtBox.Text ErrTitle = "Stupid User Error" ErrorMessage ErrTxt, ErrTitle End If Next



Posted by Mark O'Brien on December 21, 2001 8:49 PM

Brad,

You could put the cls code in several places. It may be best to put it as the first line of code that runs when people click the button to draw the triangle after the numbers have been put in.

(other places would be in the text1 change event, whereby the graphic would be cleared as soon as the first textbox was changed.)

Cheers

Mark

Mark, ' Check if the value is numeric (we declared these as single variables before I believe) For i = 1 To 3 Set TxtBox = Me.Controls("Text" & i) If Not CheckNumber(TxtBox) Then ErrTxt = "Please enter a numeric value in text box." & vbNewLine & _ "The following is not a number:" & TxtBox.Text ErrTitle = "Stupid User Error" ErrorMessage ErrTxt, ErrTitle End If Next