I have a form and data is already imported from a previous step, but at this point one can check a box and all the information that is regarding to that check box will be added to another screen. For example, Tom, Katie, and Sonya all bought a red apple. When I check all the boxes for red apples it will send it to another form that will enable me to print out all the customers who bought a red apple. But sometimes when people are clicking a box they will click too many boxes. For example, they clicked all the red apple boxes but then clicked an orange as well. So when I go to print the customer list it will have that one person who bought an orange. Is there a way where I can have VBA check to see if all the items that are checked have the same item purchased? It would run the code and then it would say in a message box "Not all items are the same product type. Are you sure you would like to continue?". I can do a message box but I do not know how to make the message box pop up when an item has been selected incorrectly.
Access Form Question about record verification
Collapse
X
-
I think that I have a good understanding as to what the OP is requesting. Here is how I interpret it:- Set the Tag Property of ALL Check Boxes to represent the Category to which they are assigned, namely: Apples Check Boxes should have a Tag Property set to Apples, Grapes Check Boxes should have a Tag Property set to Grapes, etc.
- If a Check Box does not belong to any Category, set its Tap Property to N/A.
- Loop through every Control on the Form.
- If the Control is a Selected Check Box with a Tag Property of anything but N/A, set the Tag of the 1st Check Box to a Variable. All other valid Check Boxes should now equate to this String Value, namely: Apples, Grapes, Peaches, etc.
- If there is one Check Box that does not match such as: 3 Apples and 1 Grape, notify the User and exit the Routine.
- If all is well, confirmation will be via a Comma Delimited String with all the same Values, such as: Apples,Apples,A pples.
- Hopefully this is what you are looking for, any questions feel free to ask.
Code:Dim ctl As Control Dim strTag As String Dim intCtr As Integer Dim strBuild As String For Each ctl In Me.Controls With ctl If .ControlType = acCheckBox Then 'Is the Control a Check Box If .Value = True And .Tag <> "N/A" Then 'Is it Selected and does it have a Valid Tag? intCtr = intCtr + 1 'Check Box Counter If intCtr = 1 Then strTag = .Tag 'Retrieve Tag of 1st Check Box If .Tag <> strTag Then 'Tags are not the same MsgBox "All Items selected must be of the same Type", _ vbExclamation, "Improper Selection" strBuild = "" Exit For 'Get outta here! Else strBuild = strBuild & .Tag & "," 'Items that have been 'cleared' End If End If End If End With Next 'Confirmation on Items to make sure they are all the same If strBuild <> "" Then MsgBox Left$(strBuild, Len(strBuild) - 1)
Comment
Comment