Click to See Complete Forum and Search --> : An advice.
hxjones
Nov 28th, 1999, 02:37 AM
Hi,
Once again I your need advice. I'm doing my first project and I want the numbers that the user puts in a text box to be stored and afterwards I can use these data to make some additions or substractions. The idea is that once the user writes the number in a text box and presses "Enter" this number will be stored and avalaible to do some simple mathematical operations.
I wrote this simple thing but it doesn't work. What is my mistake? Please help.
Option Explicit
Dim strTotal
Dim Stored
Dim Total
Private Sub Text1_KeyPrees(KeyAscii As Integer)
If KeyAscii = 13 Then
strTotal = Text1.Text
Stored = strTotal
Total = Total + Stored
End If
End Sub
Thanks in advance for your help,
Xenia Jones
------------------
spandex44
Nov 28th, 1999, 03:10 AM
The easiest way would be to store the numbers in a text file. Then you can retrieve the data even if the user shuts down the program and the restarts it. If you like, I could show you how to do that (if you don't already know).
Quadrex
Nov 28th, 1999, 04:17 AM
I think you code doesn't work because you're trying to add strings. Try this
Option Explicit
Dim strTotal As Integer
Dim Total As Integer
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
strTotal = CInt(Text1.Text)
Total = Total + strTotal
End If
End Sub
This should work. :)
------------------
Quadrex
webmaster@quadrex.f9.co.uk
Quadrex Programming (http://www.quadrex.f9.co.uk)
hxjones
Nov 29th, 1999, 01:32 AM
Hi,
Thanks to you both for your time. I don't know why but it didn't work.
It seems so simply but I can't do it.
Any other advice?
Thanks and best wishes,
Xenia Jones
MartinLiss
Nov 29th, 1999, 02:24 AM
If you dimension your global variables as followsDim strTotal As Integer
Dim Stored As Integer
Dim Total As Integer
it will work. That is, if you type 6 in your textbox, press Enter, then replace the 6 with a 4 and press Enter again, Total will contain 10. If that's not what you want to have happen, please give an example of what you want.
------------------
Marty
spandex44
Nov 29th, 1999, 02:26 AM
Hi again,
Looking back at my advice, I have some code that you could try. However, I would place the code in a Command button. I'm assuming that you've done that instead of using the ENTER button.
So, when you want to save the numbers, use this code (I'm assuming that you want to save only one number from Text1):
open "C:\My Documents\Numbers.txt" for output as #1
write #1, text1.text
close #1
Then, when you want to retrieve the number, use this code:
dim number
open "C:\My Documents\Number.txt" for input as #1
do while not eof(1)
input #1, number
loop
close #1
Now, when you want to perform the equation, use this:
dim total
total = total + number
I hope that this helped. If not, just tell me.
------------------
Regards,
Alexander McAndrew
VB Zone
http://gsenterprise.server101.com
hxjones
Nov 29th, 1999, 10:02 PM
Thanks a lots for your help. I really appreciate what you have done and of course your time. I hope do some day the same thing for you!
Best wishes,
Xenia Jones
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.