alprieto
alprieto

Reputation: 59

VBA MS ACCESS: How can I fix first condition in if?

I've got an if. In the first condition I'm using a Dlookup function in order to check if the value of a text in a form is different from a certain table I have, but that condition is not being taking.

I've changed the <> operator to = but I still have the same result. It never goes to the Else part.

 Private Sub btn_Cambiar_Click()

    On Error Resume Next

    DoCmd.SetWarnings False

    Dim CambiarContraseña As String
    Dim coincidenContraseñas As Variant

    CurrentDb.Execute dbFailOnError

    If (DLookup("[Contraseña]", "tbl_Usuarios", "[Contraseña] <>'" & Me.txt_Contraseña.Value & "'")) Then

        MsgBox "¡La contraseña no coincide!!/"
        Me.txt_Contraseña.SetFocus

    Else
            CambiarContraseña = _
            "UPDATE tbl_Usuarios SET Contraseña = txt_Contraseña_Nueva.value WHERE ID_Usuario = txt_Usuario"

            DoCmd.RunSQL CambiarContraseña

            MsgBox ("Contraseña cambiada.")

            txt_Contraseña = Null
            txt_Contraseña_Nueva = Null
    End If    

End Sub

I expect that this if must go directly to the else part because the text I enter in the form is exactly the same text that I have in my table:

tbl_Usuarios

Upvotes: 1

Views: 54

Answers (2)

Vlado
Vlado

Reputation: 888

You can also do something like this

  If (Nz(DCount("[CID_Usario]", "tbl_Usuarios", "[Contraseña] <>'" & Me.txt_Contraseña.Value & "'"),0) > 0) Then ...

Upvotes: 1

HackSlash
HackSlash

Reputation: 5803

The result of the DLOOKUP will return a value if your pattern matches. If it is not found DLOOKUP should return a null. So in your If you want to look for that null.

Like this:

If IsNull(DLookup("[Contraseña]", "tbl_Usuarios", "[Contraseña]='" & Me.txt_Contraseña.Value & "'")) Then

Upvotes: 1

Related Questions