Link to home
Start Free TrialLog in
Avatar of pinkpaperstars
pinkpaperstars

asked on

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

so anyone who knows how to do this please help me! =)
Avatar of Jacamar
Jacamar

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
ASKER CERTIFIED SOLUTION
Avatar of supunr
supunr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
missing bracket in the line/...
if (Val(Right(Trim(txtVal), 1) <> CheckSum) then

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

for a 0 answer for c, add some code like this

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

I hope this is helpful
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

Avatar of pinkpaperstars

ASKER

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!