Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top