A very simple question about IF statement

Ammarhm

Beginner User
Local time
Yesterday, 23:29
Joined
Jul 3, 2008
Messages
80
Hi
A very basic VBA question about IF ... Else ..... End if

What i need to do is do the following

If "a certain condition is fullfilled" then
do this
and this
and this

Else
do that
and that
and that

End if

However when i write this in VBA editor in MS access, I get an error, it seems that the code dose not understand that "do this, and this, and this" should be executed if the condition is fullfilled, the code understands each line as a seperate command and it only executes the first line "do this" if the condition is met and not the other 2 lines "and this, and this" and I get other errors regarding Else and End if "that there is no if command"
how do you solve this so that VBA understands that the lines are "together"

Regards
 
Hi
This is the code here, SearchForValue is a function i have written that looks for a string in another string.

If .FormFields("labval").Result <> "" Then _
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")
Else
Lab = "not done"

End If
 
Hi
If .FormFields("labval").Result <> "" Then _
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")
Else
Lab = "not done"

End If
1. What is Lab?
2. Is vRecordSet("Test1") = SearchForValue(Lab, "IN")
the line that give the first error?
3. If the answer to 2 is yes can you tell us what
vRecordSet("Test1") is.
 
Take out the line continuation:

If .FormFields("labval").Result <> "" Then
 
Thank you for your answers

Actually, the problem is not in the part you are referring to

1- if i remove the if statement and runn the following code only, it executes perfectly:
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")

i dont get any error here

2- with the if statement, the error i am getting is "Compile error: Else without if" and the Else word is highlighted

If .FormFields("labval").Result <> "" Then _
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")
Else
Lab = "not done"

End If



3- If I remove the part after "Else" and execute the code the following happens:

a. if the condition is meet, ie if .FormFields("labval").Result <> "" the code excutes all following code:
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")

b. if the condition is not meet ie .FormFields("labval").Result <> "" is not right, what happenes is that the code DOSE NOT execute the FIRST LINE ONLY
Lab = .FormFields("labval").Result

yet it will execute the remaining part of the code:

vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")

It just seems that the code dose not understand that all the following lines should be together as the part to be executed if the condition is meet:

Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")


Maybe this makes my problem clearer for you?
I appreciate any help
Regards
 
Did you take out the underscore? That's a line continuation, so you have

If A Then B

which is why you get the compile error. You're mixing the one-line and block formats. Again, try


If .FormFields("labval").Result <> "" Then
Lab = .FormFields("labval").Result
vRecordSet("Test1") = SearchForValue(Lab, "IN")
vRecordSet("Test2") = SearchForValue(Lab, "AP")
vRecordSet("Test3") = SearchForValue(Lab, "cr")
vRecordSet("Test4") = SearchForValue(Lab, "pl")
Else
Lab = "not done"

End If
 

Users who are viewing this thread

Back
Top Bottom