bound form does not add records

On my app I have a pop-up form whose purpose is to add a record to a table after some calculations.
In the form, there is a button for that and the code for the button is:

Private Sub btn_sairNovoExame_Click()

    Dim dDataExame As Date
    Dim nIdPaciente, _
        nIdExame As Integer
    Dim strMotivoExame As String
    
    
'On Error GoTo errorTrap

    Call logMe("btn_sairNovoExame_Click", "start")
    
    Me.frm_IDpaciente = Nz(Forms![_intData].frm_IDpaciente, 0)
    nIdPaciente = Me.frm_IDpaciente
    nIdExame = Nz(Me.frm_idExame, 0)
    If IsNull(Me.frm_MotivoExame) Then
        MsgBox "Motivo de Exame não preenchido"
    Else
        strMotivoExame = Me.frm_MotivoExame
    End If
    If IsNull(Me.frm_dataExame) Then
        MsgBox "Data de Exame não preenchida"
    Else
        dDataExame = Me.frm_dataExame
    End If
    Me.frm_idExame = Forms![_intData].frm_exameRecCount
    Forms![_intData].frm_exameSalvo = True
    DoCmd.GoToRecord , , acNewRec
    Me.frm_dataExame = dDataExame
    Me.frm_idExame = nIdExame
    Me.frm_IDpaciente = nIdPaciente
    Me.frm_MotivoExame = strMotivoExame
    Forms("frmPaciente").frmExameLista.Requery
    Call logMe("btn_sairNovoExame_Click", "end")
    DoCmd.Close acForm, "frmExameNovo"
    Exit Sub
    
errorTrap:
    MsgBox "[btn_sairNovoExame_Click]." & "[" & Err.Number & "].[" & Err.Description & "]"
    Call logMe("btn_sairNovoExame_Click", "errorTrap:" & "[btn_sairNovoExame_Click]." & "[" & Err.Number & "].[" & Err.Description & "]")
    Resume Next
    
End Sub

Open in new window


currently, the only other code in the form is the Load Event:


Private Sub Form_Load()
  
    Dim nIdExame As Integer
      
'On Error GoTo errorTrap

    Call logMe("Form Load - ExameNovo", "start")
          
    Me.frm_IDpaciente = Forms![frmPaciente].frm_IDpaciente
    Me.frm_nomePaciente = Forms![frmPaciente].frm_nome
    nIdExame = Nz(DMax("idExame", "tblExame"), 20000) + 1
    Forms![_intData].frm_exameRecCount = nIdExame
    Me.frm_idExame = nIdExame
    Me.frm_dataExame = Now()
    Me.frm_MotivoExame = Null
    Me.frm_nomePaciente = Nz(Forms![frmPaciente].Nome, "")
    Exit Sub
    
errorTrap:
    MsgBox "[Form_Load]." & "[" & Err.Number & "].[" & Err.Description & "]"
    Call logMe("Form Load - ExameNovo", "errorTrap:" & "[Form_Load]." & "[" & Err.Number & "].[" & Err.Description & "]")
    Resume Next

End Sub

Open in new window





The form goal is to add a record, but it is always replacing the sole record....
can anyone help? why is not adding records if I am using " DoCmd.GoToRecord , , acNewRec"?
João serras-pereiraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
As a general rule, I try to avoid using docmd when possible. Docmd usually works on the active object, and that might not be what you expect it to be.
You could add the record, by using a recordset instead, or by using "plain" SQL.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Set a breakpoint on the acNewRec line, and verify that you're hitting that line. When you do, walk through the code to ensure that you're not erroring out somewhere.

You could also try explicitly saving the record just before DoCmd.Close line:

If Me.Dirty Then Me.Dirty = False

Also be sure you don't have the AllowAdditions value of the form set to False
0
PatHartmanCommented:
The form goal is to add a record, but it is always replacing the sole record....
can anyone help? why is not adding records if I am using " DoCmd.GoToRecord , , acNewRec"?
Use the acAdd argument in the OpenForm method.  That will open the form to add mode.

Question?  If you are populating the fields via code, why open a form at all?  You should simply run an append query or use the DAO .addNew method.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
why open a form at all?
That crossed my mind as well, but I assume you have some other reason for opening the form? Perhaps the user can modify the data, or something of that nature?
0
João serras-pereiraAuthor Commented:
Hi! Thanks a çlot!
Replaced the Docmd by rs.addnew and it is working!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.