PDA

View Full Version : VB - calculator


jes
06-18-2002, 04:36 PM
Trying to make a calculator. Code is as follows:

Private Sub Command1_Click()
Number = Number + 1
End Sub

Private Sub Command2_Click()
Number = Number + 2
End Sub

Private Sub Text1_Change()
Text1 = Number
End Sub

Just the two buttons and the text box yet.
Why doesn't that work?

------------------
I’m not trying to do anything malicious…just curious.

jes
06-18-2002, 04:44 PM
I just tried this:

Private Sub Command1_Click()
Number = Number + 1
Text1 = Number
End Sub

Private Sub Command2_Click()
Number = Number + 2
Text1 = Number
End Sub

Still have problemss. It will put the 1 or the 2 in the text box but not both.

------------------
I’m not trying to do anything malicious…just curious.

Paul Komski
06-18-2002, 07:57 PM
Jes; you are trying hard and that is admirable. However you are asking some very basic questions and ,in a way, those are the hardest to answer because one cannot know or guess what you already know and what you dont. Most of these sort of answers can be found in the VB Help files, in examples and in simple courses. Just, for example, read up on the "Change" event and you will discover that it is not triggered simply from code - unless the code also "sets" or "updates" the value in some way.

When writing code, many things sound "logical" as the word is commonly used. In mathematics and programming, logic has a different meaning: logic here must follow absolutely strict rules. Learning this sort of logic and learning programming involves learning and UNDERSTANDING all of these rules. There are no shortcuts I'm afraid. Good luck anyways http://www.PCGuide.com/ubb/biggrin.gif

netharam
06-20-2002, 04:00 PM
Try this.

Private Sub Command1_Click()
Number = Number + 1
Text1 =Text1.Text+1

Private Sub Command2_Click()
Number = Number + 2
Text1 = Text1.Text+2
End Sub

Think in the way others Don't. http://www.PCGuide.com/ubb/biggrin.gif
Netharam. http://www.PCGuide.com/ubb/wink.gif

jes
06-24-2002, 11:50 AM
Couldn't get that to work so I tried this.
Private Sub Command1_Click()
Dim Number
Number = Number + 1
Text1 = Text1.Text + 1
End Sub

Private Sub Command2_Click()
Dim Number
Number = Number + 2
Text1 = Text1.Text + 2
End Sub

Didn't work either.

------------------
I’m not trying to do anything malicious…just curious.

geebee76
06-24-2002, 12:19 PM
jes, give this a try...I did it in VBA but I'm sure it will work with VB.

Keep at it!

Private Sub Command1_Click()
Dim num As Integer

If TextBox1.Value = "" Then 'Looks to see if textbox1 is empty

TextBox1.Value = 0 'if it is, then assigns the value of 0 to it

End If


num = TextBox1.Value + 1 'assigns the value of textbox1 + 1 to the variable num

TextBox1.Value = num 'assigns the value of the variable num to textbox1

End Sub

Private Sub Command2_Click()
Dim num As Integer

If TextBox1.Value = "" Then

TextBox1.Value = 0

End If


num = TextBox1.Value + 2

TextBox1.Value = num

End Sub

------------------
You are making progress if each mistake is a new one!

Sig from Here (http://www.oneliners-and-proverbs.com/)

geebee76@hotmail.com



[This message has been edited by geebee76 (edited 06-24-2002).]

jes
06-24-2002, 02:40 PM
What is VBA?

------------------
I’m not trying to do anything malicious…just curious.

geebee76
06-24-2002, 02:45 PM
Visual Basic for Applications. Used to program applications such as MS Excel and Word. You can't write standalone programs with it (you can't create executable files). It is very similar to VB.

How did the code work?

I lost all the formatting when I pasted the code into the reply box. Just copy and paste it with the comments. The comments are the words after each '
Comments don't affect the code.

------------------
You are making progress if each mistake is a new one!

Sig from Here (http://www.oneliners-and-proverbs.com/)

geebee76@hotmail.com

[This message has been edited by geebee76 (edited 06-24-2002).]

Paul Komski
06-24-2002, 07:34 PM
I have only just learned how to use code tags here and which will then retain the formatting - but you will have to use spaces not tabs. eg:-

If a = b then
If c = d then
DoCmd.Beep
Else
Msgbox "Sorry no Beeping"
End If
End If


It's not strictly true that applications using VBA can't be distributed as standalones though not necessarily as exe files. I think there are downloads available for both Word and Excel that will enable it. For Access you must have obtained the Developer Edition of Office/Access (depending on the version).

Also when using .text and .value properties of controls (like text boxes) .text returns a not-updated value, while .value returns an updated value. There can also be variations on this theme if you are using bound or unbound controls.

------------------
Take nice care of yourselves - Paul
"Those who say they don't let little things annoy them have never tried to sleep in a room with a buzzing mosquito."

geebee76
06-24-2002, 08:33 PM
Will it work?

Private Sub Command1_Click()

Dim num As Integer

num = TextBox1.Value + 1 'assigns the value of textbox1 + 1 to the variable num

TextBox1.Value = num 'assigns the value of the variable num to textbox1

End Sub

Private Sub Command2_Click()

Dim num As Integer

num = TextBox1.Value + 2

TextBox1.Value = num

End Sub

Private Sub UserForm_Activate()

TextBox1.Value = 0

End Sub


Paul, do you have any links to info regarding standalone applications with VBA?....I would be very interested.

------------------
You are making progress if each mistake is a new one!

Sig from Here (http://www.oneliners-and-proverbs.com/)

geebee76@hotmail.com



[This message has been edited by geebee76 (edited 06-24-2002).]

Paul Komski
06-24-2002, 09:02 PM
Don't know if Excel Viewer (http://office.microsoft.com/downloads/2000/xlviewer.aspx) works with code but could be worth trying it out.

Loads of stuff by searching through MSDN and its Library (http://msdn.microsoft.com/)

And the good old reliable Mr Google (http://www.google.com/search?q=standalone+vba&sourceid=opera&num=0&ie=utf-8&oe=utf-8)

I have been creating a couple of applications that are nearly ready for beta testing using Access VBA. I haven't decided yet whether to convert them to VB or get the Developer Edition of Office. All eMails to M$ so far have just directed me to telephone support. Perhaps I will know more in a week or two.

------------------
Take nice care of yourselves - Paul
"Those who say they don't let little things annoy them have never tried to sleep in a room with a buzzing mosquito."