문제

I'm working on running an append query (to change someones login password) in access, currently it checks to make sure the password matches and that they knew the original password. However, I run into a problem where if the update query: DoCmd.RunSQL "UPDATE Credentials SET Credentials.[Password] = [Forms]![LoginSubmit]![DesiredPassword] WHERE (((Credentials.Password)=[Forms]![LoginSubmit]![CurrentPassword]) AND ((Credentials.[User ID])=[Forms]![LoginSubmit]![User ID]));" updates 0 rows (meaning that the conditions were not met) it closes (I have a docmd.close following). If it does update their password it does the same, so in the end you don't know if it was successful until you try your new password.

I would like to know when it updates 0 rows so I can shoot up a MsgBox telling them it didn't work, end the sub and have them try again. If it updates 1 row I would have a MsgBox say "Success".

I know the VBA to the above except for the part that returns a value for the amount of updated rows in that query. Could someone help me out?

Thanks in advance.

도움이 되었습니까?

해결책

Execute your UPDATE statement from a DAO.Database object variable. Afterward you can base your user notice on the variable's RecordsAffected property.

Here is a simple example, verified with Access 2007.

Dim db As DAO.Database
Dim strUpdate As String
strUpdate = "UPDATE tblFoo" & vbCrLf & _
    "SET some_text = 'bar'" & vbCrLf & _
    "WHERE id = 7;"
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailOnError
MsgBox db.RecordsAffected & " records affected"
Set db = Nothing
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top