Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when user clears TextBox

Status
Not open for further replies.

1ooo1

Technical User
Jan 20, 2003
50
AU
Hi,
I have a project using VB6 where On Change of Text1.text a calculation is preformed. The TextBox is defaulted to "0" and as the user enters figures into the box, the calculation is done, but my problem is if the user clears the TextBox, they get an error message. How Do I get around this?

Ken
 

1ooo1, have you read FAQ222-2244 Item 15 yet?

If Trim(Text1.Text) = vbNullString Then Exit Sub

Good Luck

 
vb5prgrmr
Thanx for the reply, but that did not seem to work!

Ken
 

Perhaps a piece of code from you may be in order so we can see what the problem is.

Good Luck

 
Here is the code I am using, it is a bit crude but works pperfectly as is, but if I want to use 'On_Change()' instead of Command1_Click() then it does not like when a user clears Text1

Private Sub Command1_Click()
Dim A As Single
Dim LSL As Variant
Dim B As Variant
Dim C As Variant
Dim D As Variant
Dim E As Variant
Dim F As Variant
Dim G As Variant
Dim H As Variant
Dim I As Variant
A = Text1.Text
LSL = 0.002
B = Text1.Text * 0.01
C = (Text1.Text - 5000) * 0.0035
D = (Text1.Text - 100000) * 0.002
E = (Text1.Text / 1000) * 3
F = ((Text1.Text - 50000) / 1000) * 3.64
G = ((Text1.Text - 250000) / 1000) * 2.34
H = ((Text1.Text - 500000) / 1000) * 1.64
I = ((Text1.Text - 1000000) / 1000) * 1.44

'Road Damage Bond

If (A < 5001) And Check5 = 1 Then
Label7(4).Caption = 0
Else
If (A > 5000) And (A < 20001) And Check5 = 1 Then
Label7(4).Caption = 210
Else
If (A > 20000) And (A < 100001) And Check5 = 1 Then
Label7(4).Caption = 550
Else
If (A > 100000) And (A < 300001) And Check5 = 1 Then
Label7(4).Caption = 825
Else
If (A > 300000) And (A < 500001) And Check5 = 1 Then
Label7(4).Caption = 1100
Else
If (A > 500000) And (A < 1000001) And Check5 = 1 Then
Label7(4).Caption = 2200
Else
If (A > 1000000) And Check5 = 1 Then
Label7(4).Caption = &quot;Contact Council&quot;
Else
If (A = 0) Then
Label7(4).Caption = 0
Else
Label7(4).Caption = 0
End If
End If
End If
End If
End If
End If
End If
End If

'Kerb and Gutter Inspection Fee
If (A > 5000) And Check2 = 1 Then
Label4(1).Caption = &quot;100&quot;
Else
If (A = 0) Then
Label4(1).Caption = &quot;0&quot;
Else
Label4(1).Caption = &quot;0&quot;
End If
End If

'Long Service Levy
If (A > 24999) And Check4 = 1 Then
Label6(3).Caption = Int((A) * (LSL))
Else
If (A = 0) Then
Label6(3).Caption = &quot;0&quot;
Else
Label6(3).Caption = &quot;0&quot;
End If
End If

'Construction Certificate
'First $5000 or part of = $50 plus 1% of Building Cost
'Next $95000 or part of = 0.35% of Building Cost
'Next $150000 or part of = 0.2% of Building Cost
'Exceeding $250000 = 0.2% of Building Cost
'0.5 added to calculations to round up to next dollar
If (A < 5001) And (A > 0) And Check3 = 1 Then
Label5(2).Caption = Int((B + 50) * 1.1)
Else
If (A > 5000) And (A < 100001) And Check3 = 1 Then
Label5(2).Caption = Int(((C + 100) * 1.1) + 0.5)
Else
If (A > 100000) And (A < 250001) And Check3 = 1 Then
Label5(2).Caption = Int(((D + 433) * 1.1) + 0.5)
Else
If (A > 250000) And Check3 = 1 Then
Label5(2).Caption = Int(((D + 433) * 1.1) + 0.5)
Else
If (A = 0) Then
Label5(2).Caption = 0
Else
Label5(2).Caption = 0

End If
End If
End If
End If
End If

'DA
If (A > 0) And (A < 5001) And Check1 = 1 Then
Label3(0).Caption = 110
Else
If (A > 5000) And (A < 50001) And Check1 = 1 Then
'Added .5 onto figures to round up to next dollar
Label3(0).Caption = Int(E + 170.5)
Else
If (A > 50000) And (A < 250001) And Check1 = 1 Then
'Added .5 onto figures to round up to next dollar
Label3(0).Caption = Int(F + 352.5)
Else
If (A > 250000) And (A < 500001) And Check1 = 1 Then
'Added .5 onto figures to round up to next dollar
Label3(0).Caption = Int(G + 1160.5)
Else
If (A > 500000) And (A < 1000001) And Check1 = 1 Then
'Added .5 onto figures to round up to next dollar
Label3(0).Caption = Int(H + 1745.5)
Else
If (A > 1000000) And (A < 10000001) And Check1 = 1 Then
'Added .5 onto figures to round up to next dollar
Label3(0).Caption = Int(I + 2615.5)
Else
If (A = 0) Then
Label3(0).Caption = 0
Else
If Check1 = 0 Then
Label3(0).Caption = 0
Else
Label3(0).Caption = &quot;Contact Council&quot;
End If
End If
End If
End If
End If
End If
End If
End If

'Totals
Label8.Caption = Val(Label7(4).Caption) + Val(Label4(1).Caption) + Val(Label6(3).Caption) + Val(Label5(2).Caption) + Val(Label3(0).Caption)
End Sub
 

Well this works just fine (for me)...
[tt]
Option Explicit

Private Sub Form_Load()
Text1.Text = &quot;0&quot;
End Sub

Private Sub Text1_Change()
If Trim(Text1.Text) = vbNullString Then
Label1.Caption = &quot;No Data&quot;
Exit Sub
End If
Label1.Caption = Text1.Text
End Sub
[/tt]

Where I would suggest that you put (If Trim(Text1.Text) = vbNullString Then Exit Sub) is right after your declarations (Dim I As Varient).

Good Luck

 
if len(text1.text) < 1 then exit sub

this will do basiclly the same as what vb5prgrmr suggested, just a slightly different way.

if the text in the box has a length of zero then tehre is no text!


Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 

And if someone enters a minus or dot and there are no other characters?

You can check the input using:
Code:
Private Sub Text1_Change()
    Static bCancel As Boolean
    If bCancel Then Exit Sub

    If IsNumeric(Text1.Text) Then
       'Do the calculations
    ElseIf Not IsNumeric(Text1.Text & &quot;0&quot;) Then
        'This takes care of the Minus and dots where nothing else was entered
        'Here would be better to use the Win API Edit box Undo, 
        for which I had some code posted for a while back.
function, and also to capture the SelStart in order to reset it. But for this example, the below should do
        'Set the text box Text to nothing and prevent the code in this event from executing again:
        bCancel = True
        Text1.Text = &quot;0&quot;
        Text1.SelStart = 0
        Text1.SelLength = 1
    End If
    bCancel = False
End Sub
 

1ooo1, did any of these posts help? Have you read FAQ222-2244 item 15 yet?

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top