MessageBox if Recordset Update is Successful

Posted by Paolo Bernasconi on Stack Overflow See other posts from Stack Overflow or by Paolo Bernasconi
Published on 2012-12-02T16:45:10Z Indexed on 2012/12/02 17:04 UTC
Read the original article Hit count: 409

In Access 2007, I have a form to add a new contact to a table:

RecSet.AddNew
RecSet![Code_Personal] = Me.txtCodePersonal.Value
RecSet![FName] = Me.TxtFName.Value
RecSet![LName] = Me.txtLName.Value
RecSet![Tel Natel] = Me.txtNatTel.Value
RecSet![Tel Home] = Me.txtHomeTel.Value
RecSet![Email] = Me.txtEmail.Value
RecSet.Update

This has worked so far, and the contact has successfully been aded. But I'm having two problems:

  1. I want to display a messagebox to tell the user the contact was successfully added
  2. If the contact was not successfully added because
    1. A contact with this name already exists
    2. A different issue
      Then display a message box "Contact already exists" or "error occured" respectively.

My idea of doing this is:

If recSet.Update = true Then
MsgBox "Paolo Bernasconi was successfully added"
Else if RecSet![FName] & RecSet![LName] 'already exist in table
MsgBox "Contact already exists"
Else 
MsgBox "An unknown error occured"

I know this code is wrong, and obviously doesn't work, but it's just to give you an idea of what I'm trying to achieve. Thanks for all your help in advance.

© Stack Overflow or respective owner

Related posts about validation

Related posts about vba