why does this code not work? (1 Viewer)

Jon123

Registered User.
Local time
Today, 00:04
Joined
Aug 29, 2003
Messages
668
If (Me.[Part1b]) = (Me.[Part2b]) Or (Me.Part3b) Or (Me.Part4b) Or (Me.Part5b) Or (Me.Part6b) Or (Me.Part7b) Or (Me.Part8b) Or (Me.Part9b) Or (Me.Part10b) Or (Me.Part11b) Or (Me.Part12b) Or (Me.Part13b) Or (Me.Part14) Or (Me.Part15) Then

Is there a better way to write it?

jon
 

MarkK

bit cruncher
Local time
Yesterday, 21:04
Joined
Mar 17, 2004
Messages
8,181
There is almost certainly a normalization problem with your data, and if you fix that you would also be able to solve this comparison problem much more simply.

But the problem you are asking about is probably the difference between these expressions...
  • x = a or x = b or x = c or x = d
  • x = a or b or c or d

You probably mean to do the first one. It's unlikely that...
Code:
Part5b OR Part6b
...is a valid boolean expression.
hth
Mark
 

kevlray

Registered User.
Local time
Yesterday, 21:04
Joined
Apr 5, 2010
Messages
1,046
Valid, but what are you trying to achieve?
 

Jon123

Registered User.
Local time
Today, 00:04
Joined
Aug 29, 2003
Messages
668
I'm actually getting an error. If I drop the last to OR's it works.

So I'm building a kit that contains 15 parts all different. As a user selects the parts from a main table to put into a kit I want to verify there all different.
Part1a, Part2a, etc etc are the different parts and can be entered in any order but once you update if that one is already in the kit I want to catch it and stopn it.
 

MarkK

bit cruncher
Local time
Yesterday, 21:04
Joined
Mar 17, 2004
Messages
8,181
This is a one-to-many relationship between the kit and the parts. To model this in a single row in a database is a mistake. This should be done with two tables, and the solution will be way simpler.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:04
Joined
May 7, 2009
Messages
19,243
Use a function to do it. On a standard module add this.

Public function IsKitExists(pComp, Paramarray p() as variant) as boolean
Dim sConcat as string
Dim var as variant
Dim bolreturn as boolean
'concatrnate each kit
sConcat="/"
For each var in p
sConcat= sConcat & "/"
Next
'test for duplicate kit
For each var in p
Bolreturn=(instr (sConcat,"/" & pComp & "/")>0)
If bolreturn then exit for
Next
IsKitExists=bolreturn
End function

You pass the kit to compare followed by list of kits to compare with.

Now you call the function to validate:

If IskitExists(me.part1b,me.part2b, ...) Then
'part already exists

Else
'kit does not exist so Okey

End If
 
Last edited:

Jon123

Registered User.
Local time
Today, 00:04
Joined
Aug 29, 2003
Messages
668
It is being done in 2 tables but I need to build the kit as a form because the parts can and do change until we get a matched set if you will.
 

kevlray

Registered User.
Local time
Yesterday, 21:04
Joined
Apr 5, 2010
Messages
1,046
Without restructuring the tables (which looks like it should be done). The proper syntax for the if statement (I believe) would be like this.

If (Me.[Part1b]) = (Me.[Part2b]) Or (Me.[Part1b]) =(Me.Part3b) Or (Me.[Part1b]) =(Me.Part4b) Or (Me.[Part1b]) =(Me.Part5b) Or (Me.[Part1b]) = (Me.Part6b) Or (Me.[Part1b]) =(Me.Part7b) Or (Me.[Part1b]) = (Me.Part8b) Or (Me.[Part1b]) =(Me.Part9b) Or (Me.[Part1b]) =(Me.Part10b) Or (Me.[Part1b]) =(Me.Part11b) Or (Me.[Part1b]) =(Me.Part12b) Or (Me.[Part1b]) =(Me.Part13b) Or (Me.[Part1b]) =(Me.Part14) Or (Me.[Part1b]) =(Me.Part15) Then

basically every statement between the OR's has to evaluate to a True or False.
 

plog

Banishment Pending
Local time
Yesterday, 23:04
Joined
May 11, 2011
Messages
11,646
So I'm building a kit that contains 15 parts all different.

kevlray's last post gets you 2/15th of the way there. He does 14 comparisons, what you want requires 105.

Structure your table properly as per Mark's advice.
 

MarkK

bit cruncher
Local time
Yesterday, 21:04
Joined
Mar 17, 2004
Messages
8,181
In SQL, testing for the existence of a related child row with criteria is trivial.
Code:
SELECT Count(*)
FROM tKitParts
WHERE ParentKitID = 123
AND PartID = 456
That counts how many parts there already are in kit 123 having part number 456. If the result is > 0, then the part exists.
Or use DCount()...
Code:
dim criteria as string

criteria = "ParentKitID = " & Me.ActiveKitID & " AND PartID = " & Me.PartBeingAddedID

If DCount("*", "tKitParts", criteria) > 0 Then
   Msgbox "Part already exists in this kit"
Else
[COLOR="Green"]   'add part to kit[/COLOR]
End If

...but this...
If (Me.[Part1b]) = (Me.[Part2b]) Or (Me.[Part1b]) =(Me.Part3b) Or (Me.[Part1b]) =(Me.Part4b) Or (Me.[Part1b]) =(Me.Part5b) Or (Me.[Part1b]) = (Me.Part6b) Or (Me.[Part1b]) =(Me.Part7b) Or (Me.[Part1b]) = (Me.Part8b) Or (Me.[Part1b]) =(Me.Part9b) Or (Me.[Part1b]) =(Me.Part10b) Or (Me.[Part1b]) =(Me.Part11b) Or (Me.[Part1b]) =(Me.Part12b) Or (Me.[Part1b]) =(Me.Part13b) Or (Me.[Part1b]) =(Me.Part14) Or (Me.[Part1b]) =(Me.Part15) Then
...is a losing battle, because you haven't compared to Part7 to Part11 yet, and you haven't checked for Nulls

hth
Mark
 

Users who are viewing this thread

Top Bottom