Search code examples
vbams-access

I can't get DLookup to recognize the value in my Combobox


I have an AdminPage where I can change User [Password] and I want to select the User using a Combobox, it works fine with a Textbox, however the value keeps returning Null with the Combobox.

Private Sub ValidateBtn_Click()

On Error Resume Next
    
        'Check if the UserNameCombo matches [UserName] in the UserT table
        If IsNull(DLookup("UserID", "UserT", "UserName = '" & Me.UserNameCombo.Value & "'")) Then
        'If IsNull(DLookup("UserID", "UserT", "UserName = '" & Me.txtCombo & "'")) Then
        MsgBox "UserName not found", vbCritical, "Denied"
        Me.UserNameCombo.SetFocus
        'Me.txtCombo.SetFocus
        
            'Check if the txtNewPassword field is empty
            ElseIf IsNull(Me.txtNewPassword) Or Me.txtNewPassword = "" Then
            MsgBox "Please input your new Password", vbInformation, "New password Required"
            Me.txtNewPassword.SetFocus
   
            'Check if the txtconfirmPassword field is empty
            ElseIf IsNull(Me.txtConfirmPassword) Or Me.txtConfirmPassword = "" Then
            MsgBox "Please confirm your new password", vbInformation, "Confirm new Password Required"
            Me.txtConfirmPassword.SetFocus
    
            'Verify if the txtNewPassword and txtConfirmPassword are the same
            ElseIf Me.txtNewPassword <> Me.txtConfirmPassword Then
            MsgBox "Your new and confirm Passwords do not match", vbCritical, "Denied"
    
            'Clear fields
            Me.UserNameCombo = ""
            'Me.txtCombo = ""
            Me.txtNewPassword = ""
            Me.txtConfirmPassword = ""
            Me.UserNameCombo.SetFocus
            'Me.txtCombo.SetFocus
    
            Else
     
                'Run Query to update the [Password] in UserT table
                DoCmd.OpenQuery "UpdatePasswordAdminQ"
                MsgBox "Password succesfully changed", vbCritical, "Congratulations"
    
                'Clear fields
                Me.txtNewPassword = ""
                Me.txtConfirmPassword = ""
                Me.txtNewPassword.SetFocus
    
            End If
    
End Sub

Solution

  • Most likely, your combobox is bound to UserId, not UserName. If so, use:

    If IsNull(DLookup("UserID", "UserT", "UserID = " & Me.UserNameCombo.Value & "")) Then