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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Anders Ebro (Microsoft MVP)Connect With a Mentor 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
 
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
 
PatHartmanConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.