Solved My code inserts two identical records instead of one (1 Viewer)

5hadow

Member
Local time
Today, 07:47
Joined
Apr 26, 2021
Messages
89
When I click on OK button, it's supposed to add a record in my table based on the text boxes typed.
It works, but it makes two entries instead of just one.

Here's the code:

Code:
Private Sub cmdOk_Click()

    Dim Fail As Boolean: Fail = False
    
    ' Check to ensure the pertinate data has been intput
    If Not Len(txtLast.Value) > 0 Then
        txtLast.BackColor = RGB(255, 0, 0)
        Fail = True
    Else
        txtLast.BackThemeColorIndex = 1
    End If
    
    If Not Len(txtFirst.Value) > 0 Then
        txtFirst.BackColor = RGB(255, 0, 0)
        Fail = True
    Else
        txtFirst.BackThemeColorIndex = 1
    End If
    
    If Not Len(lstTrade.Value) > 0 Then
        lstTrade.BackColor = RGB(255, 0, 0)
        Fail = True
    Else
        lstTrade.BackThemeColorIndex = 1
    End If
    
    If Not Len(txtEmail.Value) > 0 Then
        txtEmail.BackColor = RGB(255, 0, 0)
        Fail = True
    Else
        txtEmail.BackThemeColorIndex = 1
    End If
    
    If Fail Then
        
        With CreateObject("WScript.Shell")
            Select Case .PopUp("Please fill in all the data in the red boxes before hitting Ok!", 2, "Information", 48)
                Case 1, -1
                    Exit Sub
            End Select
        End With
        
    End If
    
    On Error GoTo ErrorHandler
    
    ' Add the entry
    CurrentDb.Execute "INSERT INTO tblMember (fldLastName,fldFirstName,fldEmail,fldTradeID) VALUES('" & txtLast.Value & "','" & txtFirst.Value & "','" & txtEmail.Value & "','" & lstTrade.Value & "') ", dbFailOnError
    
    ' Exit the form
    DoCmd.Close acForm, Me.Name, acSaveYes
    
    ' Update
    Forms!frmMembers.DoRequery
        
    Exit Sub
    
ErrorHandler:
    If Err.Number <> 2046 Then
        MsgBox Err.Description, vbOKOnly, "Error #: " & Err.Number
        Exit Sub
    End If
    
End Sub
 

June7

AWF VIP
Local time
Today, 03:47
Joined
Mar 9, 2014
Messages
5,425
Are your textboxes bound to fields? If so then you are likely creating a record when data is typed in them. Then you create another record with the INSERT action. Why do you think INSERT is needed?
 

5hadow

Member
Local time
Today, 07:47
Joined
Apr 26, 2021
Messages
89
Are your textboxes bound to fields? If so then you are likely creating a record when data is typed in them. Then you create another record with the INSERT action. Why do you think INSERT is needed?
Oh man, yes. That the problem.

Thank you!
 

Users who are viewing this thread

Top Bottom