error message: "Else without if"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hannoudw
    New Member
    • Aug 2010
    • 115

    error message: "Else without if"

    I am trying to get the below statement work in VBA, but I always get the following error message: "Else without if"
    Hope that someone help me ...
    Code:
    If (Qty < SolderieQty Or Qty = SolderieQty) Then
    If (Qty < sumq) Then
    MsgBox "check the Quantity. qty < sumq"
    Me.q1.SetFocus
    End If
    If (Qty > sumq) Then
    MsgBox "check the Quantity. qty > sumq"
    Me.q1.SetFocus
    End If
    
    If (Qty = sumq) Then
    'ADD the record into SendDetails table
    If ((Not IsNull(Article)) And (Not IsNull(sentdate)) And (Not IsNull(SendFrom)) And (Not IsNull(SendTo))) Then
       MsgBox "first"
     If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo))) Then
          MsgBox "you have sent this article before to the same location"
          Forms![Transfer]![Article].SetFocus
          MsgBox "aaaa"
     Else
     If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo)) = False) Then
        MsgBox "bbbb"
    InsertIntoSendDetails CDate(sentdate), CStr(SendFrom), CStr(SendTo), CLng(Article), CLng(Qty), CLng(q1), CLng(q2), _
    CLng(q3), CLng(q4), CLng(q5), CLng(q6), CLng(q7), CLng(q8), CLng(q9), CLng(q10), CLng(q11), CLng(q12), CLng(q13), CLng(q14) 'ADD TO WearHouseStock (st_house_qi) and MINUS FROM SolderieStock (st_sol_qi)
    
    Me.st_q1.Value = (Nz(Me.st_q1.Value, 0) - Nz(Me.q1.Value, 0))
    Me.st_relax_q1.Value = (Nz(Me.st_relax_q1.Value, 0) + Nz(Me.q1.Value, 0))
    Me.st_q2.Value = (Nz(Me.st_q2.Value, 0) - Nz(Me.q2.Value, 0))
    Me.st_relax_q2.Value = (Nz(Me.st_relax_q2.Value, 0) + Nz(Me.q2.Value, 0))
    Me.st_q3.Value = (Nz(Me.st_q3.Value, 0) - Nz(Me.q3.Value, 0))
    Me.st_relax_q3.Value = (Nz(Me.st_relax_q3.Value, 0) + Nz(Me.q3.Value, 0))
    Me.st_q4.Value = (Nz(Me.st_q4.Value, 0) - Nz(Me.q4.Value, 0))
    Me.st_relax_q4.Value = (Nz(Me.st_relax_q4.Value, 0) + Nz(Me.q4.Value, 0))
    Me.st_q5.Value = (Nz(Me.st_q5.Value, 0) - Nz(Me.q5.Value, 0))
    Me.st_relax_q5.Value = (Nz(Me.st_relax_q5.Value, 0) + Nz(Me.q5.Value, 0))
    Me.st_q6.Value = (Nz(Me.st_q6.Value, 0) - Nz(Me.q6.Value, 0))
    Me.st_relax_q6.Value = (Nz(Me.st_relax_q6.Value, 0) + Nz(Me.q6.Value, 0))
    Me.st_q7.Value = (Nz(Me.st_q7.Value, 0) - Nz(Me.q7.Value, 0))
    Me.st_relax_q7.Value = (Nz(Me.st_relax_q7.Value, 0) + Nz(Me.q7.Value, 0))
    Me.st_q8.Value = (Nz(Me.st_q8.Value, 0) - Nz(Me.q8.Value, 0))
    Me.st_relax_q8.Value = (Nz(Me.st_relax_q8.Value, 0) + Nz(Me.q8.Value, 0))
    Me.st_q9.Value = (Nz(Me.st_q9.Value, 0) - Nz(Me.q9.Value, 0))
    Me.st_relax_q9.Value = (Nz(Me.st_relax_q9.Value, 0) + Nz(Me.q9.Value, 0))
    Me.st_q10.Value = (Nz(Me.st_q10.Value, 0) - Nz(Me.q10.Value, 0))
    Me.st_relax_q10.Value = (Nz(Me.st_relax_q10.Value, 0) + Nz(Me.q10.Value, 0))
    Me.st_q11.Value = (Nz(Me.st_q11.Value, 0) - Nz(Me.q11.Value, 0))
    Me.st_relax_q11.Value = (Nz(Me.st_relax_q11.Value, 0) + Nz(Me.q11.Value, 0))
    Me.st_q12.Value = (Nz(Me.st_q12.Value, 0) - Nz(Me.q12.Value, 0))
    Me.st_relax_q12.Value = (Nz(Me.st_relax_q12.Value, 0) + Nz(Me.q12.Value, 0))
    Me.st_q13.Value = (Nz(Me.st_q13.Value, 0) - Nz(Me.q13.Value, 0))
    Me.st_relax_q13.Value = (Nz(Me.st_relax_q13.Value, 0) + Nz(Me.q13.Value, 0))
    Me.st_q14.Value = (Nz(Me.st_q14.Value, 0) - Nz(Me.q14.Value, 0))
    Me.st_relax_q14.Value = (Nz(Me.st_relax_q14.Value, 0) + Nz(Me.q14.Value, 0))
    Me.SolderieStock.Value = (Nz(Me.SolderieStock.Value, 0) - Me.Qty.Value)
    Me.RelaxStock.Value = (Nz(Me.RelaxStock.Value, 0) + Me.Qty.Value)
    End If
    End If
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    Else
    MsgBox "Qty entered more than the solderie stock quantity"
    Me.Qty.SetFocus
    End If
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    The problem is at line#19 and line#20. Your code should look like this ...

    Code:
    If (Qty < SolderieQty Or Qty = SolderieQty) Then
        If (Qty < sumq) Then
            MsgBox "check the Quantity. qty < sumq"
            Me.q1.SetFocus
        End If
        
        If (Qty > sumq) Then
            MsgBox "check the Quantity. qty > sumq"
            Me.q1.SetFocus
        End If
         
        If (Qty = sumq) Then
            'ADD the record into SendDetails table
            If ((Not IsNull(Article)) And (Not IsNull(sentdate)) And (Not IsNull(SendFrom)) And (Not IsNull(SendTo))) Then
                MsgBox "first"
                If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo))) Then
                      MsgBox "you have sent this article before to the same location"
                      Forms![Transfer]![Article].SetFocus
                      MsgBox "aaaa"
                ElseIf (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo)) = False) Then
                    MsgBox "bbbb"
                    InsertIntoSendDetails CDate(sentdate), CStr(SendFrom), CStr(SendTo), CLng(Article), CLng(Qty), CLng(q1), CLng(q2), _
                        CLng(q3), CLng(q4), CLng(q5), CLng(q6), CLng(q7), CLng(q8), CLng(q9), CLng(q10), CLng(q11), CLng(q12), CLng(q13), CLng(q14) 'ADD TO WearHouseStock (st_house_qi) and MINUS FROM SolderieStock (st_sol_qi)
                 
                    Me.st_q1.Value = (Nz(Me.st_q1.Value, 0) - Nz(Me.q1.Value, 0))
                    Me.st_relax_q1.Value = (Nz(Me.st_relax_q1.Value, 0) + Nz(Me.q1.Value, 0))
                    Me.st_q2.Value = (Nz(Me.st_q2.Value, 0) - Nz(Me.q2.Value, 0))
                    Me.st_relax_q2.Value = (Nz(Me.st_relax_q2.Value, 0) + Nz(Me.q2.Value, 0))
                    Me.st_q3.Value = (Nz(Me.st_q3.Value, 0) - Nz(Me.q3.Value, 0))
                    Me.st_relax_q3.Value = (Nz(Me.st_relax_q3.Value, 0) + Nz(Me.q3.Value, 0))
                    Me.st_q4.Value = (Nz(Me.st_q4.Value, 0) - Nz(Me.q4.Value, 0))
                    Me.st_relax_q4.Value = (Nz(Me.st_relax_q4.Value, 0) + Nz(Me.q4.Value, 0))
                    Me.st_q5.Value = (Nz(Me.st_q5.Value, 0) - Nz(Me.q5.Value, 0))
                    Me.st_relax_q5.Value = (Nz(Me.st_relax_q5.Value, 0) + Nz(Me.q5.Value, 0))
                    Me.st_q6.Value = (Nz(Me.st_q6.Value, 0) - Nz(Me.q6.Value, 0))
                    Me.st_relax_q6.Value = (Nz(Me.st_relax_q6.Value, 0) + Nz(Me.q6.Value, 0))
                    Me.st_q7.Value = (Nz(Me.st_q7.Value, 0) - Nz(Me.q7.Value, 0))
                    Me.st_relax_q7.Value = (Nz(Me.st_relax_q7.Value, 0) + Nz(Me.q7.Value, 0))
                    Me.st_q8.Value = (Nz(Me.st_q8.Value, 0) - Nz(Me.q8.Value, 0))
                    Me.st_relax_q8.Value = (Nz(Me.st_relax_q8.Value, 0) + Nz(Me.q8.Value, 0))
                    Me.st_q9.Value = (Nz(Me.st_q9.Value, 0) - Nz(Me.q9.Value, 0))
                    Me.st_relax_q9.Value = (Nz(Me.st_relax_q9.Value, 0) + Nz(Me.q9.Value, 0))
                    Me.st_q10.Value = (Nz(Me.st_q10.Value, 0) - Nz(Me.q10.Value, 0))
                    Me.st_relax_q10.Value = (Nz(Me.st_relax_q10.Value, 0) + Nz(Me.q10.Value, 0))
                    Me.st_q11.Value = (Nz(Me.st_q11.Value, 0) - Nz(Me.q11.Value, 0))
                    Me.st_relax_q11.Value = (Nz(Me.st_relax_q11.Value, 0) + Nz(Me.q11.Value, 0))
                    Me.st_q12.Value = (Nz(Me.st_q12.Value, 0) - Nz(Me.q12.Value, 0))
                    Me.st_relax_q12.Value = (Nz(Me.st_relax_q12.Value, 0) + Nz(Me.q12.Value, 0))
                    Me.st_q13.Value = (Nz(Me.st_q13.Value, 0) - Nz(Me.q13.Value, 0))
                    Me.st_relax_q13.Value = (Nz(Me.st_relax_q13.Value, 0) + Nz(Me.q13.Value, 0))
                    Me.st_q14.Value = (Nz(Me.st_q14.Value, 0) - Nz(Me.q14.Value, 0))
                    Me.st_relax_q14.Value = (Nz(Me.st_relax_q14.Value, 0) + Nz(Me.q14.Value, 0))
                    Me.SolderieStock.Value = (Nz(Me.SolderieStock.Value, 0) - Me.Qty.Value)
                    Me.RelaxStock.Value = (Nz(Me.RelaxStock.Value, 0) + Me.Qty.Value)
                End If
            End If
        DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
    Else
        MsgBox "Qty entered more than the solderie stock quantity"
        Me.Qty.SetFocus
    End If

    Comment

    • dsatino
      Contributor
      • May 2010
      • 393

      #3
      You should also get out of the practice of using embedded IF...Then statements and use Select Case where possible.

      Also, you had the forsight to name your controls sequentially so why not use that and reduce the amount of code?

      Add this to the beginning of the procedure:

      Code:
      Dim xLoop as Integer
      And then replace lines 25 through 52 with this:

      Code:
      For xLoop = 1 to 14
         Me.Controls("st_q" & xLoop).Value = (Nz(Me.Controls("st_q" & xLoop).Value, 0) - Nz(Me.Controls("q" & xLoop).Value, 0)) 
      
         Me.Controls("st_relax_q" & xLoop).Value = (Nz(Me.Controls("st_relax_q" & xLoop).Value, 0) + Nz(Me.Controls("q" & xLoop).Value, 0)) 
      Next

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        Notice that Mary's code is not only without the error that's in yours, it's also properly indented so that it's much easier to see such a problem before you even try to compile it. I suggest you use this format for all your code in future and have fewer problems as a result.

        PS. DSatino's comments are also worth considering even if Mary has found and fixed your actual problem.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Here is another Version of dsatino's explanation in Post# 3, which you may/may not find easier to understand and implement:
          Code:
          Dim bytCtrlCtr As Integer
          Dim ctl1 As Control
          Dim ctl2 As Control
          Dim ctl3 As Control
          
          For bytCtrlCtr = 1 To 14
            Set ctl1 = Me.Controls("st_q" & CStr(bytCtrlCtr))
            Set ctl2 = Me.Controls("q" & CStr(bytCtrlCtr))
            Set ctl3 = Me.Controls("st_relax_q" & CStr(bytCtrlCtr))
            
            ctl1 = (Nz(ctl1, 0) - Nz(ctl2, 0))
            ctl3 = (Nz(ctl3, 0) + Nz(ctl2, 0))
          Next

          Comment

          Working...