Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer audio backup black screen blue screen boot bsod computer connection crash css dell driver drivers email error ethernet excel firefox firefox 3 hard drive internet internet explorer itunes laptop linux malware monitor network networking outlook outlook 2003 outlook 2007 outlook express partition problem router security slow software sound trojan usb video virus vista wifi windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
newbie VB 6 question?


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Dougj's Avatar
Senior Member with 280 posts.
 
Join Date: Sep 2002
14-Dec-2004, 09:22 PM #1
newbie VB 6 question?
I am currently learning VB6 using Teach yourself Visual basic 6 in 24 hours.
In lesson 8 we are learning looping. The question asks us to write a program that contains a text box and a command button with a label telling the user to type a numvber between 1 and 10 in the text box. If the number is out of range issue an error message and if the number is within the range casue the computer to beep the number of times equal to the number entered into the text box.

The answer given is shown as follows:

==================================================

Private Sub Command1_Click()
' Beep or issue error
Dim intCtr As Integer
Dim msg As Variant ' MsgBox() return
If Val(txtNumber.Text) >= 1 And _
Val(txtNumber.Text) <= 10 Then
For intCtr = 1 To Val(txtNumber.Text)
' If your computer is fast, you may
' not hear a lot of beeps
Beep
Next intCtr
' Let the user enter another
txtNumber.Text = ""
Else
msg = MsgBox("You did not type a number from 1 to 10", _
vbExclamation, "Error")
txtNumber.SetFocus
End If

End Sub

Private Sub Command2_Click()
End
End Sub

Private Sub txtNumber_Change()

End Sub

=================================================

My code looks as follows :

=================================================

Dim msg As String
Dim intCounter As Integer


Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdPress_Click()
If (txtNumber.Text < 1) Or _
(txtNumber.Text > 10) Then
msg = MsgBox("Hey Stupid!! Your entry is out ofRange",
vbExclamation, "Input ERROR!")
'reset input box
txtNumber.Text = ""
Else
For intCounter = 1 To (txtNumber.Text)
Beep
Next intCounter
'reset input box
txtNumber.Text = ""

End If
End Sub

================================================

First question : If I use the "Val" around my (txtNumber.Text) the program operates but does not give me an error message when the number is out of range? WHY??
Why is the Val required??
Second question : whenever i try to break up a line that gets too long by using the "_" character ( as the instructor did) I get a complie error...illegal character. What am I doing wrong??
Third question : using my code the PC always only beeps once....no matter what number I input. WHY??
Fourth question : the instructors code declares "msg" as a Variant and I used String. What is the difference??

Thanks in advance for the help.........D.
Glaswegian's Avatar
Computer Specs
Distinguished Member with 3,084 posts.
 
Join Date: Dec 2004
Location: Erm...Glasgow?
Experience: of what?
17-Dec-2004, 05:47 AM #2
Hello

1. You are using a TextBox – but VBA has no way of knowing that the user has input a number – therefore use the Val Function which returns numbers from a string. For example
myString = “101 Dalmations”
Val(myString) would return 101.

You have to put your operators outside the parens as in
Code:
If Val(txtNumber.Text) < 1
You had
Code:
If Val(txtNumber.Text < 1)
It will therefore return the number from the Textbox – regardless of value.

2. Make sure you type a Space and then the underscore
Code:
If Val(txtNumber.Text) >= 1 And _
3. Depends on the PC. Even on the prehistoric junk I use at work, 6 beeps just run together as one.

4. A Variant is the data type for a variable that is not explicitly declared – but it is more expensive memory wise. It gives you more flexibility if you don’t know what kind of data will be assigned to the variable. A String variable is a list of characters and can be made up of letters and/or numbers. The return from a Message Box is actually an Integer that specifies which button was pressed.

HTH

Regards
__________________
Member of ASAP Member of UNITE

The length of a minute depends on which side of the bathroom door you are on.

SpywareBlaster::SpywareGuard::Spybot::Ad-Aware::HijackThis::HOSTS file::Get Firefox!
Dougj's Avatar
Senior Member with 280 posts.
 
Join Date: Sep 2002
18-Dec-2004, 04:54 PM #3
thanx for the help.. It has cleared up some confusion for me.

l8tr...d.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:37 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.