Validate field in VBA

Asked

Viewed 3,120 times

0

In Mozambique there is a unique tax identification number (Nuit), and this number takes 9 digits.
How an application would look to validate the Nuit meeting the following conditions?

  • If there are more(+) or less(-) than nine(9) digits, say "invalid Nuit."

  • Should accept only numbers, if they are not numbers say "please type only numbers"

I have here the algorithm:

Private Sub CommandButton1_Click()

v = validarnuit(TextBox1.Text)
MsgBox v

End Sub
Function validarnuit(nuit)
Dim cd As Double
Dim valido As String
valido = "Nuit Invalido"
If Len(nuit) = 9 Then
If IsNumeric(nuit) Then
If nuit <> 0 Then

cd = Mid(nuit, 8, 1) * 2
cd = cd + Mid(nuit, 7, 1) * 3 + _
cd = cd + Mid(nuit, 6, 1) * 4 + _
cd = cd + Mid(nuit, 5, 1) * 5 + _
cd = cd + Mid(nuit, 4, 1) * 6 + _
cd = cd + Mid(nuit, 3, 1) * 7 + _
cd = cd + Mid(nuit, 2, 1) * 2 + _
cd = cd + Mid(nuit, 1, 1) * 3 + _
cd = 11 - (cd Mod 11)
If cd = 9 Then
valido = "good"
End If
End If
End If
End If
validarnuit = valido
End Function

If the cd fore equal to 11, is false and does not validate; if it is equal to 10 also does not validate; only validates if cd is equal to 9. Image if the division is equal to 2, this would be 11-2 = cd 9 and validate. I tried using this code above but did not give 9. Does anyone have an effective idea of how to do this?

  • "but using Msaccess.vba", leads me to ask, is developing the EM Msaccess application or is developing an application that garter a Msaccess?

  • I am densevolver an application in vba that connects Msaccess.

  • What a mess..! I did my editing and only later I saw that this is a chameleon question, Waste of time... @Omni, feel free to revert to the version that gave rise to your answer (or as close as possible).

  • @brasofilo the answer remains valid for all editions, the only difference was the number of digits that the OP wanted to validate.

1 answer

2

(the following reply relates to this editing)

Assuming you are receiving data on TextBox can use the function Len to determine the size of a string:

tamanhoEsperado = 9 ' Tamanho do número esperado 
tamanho = Len(TextBox.Value)
If (tamanho > tamanhoEsperado) Then
    ' Invalido
ElseIf (tamanho < tamanhoEsperado) Then
    ' Invalido
Else
    ' Válido
End If

To be able to determine if they are just digits, the VBA has the function IsNumeric which you can use as follows:

If(IsNumeric(TextBox.Value)) Then
     ' São apenas números, grave na base de dados
EndIf

Edit:

To enter the field in the database:

' Abra a ligação
Set conn = New ADODB.Connection
conn.ConnectionString = "" 'Coloque aqui a sua connection string
conn.Open

' Crie a instrução INSERT INTO.
statement = "INSERT INTO nuit " & _
    "(nuit) " & _
    " VALUES (" & _
    "'" & txtnuit.Value & "', " & _
    ")"

' Execute a instrução.
conn.Execute statement, , adCmdText

' Feche a ligação.
conn.Close
  • Omni actually I wanted the connection module for the database and as would be the whole project I do not understand much of vba but I have a test.if you can help me

  • 1

    @user7752 your question does not explain this, in your question asks a validation system in VBA and not the connection module to the database.

  • yes Omni but actually I want the connection module with the database.

  • 2

    @user7752 is not this what do you want? And if that’s what you really want, why do you ask a question unrelated to what you actually want?

  • Private Sub Command2_click() size = Len(txtnuit.Value) If (size > 8) Then Msgbox "Nuit invalido", vbInformation, "information" Elseif (size < 8) Then Msgbox "Nuit invalido ", vbInformation, "information" Else Msgbox "valid nuite", vbInformation, "info" End If If (Isnumeric(txtnuit.Value)) Then from the debug here c.Open "select * from Nuit", c, adOpenStatic, adlockoptmistic c.Addnew c("Nuit") = "txtnuit" Msgbox "successfully registered Nuit", vbInformation, "information" c.Update End If End Sub .

  • the variable Whit is not defined.

  • @user7752 ran your code and works as expected (excluding the database part). One detail, to enter data has to use the operation INSERT

  • Yes omni but the idea is to do with the database also tried without the part of the database and it worked, if you can help in this regard to work with the database thank you.hugs

  • @user7752 added the code to insert into the database

  • continues Darb debug ,no Conn.Connectionstring = "microsoft.jet.oledb. 4.0;datasource = " + app.Path & " validate.mdb" Object required

  • @user7752 see here how to build your connection string

  • hasn’t worked out yet on this side.

  • Omni you have very nice with me.tanks for the tips you give me.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.