Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2006, 02:27 PM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kevin_cheung Send a message via MSN to kevin_cheung
Default VB6 Help needed!!

Hi people, I'm doing a first year university course on VB6.

I need some help with my code. I am getting runtime error 13, type mismatch, its a really simple assignment but because my VB is a student version, I wasn't able to find out what the mismatch is and how to fix it.

This is supposed to just allow you to enter a value, when you click next, it adds the value from whatever was there before to make a new sum. It also shows count or number (how many values have been entered) and also average, the sum divided by count or number. When clear is clicked, it should clear everything.

Code:

Option Explicit
Private Sum As Double
Private Number As Integer
Private Sub cmdClear_Click()
    Sum = 0
    Number = 0
    txtValue.Text = vbNullString
    txtCount.Text = vbNullString
    txtSum.Text = vbNullString
    txtAverage.Text = vbNullString
    txtValue.SetFocus
End Sub

Private Sub cmdNext_Click()
    Dim Value As Double
    Dim Average As Double
    Value = CDbl(txtValue.Text)
    Number = CInt(txtCount.Text)
    Sum = CDbl(txtSum.Text)
    Average = CDbl(txtAverage.Text)
    Number = Number + 1
    Sum = Sum + Value
    Average = Value / Number
End Sub

Private Sub cmdExit_Click()
    Unload Form1
    End
End Sub


Thank you in advance for the help!

Kevin

 
Old January 29th, 2006, 03:34 PM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kevin_cheung Send a message via MSN to kevin_cheung
Default

Hi guys I got the thing to work on my own, but I do have some questions. Here is what I did.

Private Sub cmdNext_Click()
    Dim Value As Double
    Dim Average As Double
    numberCount = numberCount + 1
    txtCount.Text = numberCount
    Value = txtValue.Text
    Sum = Sum + Value
    txtSum.Text = Sum
    Average = Sum / numberCount
    txtAverage.Text = Average
    txtValue.SetFocus
    txtValue = vbNullString
End Sub

My question is what is the difference between declaring the data type of the variable and converting it? i.e. Dim X as Type or Private X As Type between X = CType(txtX.Text)?

Also the way I have done this, does it open it to any errors in data type?

Thanks.

 
Old January 30th, 2006, 06:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

statements like

myVariable = txtbox.Text

will always have problems because you do not know what the user entered in that text box. The right way is to use error handling, so you are always protected, like this:

on error goto errsub
dim myVal as MyType (like integer, double etc)
myval = txtbox.Text
exit sub
errsub:
msgbox "Error reading data: " & err.description

Marco
 
Old January 30th, 2006, 07:02 PM
Registered User
 
Join Date: Jan 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kevin_cheung Send a message via MSN to kevin_cheung
Default

Thanks Marco. The following code works, so what does setting erorr handling do differently?

Private Sub cmdNext_Click()
    Dim Value As Double
    Dim Average As Double
    numberCount = numberCount + 1
    txtCount.Text = numberCount
    Value = txtValue.Text
    Sum = Sum + Value
    txtSum.Text = Sum
    Average = Sum / numberCount
    txtAverage.Text = Average
    txtValue.SetFocus
    txtValue = vbNullString
End Sub

 
Old January 31st, 2006, 12:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

try to clear txtValue.Text, or enter "a" in it, and see what happens...
The problem is in the line
Value = txtValue.Text
Marco





Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed sumit_00 ASP.NET 2.0 Basics 2 August 20th, 2007 08:59 AM
What Dll is needed to pdf a crystal report frm vb6 daniel_arney Pro VB 6 3 September 25th, 2006 02:08 PM
URGENT help needed with VB6 database bassace VB Databases Basics 24 June 13th, 2005 01:08 PM
help needed grime C++ Programming 2 January 6th, 2005 09:49 PM
Big Help Needed: VB6 & Crystal8.5 reyboy Crystal Reports 2 September 22nd, 2004 08:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.