Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

visual basic check digit validation

Posted on 2003-03-05
6
Medium Priority
?
1,424 Views
Last Modified: 2013-11-25
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! =)
0
Comment
Question by:pinkpaperstars
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:Jacamar
ID: 8077266
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
 
LVL 11

Accepted Solution

by:
supunr earned 200 total points
ID: 8077269
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
 
LVL 11

Expert Comment

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

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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Jacamar
ID: 8077295
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
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8077300
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 Comment

by:pinkpaperstars
ID: 8077344
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question