Solved If ComboBox is Yes, TextBox must Not be blank (1 Viewer)

VzqCrs

Member
Local time
Today, 18:14
Joined
Nov 15, 2021
Messages
54
Hi All!

I want to add as part of a datacheck when the user clicks on Submit and Close. I have coding already there for other things, but how do I code for this?

i.e. If Me.bFinancial = -1 then Me.txtFinancial not null

I know that's not right, but I'm having trouble writing in the Not Null of Not Blank - can someone lead me the right way?

Thank you in advance!
 

bastanu

AWF VIP
Local time
Today, 15:14
Joined
Apr 13, 2010
Messages
1,402
You don't need the Yes\No field, you should just check the value of the text box"
Code:
If Isnull(Me.txtFinancial) Then
    MsgBox "Sorry, you need to add Financial"
End if
Cheers,
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:14
Joined
Oct 29, 2018
Messages
21,541
Maybe something like:

Code:
If Me.bFinancial AND IsNull(Me.txtFinancial) Then
 

VzqCrs

Member
Local time
Today, 18:14
Joined
Nov 15, 2021
Messages
54
bFinancial is a Yes/No combo box and is a required field that is defaulted to No, but if Yes is selected then txtFinancial needs something
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:14
Joined
Oct 29, 2018
Messages
21,541
bFinancial is a Yes/No combo box and is a required field that is defaulted to No, but if Yes is selected then txtFinancial needs something
Did you try what I suggested?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:14
Joined
Feb 19, 2002
Messages
43,484
I want to add as part of a datacheck when the user clicks on Submit and Close. I have coding already there for other things,
Your code is in the wrong events if it is in these two events. Validation code should almost always (there are exceptions) be in the FORM's BeforeUpdate event. This event ALWAYS runs when the record is dirty and it is the last event to run before the record is saved (or not saved) YOU have complete control over whether or not the record gets saved if your code runs from this event. Think of it as the flapper at the bottom of a funnel. If the flapper is open, the record gets saved. If it is closed, the save is prevented.

I can't tell with certainty what you are attempting to do but:
Code:
If Me.bFinancial = True  Then
    If Me.txtFinancial & "" = "" Then
        Msgbox "Financial must have a value.",vbOKOnly
        Me.txtFinancial.SetFocus
        Cancel = True
        Exit Sub
    End If
End If

Cancel = True is how you cancel the BeforeUpdate event and prevent Access from saving the record.
When you put your validation in other events, you get a lot of warning messages but the bad record usually still gets saved. Access form events are there for a reason. It is important to understand why you would use a particular event.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:14
Joined
Feb 19, 2002
Messages
43,484
Does the "like" mean that you heard me and are moving the code?
 

VzqCrs

Member
Local time
Today, 18:14
Joined
Nov 15, 2021
Messages
54
Does the "like" mean that you heard me and are moving the code?
Yes, I moved all the datachecks to the BeforeUpdate event and the AppendQueries etc to the AfterUpdate event and the Submit button will only save and requery the form. Thanks for the advice!
 

Users who are viewing this thread

Top Bottom