IF OR THEN, Multiple criteria VBA Function

J

Jerad

I searched the forums with no avail. I cant seem to figure out how to:
function somefunction(arg1, arg2)
If arg1 = "someText" OR "SomeOtherText" AND arg2 = 1 THEN
Run some code
else
Run some other code
end if
Not sure how to do this, I get errors anyway I try. I get errors with even
this:

If arg1 = "someText" OR "SomeOtherText" THEN
Run some code
else
Run other code
End if
Any help would be greatly appreciated. Thank you in advance
 
D

Douglas J Steele

If arg1 = "someText" OR arg1 = "SomeOtherText" AND arg2 = 1 THEN

Note that you should include parentheses so it's obvious which you want:

If (arg1 = "someText" OR arg1 = "SomeOtherText") AND arg2 = 1 THEN

or

If arg1 = "someText" OR (arg1 = "SomeOtherText" AND arg2 = 1) THEN
 
J

Jerad

Douglas said:
If arg1 = "someText" OR arg1 = "SomeOtherText" AND arg2 = 1 THEN

Note that you should include parentheses so it's obvious which you want:

If (arg1 = "someText" OR arg1 = "SomeOtherText") AND arg2 = 1 THEN
Douglas,
Thanks for the response but I still get a "type mismatch" when using this
function in a query???
This is a simple SELECT query containing only 1 table
In my table, the field passed to Arg1 is set as "text" and the field passed
to arg2 is "number, Single"
I thought that usually "type mismatch" has to do with data type but i cant
seem to figure this out.
Thanks,
Jerad
 
D

Douglas J Steele

Try declaring the data types of your parameters in the function declaration:

function somefunction(arg1 As String, arg2 As Single)
 
J

Jerad via AccessMonster.com

Douglas said:
Try declaring the data types of your parameters in the function declaration:

function somefunction(arg1 As String, arg2 As Single)

Thanks for the response, I declared the variables (probably good practice
anyway,
but recieved the same erros); heres what solved the problem in case anyone
else encounters the same thing.

function somefunction(arg1 as string, arg2 as single)
If (arg1 = "someText" And arg2 = 1) OR (Arg1 = "SomeOtherText" And arg2 = 1)
Then
run some code
else
run other code
end if
I gues I had to be more specific . Works now though, thanks for help guys
JK
 
D

Douglas J. Steele

Jerad via AccessMonster.com said:
Thanks for the response, I declared the variables (probably good practice
anyway,
but recieved the same erros); heres what solved the problem in case anyone
else encounters the same thing.

function somefunction(arg1 as string, arg2 as single)
If (arg1 = "someText" And arg2 = 1) OR (Arg1 = "SomeOtherText" And arg2 =
1)
Then
run some code
else
run other code
end if
I gues I had to be more specific . Works now though, thanks for help guys

Glad you got it working, but what you ended up with should be the same as

If (arg1 = "someText" OR arg1 = "SomeOtherText") AND arg2 = 1 THEN

I suspect you must have mistyped something if that didn't work for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top