Solved Code not working? (1 Viewer)

Jolland26

New member
Local time
Today, 11:23
Joined
Sep 14, 2020
Messages
22
Hi guys,
I'm quite new to Access and coding in general and have been asked to create a database at work. I've been using the vast resources of the internet to help and have learned a lot in the month or so since I started. However, I have encountered a problem that I can't seem to find a solution for anywhere. I have a form for requesting an EQN (Engineering change essentially). Within the form I have a button that when clicked, saves the form and sends an email to the relevant person to grant the request. It also looks at the textboxes, which are bound to the fields in my table, to check that there is text in them and a warning message pops up if not. The code on this button works fine, but I also have a button on this form which opens a separate resubmission form for if an EQN request is rejected. I have literally copy and pasted the code from one button to the other, changed the names of the form etc and when I click the button on the resubmission form, nothing happens at all. It's as if there's no code behind the button whatsoever. I also have a form for the granting of these requests with the same button, I copy and pasted the code to that one and it worked fine. I'm just stumped as to why it's only on one particular form it doesn't work. I'll post the code below, and any help would be greatly appreciated.

TL;DR two buttons on separate forms with the exact same code, one works, the other doesn't.

Code:

Private Sub cmdSaveEmail_Click()

If IsNull(Me.Value_Stream.Value) Then
MsgBox "Please select a Value Stream", vbOKOnly, "Warning"
Me.Value_Stream.Undo
If IsNull(Me.Part_Number.Value) Then
MsgBox "You must enter a value into the part number field", vbOKOnly, "Warning"
Me.Part_Number.Undo
If IsNull(Me.Detailed_Description.Value) Then
MsgBox "You must enter the description", vbOKOnly, "Warning"
Me.Detailed_Description.Undo
If IsNull(Me.Raised_By.Value) Then
MsgBox "You must enter who is raising this request.", vbOKOnly, "Warning"
Me.Raised_By.Undo

Else

SaveRecord

Dim f As Form
Set f = Forms![frmEQNResubmit]
f.Filter = "ID=" & f!ID
f.FilterOn = True
DoCmd.SendObject acSendForm, f.Name, acFormatPDF, "EMAIL", , , "EQN Resubmit", "See Attached", True, ""
f.FilterOn = False
Set f = Nothing
End If
End If
End If
End If

End Sub
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:23
Joined
Aug 11, 2003
Messages
11,696
I am guessing that the check for partnumber only is checked when the first check for Value_Stream fails.
Proper indentation shows this problem
Code:
Private Sub cmdSaveEmail_Click()
  
    If IsNull(Me.Value_Stream.Value) Then
        MsgBox "Please select a Value Stream", vbOKOnly, "Warning"
        Me.Value_Stream.Undo
        If IsNull(Me.Part_Number.Value) Then
            MsgBox "You must enter a value into the part number field", vbOKOnly, "Warning"
            Me.Part_Number.Undo
            If IsNull(Me.Detailed_Description.Value) Then
                MsgBox "You must enter the description", vbOKOnly, "Warning"
                Me.Detailed_Description.Undo
                If IsNull(Me.Raised_By.Value) Then
                    MsgBox "You must enter who is raising this request.", vbOKOnly, "Warning"
                    Me.Raised_By.Undo
                Else
                    SaveRecord
                    Dim f As Form
                    Set f = Forms![frmEQNResubmit]
                    f.Filter = "ID=" & f!ID
                    f.FilterOn = True
                    DoCmd.SendObject acSendForm, f.Name, acFormatPDF, "EMAIL", , , "EQN Resubmit", "See Attached", True, ""
                    f.FilterOn = False
                    Set f = Nothing
                End If
            End If
        End If
    End If
End Sub

Instead you want each check to be done seperately in one big if and only save when all checks are passed. Something along these lines:
Code:
Private Sub cmdSaveEmail_Click()
  
    If IsNull(Me.Value_Stream.Value) Then
        MsgBox "Please select a Value Stream", vbOKOnly, "Warning"
        Me.Value_Stream.Undo
    ElseIf IsNull(Me.Part_Number.Value) Then
        MsgBox "You must enter a value into the part number field", vbOKOnly, "Warning"
        Me.Part_Number.Undo
    ElseIf IsNull(Me.Detailed_Description.Value) Then
        MsgBox "You must enter the description", vbOKOnly, "Warning"
        Me.Detailed_Description.Undo
    ElseIf IsNull(Me.Raised_By.Value) Then
        MsgBox "You must enter who is raising this request.", vbOKOnly, "Warning"
        Me.Raised_By.Undo
    Else
        SaveRecord
        Dim f As Form
        Set f = Forms![frmEQNResubmit]
        f.Filter = "ID=" & f!ID
        f.FilterOn = True
        DoCmd.SendObject acSendForm, f.Name, acFormatPDF, "EMAIL", , , "EQN Resubmit", "See Attached", True, ""
        f.FilterOn = False
        Set f = Nothing
    End If
End Sub
 

Jolland26

New member
Local time
Today, 11:23
Joined
Sep 14, 2020
Messages
22
I have just tried that and lo and behold it works! Very annoying for me because I did put it all in one big if statement to start with, but I must have made a mistake somewhere as it threw up an error so I split the ifs up just to see if it would work. Many thanks for your help namliaM!
 

namliam

The Mailman - AWF VIP
Local time
Today, 12:23
Joined
Aug 11, 2003
Messages
11,696
Proper indentation of your IF/else/endif and other places really helps to keep track of where you have what.
 

Users who are viewing this thread

Top Bottom