# visual basic check digit validation

hey all,
i am working on an assignment for class in which we enter a upc into a text box and it must be in the format mmmppp-c where c is the check digit. I need to validate the check digit by using this algorithm.

Calculate the sum of the odd-spaced digits (the 1st, 3rd, and 5th) Save that as Answer 1. Multiply Answer 1 by 3. Calculate the sum of the even-spaced digits (the 2nd, 4th, and 6th) and add this to answer1. The check digit should be whatever number you have to add to your last answer to get it up to the next multiple of ten.

example valid upc's are 111222-3, 333444-9

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Private Sub Text1_Lostfocus()
Dim stString As String
Dim stTemp As String

stString = Text1.Text

stTemp = Int(Mid(stString, 1, 1)) + Int(Mid(stString, 3, 1)) _
+ Int(Mid(stString, 5, 1))
stTemp = stTemp * 3
stTemp = stTemp + Int(Mid(stString, 2, 1)) + Int(Mid(stString, 4, 1)) _
+ Int(Mid(stString, 6, 1))

If 10 - Int(Right(stTemp, 1)) = Int(Right(stString, 1)) Then
Call MsgBox("It is good", vbInformation)
Else
Call MsgBox("not good", vbInformation)
Call Text1.SetFocus
End If

End Sub
0
Commented:
Dim txtVal as string
Dim SumOdds as long
Dim SubEvens as long
DIm CheckSum as long

txtVal = Text1.text
SumOdds = Val(Mid(txtVal, 1, 1)) + Val(Mid(txtVal, 3, 1)) + Val(Mid(txtVal, 4, 1))
SumEvens = Val(Mid(txtVal, 2, 1)) + Val(Mid(txtVal, 4, 1)) + Val(Mid(txtVal, 6, 1))
CheckSum = SumOdds * 3 + SumEvens
CheckSum = 10 - (CheckSum MOD 10)

If (Val(Right(Trim(txtVal), 1) <> CheckSum) then
MsgBox "CheckSum Failed"
End If

Good Luck!
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
missing bracket in the line/...
if (Val(Right(Trim(txtVal), 1) <> CheckSum) then

correct to ....
if (Val(Right(Trim(txtVal), 1)) <> CheckSum) then

0
Commented:

If int(right(stTemp, 1)) = 0 and int(Right(stString,1)) = 0 then

Call msgbox("it is good",vbinformation)

end if

add this code in this part.

If 10 - Int(Right(stTemp, 1)) = Int(Right(stString, 1)) Then
Call MsgBox("It is good", vbInformation)
Else
If int(right(stTemp, 1)) = 0 and int(Right(stString,1)) = 0 then
Call msgbox("it is good",vbinformation)
else
Call MsgBox("not good", vbInformation)
Call Text1.SetFocus
end if
End If

0
Commented:
Final Code looks like

Private Sub Text1_Lostfocus()
Dim stString As String
Dim stTemp As String

stString = Text1.Text

stTemp = Int(Mid(stString, 1, 1)) + Int(Mid(stString, 3, 1)) _
+ Int(Mid(stString, 5, 1))
stTemp = stTemp * 3
stTemp = stTemp + Int(Mid(stString, 2, 1)) + Int(Mid(stString, 4, 1)) _
+ Int(Mid(stString, 6, 1))

If 10 - Int(Right(stTemp, 1)) = Int(Right(stString, 1)) Then
Call MsgBox("It is good", vbInformation)
Else
If int(right(stTemp, 1)) = 0 and int(Right(stString,1)) = 0 then
Call msgbox("it is good",vbinformation)
else
Call MsgBox("not good", vbInformation)
Call Text1.SetFocus
end if
End If

End Sub

0
Author Commented:
Thanks you guys! That helped a lot because i was totally clueless! This was my first time on here so i hope i am doing everything right!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.