End If without Block If error

  • Thread starter Thread starter Markus
  • Start date Start date
M

Markus

I have the following function on a form to sum the values
of controls based on the value in other controls. Here is
the function:

Option Compare Database
Option Explicit

Private Function fSumUp()
Dim intHourCount As Integer
Dim varReturn

For intHourCount = 1 To 12
If Me("CB" & intHourCount) = "EV" Then
varReturn = varReturn & Nz(Me("TS" & intHourCount), 0)
End If
Next intHourCount

fSumUp = varReturn

End Function

I have a text box named evRES with =fSumUp() as the
control source. When I switch the form from design view I
get an error mesage thats says End If without Block If.
Any help would be appreciated.
 
Markus said:
I have the following function on a form to sum the values
of controls based on the value in other controls. Here is
the function:

Option Compare Database
Option Explicit

Private Function fSumUp()
Dim intHourCount As Integer
Dim varReturn

For intHourCount = 1 To 12
If Me("CB" & intHourCount) = "EV" Then
varReturn = varReturn & Nz(Me("TS" & intHourCount), 0)
End If
Next intHourCount

fSumUp = varReturn

End Function

I have a text box named evRES with =fSumUp() as the
control source. When I switch the form from design view I
get an error mesage thats says End If without Block If.
Any help would be appreciated.

What makes you think the code above is throwing the error? Are you running
this function in the form's Open or Load event?

Doesn't the error give you the option of "debugging" and taking you to the
line of code that is failing?
 
Markus said:
I don't know if it is on the code itself or not. I just
tried to supply as much information as I could. I've tried
both the Open and Load event(I've also taken it off the
form and left it on the text box only) and when I switch
from design view I get the error. The highlighted section
is:
Private Function fsumup()
The help file for the error simply says I may be missing
an If statement for the End If statement.

I have on occasion seen Access highlight the wrong row of code when there
was a problem. Perhaps that is what is happening to you. Can you do a
compile and save on the entire project without errors?
 
Markus, is you if statement 1 long line? ie.e IF soemthing Then something.
If so, you don't need to use End If. End If is needed when the statement
after Then is on the next line by itself. This allows you to put multiple
command after the Then and the End If tells it when to stop. If it is on
the same line as Then, you are only allowed one command so End If isn't
needed.

Kelvin
 
Thanks Kelvin that was exactly the problem.
Once the function successfully went through on the form I
noticed another issue. I want the Text box to contain the
SUM of the controls. Instead it is providing the values of
the controls. so instead of a total such as 240 what I get
right now is 60606060. Thanks
 
It is the line

varReturn = varReturn & Nz(Me("TS" & intHourCount), 0)

The & means to connect strings. To add you need to use +

varReturn = varReturn + Nz(Me("TS" & intHourCount), 0)

Assuming your control is a number, if not you will need to convert to a
number first.

varReturn = varReturn + CInt(Nz(Me("TS" & intHourCount), 0))

Kelvin
 
Markus said:
I have the following function on a form to sum the values
of controls based on the value in other controls. Here is
the function:

Option Compare Database
Option Explicit

Private Function fSumUp()
Dim intHourCount As Integer
Dim varReturn

For intHourCount = 1 To 12
If Me("CB" & intHourCount) = "EV" Then
varReturn = varReturn & Nz(Me("TS" & intHourCount), 0)
End If
Next intHourCount

fSumUp = varReturn

End Function

I have a text box named evRES with =fSumUp() as the
control source. When I switch the form from design view I
get an error mesage thats says End If without Block If.


Because of news reader line wrapping, I can't tell for sure,
but it looks like you wrote your If statement in this style:

If condition Then varReturn = ...
End If

If that's the case, you're mixing the two different forms of
the If statement. Either use

If condition Then varReturn = ...

without End If

Or use this style

If condition Then
varReturn = ...
End If
 
Back
Top