Need help with vba code (1 Viewer)

kasmax

Registered User.
Local time
Today, 17:46
Joined
Jun 4, 2012
Messages
38
Hi Everyone
Im new here and this is my first post.
i have a vba code that i dont fully understand:

Public Function SCodeSelect(ByVal Value As Long) As Boolean
Dim bResult As Boolean
Dim ScodePattern As Long
Dim ExactMatch As Boolean
Dim IgnoreSpecial As Boolean
Dim UseOR As Boolean
'Does a bitewise AND check on value looking for the
ScodePattern = DLookup("Value1", "Variables", "Parameter='SCODE'")
ExactMatch = Val(DLookup("Value1", "Variables", "Parameter='EXACTMATCH'") & "") = 1
IgnoreSpecial = Val(DLookup("Value1", "Variables", "Parameter='IGNORESPECIAL'") & "") = 1
UseOR = Val(DLookup("Value1", "Variables", "Parameter='ANDOR'") & "") = 0
If Not UseOR Then
If ExactMatch Then
bResult = (Value = ScodePattern)
If Not bResult And IgnoreSpecial Then
bResult = ((Value - 64) = ScodePattern)
End If
Else
bResult = (Value And ScodePattern) = ScodePattern
End If
Else
'Use the OR method
'Check Each school type
'Kindy = 1
If Not bResult And (ScodePattern And 1) = 1 And (Value And 1) = 1 Then bResult = True
If Not bResult And (ScodePattern And 2) = 2 And (Value And 2) = 2 Then bResult = True
If Not bResult And (ScodePattern And 4) = 4 And (Value And 4) = 4 Then bResult = True
If Not bResult And (ScodePattern And 8) = 8 And (Value And 8) = 8 Then bResult = True
If Not bResult And (ScodePattern And 16) = 16 And (Value And 16) = 16 Then bResult = True
If Not bResult And (ScodePattern And 32) = 32 And (Value And 32) = 32 Then bResult = True
If Not bResult And (ScodePattern And 64) = 64 And (Value And 64) = 64 Then bResult = True
End If
SCodeSelect = bResult
End Function

can anyone plz help me to understand what is it doing. especially the contitions in it. maybe if someone can comment or explain what each line is doing and how is it getting to the final result which is true or false.

many thanks in advance
 

NigelShaw

Registered User.
Local time
Today, 10:46
Joined
Jan 11, 2008
Messages
1,573
Is this a school project? Why wold you have a code routine tht you dont understand?

It is looking for values in in a table called Variables using DLookup. Each Variable set at the 'Dim' section is set at DLookup by collecting the value of 'Value1' based on the criteria of the parameter.

Booleans are true/false values so a check for ExactMatch will be one of the two.

IfNot is the opposite of If.

IfNot UseOR ( check for false )
If ExactMatch (check or true )

Vba is quite methodical so look through it and understand it. If value is 1 then do something else, do another something. Think of like a multi directional story book-

If jack takes the bucket, he goes down the red path but if he leaves the bucket, he can go down the blue path...........


Nidge
 

kasmax

Registered User.
Local time
Today, 17:46
Joined
Jun 4, 2012
Messages
38
Hi NigelShaw, Many thanks for the help. Well its not exactly a school project but its a data analysis. and the reason i have this routine and dont fully understand is because its initially developed by someone else and i have to modify it.
i understand vba and im not new to it. however the logics used in this code are bit different. i understand how it lookup table values etc. the confusing part for me is

bResult = (Value = ScodePattern)

bResult = ((Value - 64) = ScodePattern)

bResult = (Value And ScodePattern) = ScodePattern

What does those codes above do?

and finally


If Not bResult And (ScodePattern And 16) = 16 And (Value And 16) = 16 Then bResult = True

what exactly it means.

thanks for the help again. chees
 

ChrisO

Registered User.
Local time
Today, 19:46
Joined
Apr 30, 2003
Messages
3,202
The parentheses here help to understand what’s going on:-

bResult = (Value = ScodePattern)

Work inside out…

(Value = ScodePattern) is either True or False.
Whatever it is, assign it to the Boolean.

Basically the same goes for all the others, work inside out.

Chris.
 

kasmax

Registered User.
Local time
Today, 17:46
Joined
Jun 4, 2012
Messages
38
Thanks very much chris. i understand it now.
cheers
 

Users who are viewing this thread

Top Bottom