1
I have the VBA code below that saves a new release. When Close the Form, even if I have entered any name, it is not saved. But I noticed that Access automatically generates a new primary key, ie skips a primary key that will no longer be used. Example: if I had until primary key 9, when closing the form Access will skip primary key 10, and when opening the form again the next primary key will be 11. It’s normal or it’s avoidable?
Option Compare Database
Private Sub Form_Load()
    DoCmd.GoToRecord , , acNewRec
    Me.NomeGrupo.SetFocus
End Sub
Private Sub btSalvar_Click()
    Me.NomeGrupo.SetFocus
    If Me.NomeGrupo.Text = Empty Then
        MsgBox "Operação cancelada!" & vbNewLine & vbNewLine & _
        "O campo Nome do grupo está vazio!", vbInformation, " CADASTRO DE GRUPOS"
        Exit Sub
    End If
    If (Not IsNull(DLookup("[NomeGrupo]", "tabGrupos", "[NomeGrupo] ='" & Me!NomeGrupo & "'"))) Then
        MsgBox "Operação cancelada!" & vbNewLine & vbNewLine & _
        "O grupo " & Me.NomeGrupo.Text & " já está cadastrado no sistema!", vbInformation, "CADASTRO DE GRUPO"
        Cancel = True 'cancela o evento.
        Me!NomeGrupo.Undo 'desfaz a digitação.
    Else
        DoCmd.GoToRecord , , acNewRec
        MsgBox "Cadastro realizado com sucesso!", vbInformation, " CADASTRO DE GRUPOS"
    End If
End Sub
Private Sub btFechar_Click()
    Me.Undo
    DoCmd.Close acForm, "FormGrupos"
End Sub
Me, newbie, giving you trouble. Thanks for the hkotsubo tip!
– Roberto Paixão