Button makes two records instead of one on click (1 Viewer)

5hadow

Member
Local time
Today, 11:51
Joined
Apr 26, 2021
Messages
89
I've got two forms, one is for list of users "frmMembers" and another to get new users via outlook application code "frmNewMember".
Once I click on "OK" button "cmdOk" it runs the following 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 & "') "
   
    ' Exit the form
    DoCmd.Close acForm, Me.Name
    ' 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

My problem is that this creates two records in my "tblMember". One which is expected with all information, and another record with empty fields EXCEPT for E-Mail address.

I cannot isolate this issue. Can someone figure it out?

Edit: If it helps, it seams it makes bad record first, then the good one, in that order.
 

5hadow

Member
Local time
Today, 11:51
Joined
Apr 26, 2021
Messages
89
Ok, it looks like first part of code was the problem:
Code:
    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

I commented it out and it worked fine.
Still don't know where, but in there somewhere was the bug.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:51
Joined
Oct 29, 2018
Messages
21,360
Hi. Just guessing here, but if your form is bound to the same table, then the form might be creating one record and the SQL is creating another.
 

Users who are viewing this thread

Top Bottom