Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Else without If?

Below are my IF statements.  When I compile I get an "Else without If" error.  Any idea what could be wrong?




             If transtype = "D" Then endbal = custbal - transamt
                credits = 0
                debits = transamt
                balancetotal = balancetotal + endbal
                FormatCurrency (custbal)
                Write #3, custnum, custname, custbal, debits, credits, endbal
               
           
            Else: If transtype = "C" Then endbal = custbal + transamt
                debits = 0
                credits = transamt
                balancetotal = balancetotal + endbal
                Write #3, custnum, custname, custbal, debits, credits, endbal
            End If
            End If



0
JustinBGSU
Asked:
JustinBGSU
  • 5
  • 3
  • 2
1 Solution
 
JacamarCommented:
If transtype = "D" Then endbal = custbal - transamt
               credits = 0
               debits = transamt
               balancetotal = balancetotal + endbal
               FormatCurrency (custbal)
               Write #3, custnum, custname, custbal, debits, credits, endbal
               
           
           Else: If transtype = "C" Then endbal = custbal + transamt
               debits = 0
               credits = transamt
               balancetotal = balancetotal + endbal
               Write #3, custnum, custname, custbal, debits, credits, endbal
           End If
           End If


try this.  I moved it around a bit
0
 
Da_WeaselCommented:
Should prolly look something like this....

If transtype = "D" Then
  endbal = custbal - transamt
  credits = 0
  debits = transamt
  balancetotal = balancetotal + endbal
  FormatCurrency (custbal)
  Write #3, custnum, custname, custbal, debits, credits, endbal
Else If transtype = "C" Then
  endbal = custbal + transamt
  debits = 0
  credits = transamt
  balancetotal = balancetotal + endbal
  Write #3, custnum, custname, custbal, debits, credits, endbal
End If
0
 
JacamarCommented:
If transtype = "D" Then
    endbal = custbal - transamt
               credits = 0
               debits = transamt
               balancetotal = balancetotal + endbal
               FormatCurrency (custbal)
               Write #3, custnum, custname, custbal, debits, credits, endbal
               
           
           Else
           If transtype = "C" Then
           endbal = custbal + transamt
               debits = 0
               credits = transamt
               balancetotal = balancetotal + endbal
               Write #3, custnum, custname, custbal, debits, credits, endbal
           End If
           End If

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JacamarCommented:
sorry, I pasted the wrong one.
0
 
JacamarCommented:
The reason you had an error was because of this line.

If transtype = "D" Then endbal = custbal - transamt

and this line

Else: If transtype = "C" Then endbal = custbal + transamt

because you have your if condition with a statement on the same line, the statement  "endbal = custbal + transamt" is ending the if automatically because it is on the same line as the if statement.  

If you put the code that you want it to do all on lines after the if statement then it will leave the if open until you "end if"

I hope this helps you understand better.

Jacamar
0
 
JustinBGSUAuthor Commented:
Jacamar,

Thanks a ton, that worked for me.

Any idea how to write to the .txt file without having quotes and commas appear?
0
 
Da_WeaselCommented:
There is a slight mistake in the code I posted above.  The Else statment should be an ElseIf statement.

Here is an explaination of what what wrong with your code:

In an IF statement if there is code directly following the THEN keyword or a line continuation characters _ then only that code is executed when the statement evaluates to TRUE.

Example 1:

If x = True Then DoSomething
Else DoSomethingElse
EndIf

is the same as:
If x = True Then
  DoSomething
Else
  DoSomethingElse
EndIf

Example 2:

If x = True Then DoSomething & _
  DoSomethingMore
Else DoSomethingElse & _
  DoSomethingMore
EndIf

is the same as:
If x = True Then
  DoSomething
  DoSomethingMore
Else
  DoSomethingElse
  DoSomethingMore
EndIf

I would suggest using the second form of each of the 2 above examples as there are not real benifits to using the first, and the first form can often create hard to find bugs.
0
 
Da_WeaselCommented:
Use the PRINT statement instead of WRITE
0
 
JacamarCommented:
yep, da weasel is right.  Print will get rid of the quotations.  
0
 
JustinBGSUAuthor Commented:
Thanks a bunch to Jacamar and Da Weasel....

I'm sure I wouldn't have figured this out without you.  I really appreciate the help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now