Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to total 4 figures in a text box 1

Status
Not open for further replies.

bebbo

Programmer
Dec 5, 2000
621
GB
I have a text box which holds four digits. I want to total those digits and get the last number.

Ok I know I could do it as below

Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim num5 As Integer
Dim num6 As Integer

num1 = 1234
num2 = Mid(num1, 1, 1)
num3 = Mid(num1, 2, 1)
num4 = Mid(num1, 3, 1)
num5 = Mid(num1, 4, 1)
num6 = num2 + num3 + num4 + num5

If Len(Num6) = 2
NumRequired = Mid(Num6,2,1)
Else
NumRequired = Num6
End If

I was wondering if there was an easier and quicker way of doing it?

 
you could use a loop and add the values to an array or listbox then do caluculations, or total them as you go.
 
A simple function would do:

Public Function fnAddDigits(myNum As String)
Dim intCount As Integer
For intCount = 1 To 4
fnAddDigits = fnAddDigits + Val(Mid(myNum, intCount, 1))
Next intCount
fnAddDigits = Right(fnAddDigits, 1)
End Function

You'll need to add error checking etc of course

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hi john,

I know i'm sounding stooopid but what does the val and right functions do?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
x508 - guess you know what val does now - we've just talked about this in another thread!

right:

Returns a Variant (String) containing a specified number of characters from the right side of a string.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Hmmm.
OK
So if I had a string with value MyNameIsX and I used mid to jump to the 3rd character of that string and used right, it would return NameIsX?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
You dont need to use Mid and Right. For example:
Code:
Option Explicit
Dim MyValue As String

Private Sub Command1_Click()
MsgBox Right(MyValue, 7)
End Sub

Private Sub Command2_Click()
MsgBox Mid(MyValue, 3, 7)
End Sub

Private Sub Form_Load()
MyValue = "MyNameIsX"
End Sub


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I see....

Now why do we then have two functions that do precisely the same thing, any specific stuff you can do with these seperate functions?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
mid returns a string of specific length starting from a specific point.

mid(OriginalString, StartPos, LengthofString)

left() returns a string from the left of specific position

left(OriginalString, LengthOfString)

right() returns a string from the right of specific position

right(OriginalString, StartPosition)

BB
 
ta bb

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Thanks for the
star.gif


Mid() can also be used to change a string in a string:

myString = "abcde"
mid(myString,3,1) = "x"

will convert myString to:
abxde

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top