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 August 22nd, 2006, 10:28 PM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default basic textbox property

What is wrong with my code?

Private Sub Form_Load()
Dim y, y1, y2 As Double

y1 = Val(txtGencharge.Text)
y2 = Val(txtFranchise.Text)
y = y1 + y2
txtGencorev.Text = Str$(y)
End Sub

I want to work is this way. As the form loads, the user will enter values at txtGencharge and at txtFranchise. As the user tabs to another field inside the form, the textbox txtGencorev.Text displays the variable y without clicking any button.

Thank you!
 
Old August 23rd, 2006, 11:17 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

First of all with
Code:
    Dim y, y1, y2 As Double
y and y1 are Variants. Only y2 is a Double.

Your code fills in txtGencorev.Text when the form first opens. Move everything to a different location.
Code:
Private Sub Form_Load()
End Sub
————————————————————————————————————————
Private Sub txtGencharge_LostFocus()

    Dim y As Double, y1 As Double, y2 As Double

    y1 = Val("0" & txtGencharge.Text)  ' Protect against an empty control.
    y2 = Val("0" & txtFranchise.Text)  ' Protect against an empty control.
    y = y1 + y2
    txtGencorev.Text = CStr(y)

End Sub
————————————————————————————————————————
Private Sub txtFranchise_LostFocus()

    Dim y As Double, y1 As Double, y2 As Double

    y1 = Val("0" & txtGencharge.Text)  ' Protect against an empty control.
    y2 = Val("0" & txtFranchise.Text)  ' Protect against an empty control.
    y = y1 + y2
    txtGencorev.Text = CStr(y)

End Sub
But perhaps better:
Code:
Private Sub Form_Load()
End Sub
————————————————————————————————————————
Private Sub txtGencharge_LostFocus()

    txtGencorev.Text = CStr(CDbl("0" & txtGencharge.Text) + _
                            CDbl("0" & txtFranchise.Text))

End Sub
————————————————————————————————————————
Private Sub txtFranchise_LostFocus()

    txtGencorev.Text = CStr(CDbl("0" & txtGencharge.Text) + _
                            CDbl("0" & txtFranchise.Text))

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the backcolor property of the textbox Fehrer Access VBA 9 April 19th, 2016 10:59 AM
change TextBox.BackColor property onFocus event drasko ASP.NET 1.0 and 1.1 Basics 8 January 26th, 2009 12:23 AM
How to set textbox property to readonly when text bekim Classic ASP Basics 1 July 12th, 2005 12:06 AM
TextBox.Text property returns only old value? humour General .NET 9 August 3rd, 2004 07:01 AM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





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