运行时错误15-键入不匹配的代码以更改MS Access数据库中的密码

问题描述

我在以下行中收到运行时错误15:

MyuserID = Me.txtfirstname.Value来自以下代码

Option Compare Database
Option Explicit

Private Sub cmdchange_Click()
    On Error Resume Next
    If Trim(Me.txtnewpass & "") <> Trim(Me.txtconfirmpass & "") Then
    MsgBox "Passwords do not match",vbExclamation + vbOKOnly,""
    Me.cmdchange.Enabled = False
    Else
    Me.cmdchange.Enabled = True
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("Select' From[User Registration Details] where [UserID]=" & MyuserID)
    If Not rs.EOF And Not rs.BOF Then
        rs.Edit
        rs("Password") = txtconfirmpass
        rs.Update
        rs.Close
        Set rs = nothing
        MsgBox "Your Password has been successfully changed",vbinformation,"Electporl"
        DoCmd.Close acForm,"frmnewpassword",acSaveNo
        DoCmd.OpenForm "frmlogin"
      End If
    End If

鉴于我将下面的代码放置在将用户带到更改密码表单的按钮上。

Private Sub cmdproceed_Click()
       If IsNull(Me.txtfirstname) Or Me.txtfirstname = "" Then
       Me.mand1.Visible = True
       Me.txtfirstname.SetFocus
    End If
      If IsNull(Me.txtemail) Or Me.txtemail = "" Then
      Me.mand2.Visible = True
      Me.txtemail.SetFocus
    End If
      Dim rs As Recordset

    Set rs = CurrentDb.OpenRecordset("User Registration Details",dbOpenSnapshot,dbReadOnly)
    rs.FindFirst ("Firstname='" & Nz(Me.txtfirstname,"") & "'")

    If rs.NoMatch = True Then
        Me.lbl1.Visible = True
        Me.txtfirstname.SetFocus
        Exit Sub
    End If

    If rs!Username <> Nz(Me.txtemail,"") Then
        Me.lbl2.Visible = True
        Me.txtemail.SetFocus
        Exit Sub
    End If
    'MyuserID is publicly declared as Long in a module
    MyuserID = Me.txtfirstname.Value
    DoCmd.OpenForm " frmnewpassword"
    DoCmd.Close acForm,Me.Name

End Sub

第二个代码被分配给该按钮,该按钮将用户重定向到表单,该表单将使他或她在验证其姓氏和电子邮件之后可以更改密码。

现在将第二个分配给该按钮,该按钮将帮助用户通过覆盖旧密码来更改密码。

解决方法

请在过程中传递UserID值。

在您的cmdproceed_Click()过程中,更新以下部分:

'MyuserID is publicly declared as Long in a module
MyuserID = rs("UserID")

在您的cmdchange_Click()过程中,更新以下行:

 Set rs = CurrentDb.OpenRecordset("Select * From [User Registration Details] where [UserID]=" & MyuserID)

从逻辑的角度来看,可以让其他人具有相同的firstname,因此仅对firstname进行过滤将在应用程序生命周期的后期引入意外行为。

如果您有两个或多个名字为“ Joshua”的用户,则您的代码将始终选择该名字的第一个用户。您需要更新此逻辑以选择唯一用户。