How to run function

  • Thread starter mattc66 via AccessMonster.com
  • Start date
M

mattc66 via AccessMonster.com

Hi All,

I found this function on the web and I want to incorporate this into my
database to check the entry of credit card numbers. How would I add this
function to my form?


Public Function Mod10Check(sNum As String) As Boolean
' Check a credit card number using mod10

Dim i, iDigit, iTotal, iDoubleFlag As Integer

For i = Len(sNum) To 1 Step -1
iDigit = Asc(Mid$(sNum, i, 1)) - 48
If iDigit < 0 Or iDigit > 9 Then
Mod10Check = False
Exit Function
End If
If iDoubleFlag Then
iDigit = iDigit + iDigit
If iDigit > 9 Then iDigit = iDigit - 9
iDoubleFlag = False
Else
iDoubleFlag = True
End If
iTotal = iTotal + iDigit
Next
Mod10Check = (iTotal Mod 10 = 0)
End Function
 
T

Tom van Stiphout

On Thu, 19 Mar 2009 04:14:20 GMT, "mattc66 via AccessMonster.com"

I'm assuming you want to validate CC numbers based on this function.
In your textbox' BeforeUpdate event write the one-liner:
Cancel = Mod10Check(Me.myTextboxName)
(of course you change myObjectNames with yours)

-Tom.
Microsoft Access MVP
 
D

Danny J. Lesandrini

Well, you probably want to put it into a standard module, not behind the
form, since it's rather generic and you may need to call it from other
forms as well. Just paste it into a new module and save it with any
name you want.

Next, from your form you would "call" the function in code. Without knowing
your form, it's hard to say where you'd do that, but let's say you have a
command button named cmdModCheck. In the OnClick event, you'd add
this code ...

Dim fResult As Boolean
Dim strArg As String

strArg = Inputbox("Enter some text","Test Function")
fResult = Mod10Check(strArg)
MsgBox "The result is " & fResult


It's a little concerning to me, however, that a function to check a "number"
accepts a "string" as the argument. Expect this to break someday when
someone enters "OneTwoThreeFour" instead of "1234."
 
D

DStegon via AccessMonster.com

I agree that the type should be changed from string to long and checked that
is IsNumeric (standard vb function). you dont want people typing in 1234-1266-
1236-3435 with dashes, because if you ever connect a USB card reader to your
system to read the card it will not send you any dashes. You will have to
parse out the USB info, but the data is stored without "-".

Also.... just what are you trying to vaildate????
Well, you probably want to put it into a standard module, not behind the
form, since it's rather generic and you may need to call it from other
forms as well. Just paste it into a new module and save it with any
name you want.

Next, from your form you would "call" the function in code. Without knowing
your form, it's hard to say where you'd do that, but let's say you have a
command button named cmdModCheck. In the OnClick event, you'd add
this code ...

Dim fResult As Boolean
Dim strArg As String

strArg = Inputbox("Enter some text","Test Function")
fResult = Mod10Check(strArg)
MsgBox "The result is " & fResult

It's a little concerning to me, however, that a function to check a "number"
accepts a "string" as the argument. Expect this to break someday when
someone enters "OneTwoThreeFour" instead of "1234."
[quoted text clipped - 24 lines]
Mod10Check = (iTotal Mod 10 = 0)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top