Solved Combination of two commands (1 Viewer)

tihmir

Registered User.
Local time
Today, 10:19
Joined
May 1, 2018
Messages
257
Hi, all. I need some help, please!
I need to combine two messages with the corresponding commands.
First I want to execute this command:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 If Not Nz(Me.cbo_PSD, "") = "" And Nz(Me.numbeрOfPproducts, "") = "" Then
        MsgBox "Please, enter the number of products!", vbInformation, "Attention !"
        Cancel = True
        Exit Sub
    End If

The second command I want to combine is:
Code:
Dim answer As Integer

    answer = MsgBox("Do you want to Continue?", vbQuestion + vbYesNo)
    If answer = vbYes Then
        'I want enter the number of products into txt_NumbersOfProduct
    Else
        'I want to go beck and exit the form.
        Me.Undo
        DoCmd.Close acForm, "fm_Inspection"
    End If
    End Sub
How should I combine these commands to work?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:19
Joined
Oct 29, 2018
Messages
21,358
Hi. Looks like the first one is tied to the form's update event, so it's probably triggered when the user wants to save the record. What event is the second code located? By combining the two, what are you hoping to accomplish? For example, are you trying to get rid of a Save button?
 

tihmir

Registered User.
Local time
Today, 10:19
Joined
May 1, 2018
Messages
257
Hi. Looks like the first one is tied to the form's update event, so it's probably triggered when the user wants to save the record. What event is the second code located? By combining the two, what are you hoping to accomplish? For example, are you trying to get rid of a Save button?
I want to have a second option that allows the user if he made the wrong choice to be able to go back and not save the current record.
Because if I only have the first part of the code:
Code:
 If Not Nz(Me.cbo_PSD, "") = "" And Nz(Me.numbeрOfPproducts, "") = "" Then
        MsgBox "Please, enter the number of products!", vbInformation, "Attention !"
        Cancel = True
        Exit Sub
    End If
Because if I only have the first part of the code I can do nothing just enter a value in the field. And so there is no way to go back without saving the record, if I made the wrong entry
 
Last edited:

Cronk

Registered User.
Local time
Tomorrow, 04:19
Joined
Jul 4, 2013
Messages
2,770
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not Nz(Me.cbo_PSD, "") = "" And Nz(Me.numbeрOfPproducts, "") = "" Then
        answer = MsgBox("You have not entered the number of products" & vbcrlf & vbcrlf & "Do you want to Continue?", vbQuestion + vbYesNo, "Missing Data")
        If answer = vbYes Then
            me.numbeрOfPproducts.setfocus               
        Else
            Me.Undo
            DoCmd.Close acForm, "fm_Inspection"
        End If
   End If
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:19
Joined
Oct 29, 2018
Messages
21,358
I want to have a second option that allows the user if he made the wrong choice to be able to go back and not save the current record.
Because if I only have the first part of the code:
Code:
 If Not Nz(Me.cbo_PSD, "") = "" And Nz(Me.numbeрOfPproducts, "") = "" Then
        MsgBox "Please, enter the number of products!", vbInformation, "Attention !"
        Cancel = True
        Exit Sub
    End If
Because if I only have the first part of the code I can do nothing just enter a value in the field. And so there is no way to go back without saving the record, if I made the wrong entry
In that case, I think, you can just eliminate the second code. You don't need it.
 

tihmir

Registered User.
Local time
Today, 10:19
Joined
May 1, 2018
Messages
257
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not Nz(Me.cbo_PSD, "") = "" And Nz(Me.numbeрOfPproducts, "") = "" Then
        answer = MsgBox("You have not entered the number of products" & vbcrlf & vbcrlf & "Do you want to Continue?", vbQuestion + vbYesNo, "Missing Data")
        If answer = vbYes Then
            me.numbeрOfPproducts.setfocus              
        Else
            Me.Undo
            DoCmd.Close acForm, "fm_Inspection"
        End If
   End If
End Sub
Cronk, thanks for the help and advice you gave me.
 

tihmir

Registered User.
Local time
Today, 10:19
Joined
May 1, 2018
Messages
257
it Will still Save the record Without the "number of products" when you select "Yes"
because you did not Cancel.
arnelgp, you are correct. I added in the code: Cancel = True
Code:
        If answer = vbYes Then
        Cancel = True
        me.numbeрOfPproducts.setfocus
I need to do one more thing whit cbo_PSD (which is located on subform "fm_PSD") and cbo_TypeOfProducts (which is located on sub sub form "fm_Products). My Main form is called "fm_Inspection.
Code:
If Me.cbo_PSD = "Cosmetic products" Then 'cbo_PSD is located on subform "fm_PSD"

    if cbo_TypeOfProducts isNull 'cbo_TypeOfProducts is located on sub sub form "fm_Products"

    MsgBox "Plase enter type of product!", vbInformation, "Attention!"

        Cancel = True

     Exit Sub
    End If
How can I oblige it to be filled out my cbo_TypeOfProducts (on sub sub form "fm_Products), if my cbo_PSD (which is located on subform "fm_PSD") is equals to "Cosmetic products"?
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:19
Joined
May 7, 2009
Messages
19,169
add code to Each subform's Exit event:
Code:
Private Sub frm_Products_Exit(Cancel As Integer)
If Me![frm_PSD]![cbo_psd] = "Cosmetic products" Then
    If Me![frm_Products]![cbo_TypeOfProducts].ListIndex < 0 Then
        MsgBox "You need to select Product type from Product subform"
        Me![frm_Products].SetFocus
        Me![frm_Products]![cbo_TypeOfProducts].SetFocus
        Cancel = True
    End If
End If
End Sub

Private Sub frm_PSD_Exit(Cancel As Integer)
If Me![frm_PSD]![cbo_psd] = "Cosmetic products" Then
    If Me![frm_Products]![cbo_TypeOfProducts].ListIndex < 0 Then
        MsgBox "You need to select Product type from Product subform"
        Me![frm_Products].SetFocus
        Me![frm_Products]![cbo_TypeOfProducts].SetFocus
    End If
End If
End Sub
 

tihmir

Registered User.
Local time
Today, 10:19
Joined
May 1, 2018
Messages
257
add code to Each subform's Exit event:
Code:
Private Sub frm_Products_Exit(Cancel As Integer)
If Me![frm_PSD]![cbo_psd] = "Cosmetic products" Then
    If Me![frm_Products]![cbo_TypeOfProducts].ListIndex < 0 Then
        MsgBox "You need to select Product type from Product subform"
        Me![frm_Products].SetFocus
        Me![frm_Products]![cbo_TypeOfProducts].SetFocus
        Cancel = True
    End If
End If
End Sub

Private Sub frm_PSD_Exit(Cancel As Integer)
If Me![frm_PSD]![cbo_psd] = "Cosmetic products" Then
    If Me![frm_Products]![cbo_TypeOfProducts].ListIndex < 0 Then
        MsgBox "You need to select Product type from Product subform"
        Me![frm_Products].SetFocus
        Me![frm_Products]![cbo_TypeOfProducts].SetFocus
    End If
End If
End Sub
Thanks, arnelgp.
I added the code you wrote to me, fixed my typos and now the code works!
 
Last edited:

Users who are viewing this thread

Top Bottom