By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

error message: "Else without if"

100+
P: 115
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 ...
Expand|Select|Wrap|Line Numbers
  1. If (Qty < SolderieQty Or Qty = SolderieQty) Then
  2. If (Qty < sumq) Then
  3. MsgBox "check the Quantity. qty < sumq"
  4. Me.q1.SetFocus
  5. End If
  6. If (Qty > sumq) Then
  7. MsgBox "check the Quantity. qty > sumq"
  8. Me.q1.SetFocus
  9. End If
  10.  
  11. If (Qty = sumq) Then
  12. 'ADD the record into SendDetails table
  13. If ((Not IsNull(Article)) And (Not IsNull(sentdate)) And (Not IsNull(SendFrom)) And (Not IsNull(SendTo))) Then
  14.    MsgBox "first"
  15.  If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo))) Then
  16.       MsgBox "you have sent this article before to the same location"
  17.       Forms![Transfer]![Article].SetFocus
  18.       MsgBox "aaaa"
  19.  Else
  20.  If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo)) = False) Then
  21.     MsgBox "bbbb"
  22. InsertIntoSendDetails CDate(sentdate), CStr(SendFrom), CStr(SendTo), CLng(Article), CLng(Qty), CLng(q1), CLng(q2), _
  23. 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)
  24.  
  25. Me.st_q1.Value = (Nz(Me.st_q1.Value, 0) - Nz(Me.q1.Value, 0))
  26. Me.st_relax_q1.Value = (Nz(Me.st_relax_q1.Value, 0) + Nz(Me.q1.Value, 0))
  27. Me.st_q2.Value = (Nz(Me.st_q2.Value, 0) - Nz(Me.q2.Value, 0))
  28. Me.st_relax_q2.Value = (Nz(Me.st_relax_q2.Value, 0) + Nz(Me.q2.Value, 0))
  29. Me.st_q3.Value = (Nz(Me.st_q3.Value, 0) - Nz(Me.q3.Value, 0))
  30. Me.st_relax_q3.Value = (Nz(Me.st_relax_q3.Value, 0) + Nz(Me.q3.Value, 0))
  31. Me.st_q4.Value = (Nz(Me.st_q4.Value, 0) - Nz(Me.q4.Value, 0))
  32. Me.st_relax_q4.Value = (Nz(Me.st_relax_q4.Value, 0) + Nz(Me.q4.Value, 0))
  33. Me.st_q5.Value = (Nz(Me.st_q5.Value, 0) - Nz(Me.q5.Value, 0))
  34. Me.st_relax_q5.Value = (Nz(Me.st_relax_q5.Value, 0) + Nz(Me.q5.Value, 0))
  35. Me.st_q6.Value = (Nz(Me.st_q6.Value, 0) - Nz(Me.q6.Value, 0))
  36. Me.st_relax_q6.Value = (Nz(Me.st_relax_q6.Value, 0) + Nz(Me.q6.Value, 0))
  37. Me.st_q7.Value = (Nz(Me.st_q7.Value, 0) - Nz(Me.q7.Value, 0))
  38. Me.st_relax_q7.Value = (Nz(Me.st_relax_q7.Value, 0) + Nz(Me.q7.Value, 0))
  39. Me.st_q8.Value = (Nz(Me.st_q8.Value, 0) - Nz(Me.q8.Value, 0))
  40. Me.st_relax_q8.Value = (Nz(Me.st_relax_q8.Value, 0) + Nz(Me.q8.Value, 0))
  41. Me.st_q9.Value = (Nz(Me.st_q9.Value, 0) - Nz(Me.q9.Value, 0))
  42. Me.st_relax_q9.Value = (Nz(Me.st_relax_q9.Value, 0) + Nz(Me.q9.Value, 0))
  43. Me.st_q10.Value = (Nz(Me.st_q10.Value, 0) - Nz(Me.q10.Value, 0))
  44. Me.st_relax_q10.Value = (Nz(Me.st_relax_q10.Value, 0) + Nz(Me.q10.Value, 0))
  45. Me.st_q11.Value = (Nz(Me.st_q11.Value, 0) - Nz(Me.q11.Value, 0))
  46. Me.st_relax_q11.Value = (Nz(Me.st_relax_q11.Value, 0) + Nz(Me.q11.Value, 0))
  47. Me.st_q12.Value = (Nz(Me.st_q12.Value, 0) - Nz(Me.q12.Value, 0))
  48. Me.st_relax_q12.Value = (Nz(Me.st_relax_q12.Value, 0) + Nz(Me.q12.Value, 0))
  49. Me.st_q13.Value = (Nz(Me.st_q13.Value, 0) - Nz(Me.q13.Value, 0))
  50. Me.st_relax_q13.Value = (Nz(Me.st_relax_q13.Value, 0) + Nz(Me.q13.Value, 0))
  51. Me.st_q14.Value = (Nz(Me.st_q14.Value, 0) - Nz(Me.q14.Value, 0))
  52. Me.st_relax_q14.Value = (Nz(Me.st_relax_q14.Value, 0) + Nz(Me.q14.Value, 0))
  53. Me.SolderieStock.Value = (Nz(Me.SolderieStock.Value, 0) - Me.Qty.Value)
  54. Me.RelaxStock.Value = (Nz(Me.RelaxStock.Value, 0) + Me.Qty.Value)
  55. End If
  56. End If
  57. DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
  58. Else
  59. MsgBox "Qty entered more than the solderie stock quantity"
  60. Me.Qty.SetFocus
  61. End If
  62.  
Oct 6 '10 #1
Share this Question
Share on Google+
4 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
The problem is at line#19 and line#20. Your code should look like this ...

Expand|Select|Wrap|Line Numbers
  1. If (Qty < SolderieQty Or Qty = SolderieQty) Then
  2.     If (Qty < sumq) Then
  3.         MsgBox "check the Quantity. qty < sumq"
  4.         Me.q1.SetFocus
  5.     End If
  6.  
  7.     If (Qty > sumq) Then
  8.         MsgBox "check the Quantity. qty > sumq"
  9.         Me.q1.SetFocus
  10.     End If
  11.  
  12.     If (Qty = sumq) Then
  13.         'ADD the record into SendDetails table
  14.         If ((Not IsNull(Article)) And (Not IsNull(sentdate)) And (Not IsNull(SendFrom)) And (Not IsNull(SendTo))) Then
  15.             MsgBox "first"
  16.             If (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo))) Then
  17.                   MsgBox "you have sent this article before to the same location"
  18.                   Forms![Transfer]![Article].SetFocus
  19.                   MsgBox "aaaa"
  20.             ElseIf (CheckIfSent(CLng(Forms![Transfer]!Article), CDate(Forms![Transfer]!send_Date), CStr(Forms![Transfer]!SendFrom), CStr(Forms![Transfer]!SendTo)) = False) Then
  21.                 MsgBox "bbbb"
  22.                 InsertIntoSendDetails CDate(sentdate), CStr(SendFrom), CStr(SendTo), CLng(Article), CLng(Qty), CLng(q1), CLng(q2), _
  23.                     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)
  24.  
  25.                 Me.st_q1.Value = (Nz(Me.st_q1.Value, 0) - Nz(Me.q1.Value, 0))
  26.                 Me.st_relax_q1.Value = (Nz(Me.st_relax_q1.Value, 0) + Nz(Me.q1.Value, 0))
  27.                 Me.st_q2.Value = (Nz(Me.st_q2.Value, 0) - Nz(Me.q2.Value, 0))
  28.                 Me.st_relax_q2.Value = (Nz(Me.st_relax_q2.Value, 0) + Nz(Me.q2.Value, 0))
  29.                 Me.st_q3.Value = (Nz(Me.st_q3.Value, 0) - Nz(Me.q3.Value, 0))
  30.                 Me.st_relax_q3.Value = (Nz(Me.st_relax_q3.Value, 0) + Nz(Me.q3.Value, 0))
  31.                 Me.st_q4.Value = (Nz(Me.st_q4.Value, 0) - Nz(Me.q4.Value, 0))
  32.                 Me.st_relax_q4.Value = (Nz(Me.st_relax_q4.Value, 0) + Nz(Me.q4.Value, 0))
  33.                 Me.st_q5.Value = (Nz(Me.st_q5.Value, 0) - Nz(Me.q5.Value, 0))
  34.                 Me.st_relax_q5.Value = (Nz(Me.st_relax_q5.Value, 0) + Nz(Me.q5.Value, 0))
  35.                 Me.st_q6.Value = (Nz(Me.st_q6.Value, 0) - Nz(Me.q6.Value, 0))
  36.                 Me.st_relax_q6.Value = (Nz(Me.st_relax_q6.Value, 0) + Nz(Me.q6.Value, 0))
  37.                 Me.st_q7.Value = (Nz(Me.st_q7.Value, 0) - Nz(Me.q7.Value, 0))
  38.                 Me.st_relax_q7.Value = (Nz(Me.st_relax_q7.Value, 0) + Nz(Me.q7.Value, 0))
  39.                 Me.st_q8.Value = (Nz(Me.st_q8.Value, 0) - Nz(Me.q8.Value, 0))
  40.                 Me.st_relax_q8.Value = (Nz(Me.st_relax_q8.Value, 0) + Nz(Me.q8.Value, 0))
  41.                 Me.st_q9.Value = (Nz(Me.st_q9.Value, 0) - Nz(Me.q9.Value, 0))
  42.                 Me.st_relax_q9.Value = (Nz(Me.st_relax_q9.Value, 0) + Nz(Me.q9.Value, 0))
  43.                 Me.st_q10.Value = (Nz(Me.st_q10.Value, 0) - Nz(Me.q10.Value, 0))
  44.                 Me.st_relax_q10.Value = (Nz(Me.st_relax_q10.Value, 0) + Nz(Me.q10.Value, 0))
  45.                 Me.st_q11.Value = (Nz(Me.st_q11.Value, 0) - Nz(Me.q11.Value, 0))
  46.                 Me.st_relax_q11.Value = (Nz(Me.st_relax_q11.Value, 0) + Nz(Me.q11.Value, 0))
  47.                 Me.st_q12.Value = (Nz(Me.st_q12.Value, 0) - Nz(Me.q12.Value, 0))
  48.                 Me.st_relax_q12.Value = (Nz(Me.st_relax_q12.Value, 0) + Nz(Me.q12.Value, 0))
  49.                 Me.st_q13.Value = (Nz(Me.st_q13.Value, 0) - Nz(Me.q13.Value, 0))
  50.                 Me.st_relax_q13.Value = (Nz(Me.st_relax_q13.Value, 0) + Nz(Me.q13.Value, 0))
  51.                 Me.st_q14.Value = (Nz(Me.st_q14.Value, 0) - Nz(Me.q14.Value, 0))
  52.                 Me.st_relax_q14.Value = (Nz(Me.st_relax_q14.Value, 0) + Nz(Me.q14.Value, 0))
  53.                 Me.SolderieStock.Value = (Nz(Me.SolderieStock.Value, 0) - Me.Qty.Value)
  54.                 Me.RelaxStock.Value = (Nz(Me.RelaxStock.Value, 0) + Me.Qty.Value)
  55.             End If
  56.         End If
  57.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, 5, , acMenuVer70
  58. Else
  59.     MsgBox "Qty entered more than the solderie stock quantity"
  60.     Me.Qty.SetFocus
  61. End If
Oct 6 '10 #2

dsatino
100+
P: 393
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:

Expand|Select|Wrap|Line Numbers
  1. Dim xLoop as Integer
  2.  
And then replace lines 25 through 52 with this:

Expand|Select|Wrap|Line Numbers
  1. For xLoop = 1 to 14
  2.    Me.Controls("st_q" & xLoop).Value = (Nz(Me.Controls("st_q" & xLoop).Value, 0) - Nz(Me.Controls("q" & xLoop).Value, 0)) 
  3.  
  4.    Me.Controls("st_relax_q" & xLoop).Value = (Nz(Me.Controls("st_relax_q" & xLoop).Value, 0) + Nz(Me.Controls("q" & xLoop).Value, 0)) 
  5. Next
  6.  
Oct 7 '10 #3

NeoPa
Expert Mod 15k+
P: 31,419
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.
Oct 7 '10 #4

ADezii
Expert 5K+
P: 8,623
Here is another Version of dsatino's explanation in Post# 3, which you may/may not find easier to understand and implement:
Expand|Select|Wrap|Line Numbers
  1. Dim bytCtrlCtr As Integer
  2. Dim ctl1 As Control
  3. Dim ctl2 As Control
  4. Dim ctl3 As Control
  5.  
  6. For bytCtrlCtr = 1 To 14
  7.   Set ctl1 = Me.Controls("st_q" & CStr(bytCtrlCtr))
  8.   Set ctl2 = Me.Controls("q" & CStr(bytCtrlCtr))
  9.   Set ctl3 = Me.Controls("st_relax_q" & CStr(bytCtrlCtr))
  10.  
  11.   ctl1 = (Nz(ctl1, 0) - Nz(ctl2, 0))
  12.   ctl3 = (Nz(ctl3, 0) + Nz(ctl2, 0))
  13. Next
Oct 7 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.