Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Novice code question


Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2002, 09:54 AM
wekurn
Guest
 
Posts: n/a
Default Novice code question


This week I am studying input boxes. I have a form with a text box, a command button and a label for the sum to be displayed.
Here is the code:

Private Sub Command1_Click()
Dim first As Single
Dim last As Single
Dim result As Single
first = Text1.Text
last = InputBox("Insert a value", "Value insertion")
result = first + last
Label1.Caption = result
End Sub

Private Sub Form_Load()
Text1.Text = ""
End Sub

I can get the text box and input box to add the values, but if I enter nothing, or press "cancel", I get the "type mismatch" error.
Where am I going wrong here?
Are errors handled later in the code? Is that what is meant by "error handling"
Reply With Quote
  #2  
Old 09-17-2002, 09:58 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

The InputBox returns a String.
You have last as a Single.
You will have to surround your InputBox with CSng(...) or Val(...)
CSng converts to a Single
and Val is the "generic" string to number convertor.

I suggest using Val(...) as it returns zero for an empty string.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 09-17-2002, 10:36 AM
wekurn
Guest
 
Posts: n/a
Default

Like this?:

last = CSng(InputBox("Insert a value", "Value insertion"))

It's still returning an error when the cancel button is pressed.

I have changed the code to this:

Private Sub Form_Load()
Text1.Text = "0"
End Sub

Private Sub Command1_Click()
Dim first As Single
Dim last As Single
Dim result As Single
If Text1.Text = "" Then
MsgBox ("sorry, try again")
End If
last = CSng(InputBox("Insert a value", "Value insertion", "0"))
first = Text1.Text
result = first + last
Label1.Caption = result
End Sub

This has cured the "type mismatch" error when the text box and input boxes are left null, but still the cancel button returns the mismatch error. I'm wondering if I'm beginning to create code that fixes code that fixes code that...

THANKS ICEPLUG!!!

BK
Reply With Quote
  #4  
Old 09-17-2002, 10:59 AM
iowahawk43's Avatar
iowahawk43 iowahawk43 is offline
Contributor
 
Join Date: Aug 2002
Location: Iowa
Posts: 450
Default

Or after your INPUT line, check for a valid valid before causing an error in your math!

If IsNumeric(Last) then
continue math
End if
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->