UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Validate 1 field based on another field in a form    
 
   
albertnx
post Oct 29 2009, 11:56 AM
Post #1

UtterAccess Addict
Posts: 122



Hi
I have a form where I need to validate a field based on the response of another field.
From reading all the posts it looks likethis needs to be done on the Before Update action of the Form.
is this right?

Can anyone help me with this, I am new to VB and can use as much direction as possible on this one!!

Do I need to use Cases?

The first field is a combo box [AgDirectCB] with 2 choices
Agency
Direct

The 2nd Field is a combo box [AgencyCB] with a list of Agencies

My logic needs to be as follows

AgDirectCB can not be null
If AgDirectCB = Agency
Then AgencyCB must not be null

If AgDirectCB = Direct
Then AgencyCB must be null
Go to the top of the page
 
+
RuralGuy
post Oct 29 2009, 12:11 PM
Post #2

UtterAccess VIP
Posts: 2,428
From: @ 8300' in the Colorado Rocky Mountains



If I were doing it, I would keep the control disabled until the first control has been properly selected. Then for complete validation I would use the BeforeUpdate event of *both* the control and the form.
Go to the top of the page
 
+
pbaldy
post Oct 29 2009, 12:33 PM
Post #3

UtterAccess VIP
Posts: 5,052
From: Nevada, USA



Since I was PM'd I'll jump in. Disabling the second control isn't a bad idea. I would make the first field required in the table, so Access would handle the validation for that. Then code in the before update event of the form might look like:

CODE
If Me.AgDirectCB = "Agency" Then
  If IsNull(Me.AgencyCB) Then
    Msgbox "Must select agency"
    Cancel = True
  End If
Else
  If Not IsNull(AgencyCB) Then
    Msgbox "Must NOT select agency"
    Cancel = True
  End If
End If


Hey RG, hope you're seeing that snow that moved through here yesterday!
Go to the top of the page
 
+
RuralGuy
post Oct 29 2009, 12:41 PM
Post #4

UtterAccess VIP
Posts: 2,428
From: @ 8300' in the Colorado Rocky Mountains



Indeed Paul. We've got 3-4 inches sitting on the ground right now.
Go to the top of the page
 
+
pbaldy
post Oct 29 2009, 12:52 PM
Post #5

UtterAccess VIP
Posts: 5,052
From: Nevada, USA



We just got dusted here, though they got a few inches up at Lake Tahoe. I guess it's almost time to hang up the golf clubs and take down the skis!
Go to the top of the page
 
+
RuralGuy
post Oct 29 2009, 12:59 PM
Post #6

UtterAccess VIP
Posts: 2,428
From: @ 8300' in the Colorado Rocky Mountains



I got the tractor ready for pushing snow a couple of week ago. I'm ready, let 'er rip.
Go to the top of the page
 
+
albertnx
post Oct 29 2009, 10:05 PM
Post #7

UtterAccess Addict
Posts: 122



enough about the snow,,, what about my needs!!!

Actually can't wait to hit Tahoe, Mammoth opened last week.

This worked great,
only problem is,,
If i select Direct and select an agency, then when i close the form using the X at the top right,, i get my nice warning message and then access message saying it will not save the record,,,
How can i get rid of that?

The other issue is if i select the Close button that I added, I get the warning with an OK button but the form closes anyway and does not save,, doesn't allow the user to make the change

I tried adding the same code in the close button on click event and also added a save before the close, but now i get the warning twice and still the form closes

Can you get your minds off the slopes and back on me for just a minute please!!!

Private Sub Close_Click()
On Error GoTo Err_Close_Click

If Me.AgDirectCB = "Agency" Then
If IsNull(Me.AgencyCB) Then
MsgBox "Please Select an Agency"
Cancel = True
End If

Else
If Not IsNull(AgencyCB) Then
MsgBox "You have selected Direct, Either change to Agency or remove the Agency"
Cancel = True
End If
End If
DoCmd.save
DoCmd.close
DoCmd.RunCommand (acCmdRefresh)


Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub
Go to the top of the page
 
+
RuralGuy
post Oct 31 2009, 06:45 AM
Post #8

UtterAccess VIP
Posts: 2,428
From: @ 8300' in the Colorado Rocky Mountains



For the record, if the form is "Dirty" the FORM BeforeUpdate event *will* run regardless of how the form is closed.
Go to the top of the page
 
+
missinglinq
post Oct 31 2009, 09:16 AM
Post #9

UtterAccess Ruler
Posts: 2,657



The validation code you have in the Close_Click() event should be in the Form_BeforeUpdate which, as Allan said, fires anytime the form goes from Dirty to Not Dirty, i.e. is saved, regardless of how the record is saved or hoe the form is closed.

Also note that the line you have

DoCmd.Save


does not save a record, it saves design changes to the form! The code to save a record would be

DoCmd.RunCommand acCmdSaveRecord

but in point of fact should not be in the code at all. This will be handled automatically by Access unless you prevent it, as the first part of your validation code should.

As a last note,

Cancel = True

can only be used in events where it appears in the sub header in the parens following the sub name, such as

Form_BeforeUpdate(Cancel As Integer)

and no, you cannot simply add it to the header. If Access doesn't put it there it's not available for that event.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 21st May 2013 - 09:45 AM