My Access database has one big table. Each record holds data about a member of a club. Some fields are connected to small lookup tables. Thus it is basically a straightforward design. The main table does not have any primary key. Instead the combination of first name and last name is unique. I use an input form that is switched into Add Mode by the user. The user enters the data and presses the Add Record key. At that moment the record is added to the table. The code showing all this is below. The problem is that the system adds the record twice. Please help.
Code to switch to add mode:
Code for adding record:
Code to switch to add mode:
Code:
Private Sub SetAddModeBt_Click()
If G_ReadOnly = "true" Then GoTo Stay_ReadOnly33:
If Me.Mode <> "Add Mode" Then
Me.AllowEdits = False
Me.AllowAdditions = True
Me.AllowDeletions = False
Me.DataEntry = True
Me.NOM.Locked = False
Me.PRENOM.Locked = False
Me.Mode = "Add Mode"
End If
Stay_ReadOnly33:
End Sub
Code for adding record:
Code:
Private Sub AddNewBt_Click()
On Error GoTo Err_AddNewBt_Click
Dim UF_Rec1, UF_Rec2, UF_Rec3 As String
UF_Rec1 = Me.NOM
UF_Rec2 = Me.PRENOM
If Me.Mode = "Read Only" Or Me.Mode = "Edit Mode" Then
MsgBox "To add a new record change to Add Mode"
Exit Sub
End If
Dim rstarget As Recordset
Set rstarget = CurrentDb.OpenRecordset("MembersTbl")
rstarget.FindFirst "[NOM] = '" & UF_Rec1 & "'" & " And " & _
"[PRENOM] = '" & UF_Rec2 & "'"
If rstarget.NoMatch = False Then
MsgBox "Member exists - nothing added"
Set rstarget = Nothing
Exit Sub
End If
' MsgBox "adding record NOM = " & Me.NOM
With rstarget
.AddNew
.Fields("NOM").Value = Me.NOM.Value
.Fields("PRENOM").Value = Me.PRENOM.Value
.Fields("STATUT").Value = Me.STATUT.Value
.Fields("NAT").Value = Me.NAT.Value
.Fields("LANG").Value = Me.LANG.Value
.Fields("ENTRORG").Value = Me.ENTRORG.Value
.Fields("DEPORG").Value = Me.DEPORG.Value
.Fields("DNAISS").Value = Me.DNAISS.Value
.Fields("DateCreated").Value = Now()
.Fields("LastUpdate").Value = Now()
MsgBox "adding record NOM = " & Me.NOM
.Update
.Close
End With
Set rstarget = Nothing
MsgBox "Record added to database", , GC_Title
DoCmd.Close
Exit_AddNewBt_Click:
Exit Sub
Err_AddNewBt_Click:
MsgBox Err.Description
Resume Exit_AddNewBt_Click
End Sub