VBA for Form Validation (1 Viewer)

freidaf

Registered User.
Local time
Yesterday, 23:05
Joined
Aug 13, 2012
Messages
44
Hello,
I have a form which includes fields for a Machine Name (cboMachID), Activity ID (cboActID) and Total Items Processed (TtlItems).
I need to write a vba statement for the forms before update event that checks the following:
If Activity ID = “P” and Machine ID is not equal to (“F-123-D” or “F-45-D” or “ABC-D” or “EFGH-D”) and TotalItems = 0
Then display a message that you must enter TotalItems.
Thank you for your help.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:05
Joined
Aug 30, 2003
Messages
36,123
This should get you started:

http://www.baldyweb.com/BeforeUpdate.htm

You'll have to repeat the machineID field and use AND

Machine ID <> “F-123-D” AND Machine ID <> “F-45-D”

Unless VBA supports IN(), which I can't recall offhand.
 

freidaf

Registered User.
Local time
Yesterday, 23:05
Joined
Aug 13, 2012
Messages
44
Thank you I appreciate your help. So something like this:

If cboActID = "P And cboMachID <>F-123-D And cboMachID <>F-45-D And cboMachID <>ABC-D And TtlItems = 0 Then
MsgBox "You must enter total items"
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:05
Joined
Aug 30, 2003
Messages
36,123
Yes, with quotes around the text values. Personally I like to disambiguate by referring to controls with "Me", but it should work as is. In other words:

Me.cboActID
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 23:05
Joined
Aug 30, 2003
Messages
36,123
Happy to help!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:05
Joined
Feb 19, 2002
Messages
43,213
If Activity ID = “P” and Machine ID is not equal to (“F-123-D” or “F-45-D” or “ABC-D” or “EFGH-D”) and TotalItems = 0
I would suggest a schema change. Rather than having to hard code a list of IDs that will likely change, why not add a new column to the table that identifies the "special" IDs. That way it won't matter if you have to add more. You're just checking another column for True or "whatever".
 

freidaf

Registered User.
Local time
Yesterday, 23:05
Joined
Aug 13, 2012
Messages
44
Thank you Pat, that's an excellent suggestion because these Machine names are likely to change. Some will be added and others will be deleted over time.

It's nice to know more than one way of solving my problem. Your help is very much appreciated!
 

Users who are viewing this thread

Top Bottom