473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Compile Error: Statement Invalid Outside Type Block

I am trying to run a module from a button on a form where I populate a subform from Query1 with a product formula. The module is supposed to check whether the product formula can be implemented given enough raw material inventory. So, I'm trying to loop through my product formula based on OrderID and MaterialID(Material Name) and check against the current stock inventory table. If any of the ingredients required for the product to be made is over the available stock inventory on hand, then I want an error to be displayed, otherwise I don't want any errors.

The problem I'm having is with the Compile Error at the line where it states:

Expand|Select|Wrap|Line Numbers
  1.  Sub check_quantity () 
I'd appreciate any help possible, THANKS!!

The entire code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Sub check_quantity()
  5. On Error GoTo err_check_quantity
  6.  
  7. Dim material As Variant
  8. Dim totalKg As Variant
  9. queryString As String
  10. queryString2 As String
  11. Quantity As Variant
  12. valid As Integer
  13. strMsg As String
  14. Dim Rs As DAO.Recordset
  15. Dim Rs2 As DAO.Recordset
  16. Dim Db As DAO.Database
  17.  
  18.  
  19. Set Db = CurrentDb()
  20.  
  21. queryString = "SELECT * FROM Query1 WHERE [ProductName] = '" & [Forms]![Batchsheet Subform]![ProductName] & "' AND [OrderID] = " & [Forms]![Batchsheet Subform]![OrderID] & ";"
  22.  
  23. Set Rs = Db.OpenRecordset(queryString, dbOpenDynaset)
  24.  
  25. If Not (Rs.BOF And Rs.EOF) Then
  26. Rs.MoveLast
  27. Rs.MoveFirst
  28.  
  29. With Rs
  30. Do While Not Rs.EOF
  31.  
  32. material = Rs.Fields("MaterialID").Value
  33. totalKg = Rs.Fields("Total (kg)").Value
  34.  
  35. queryString2 = "SELECT * FROM RawMaterials WHERE [ItemName] = '" & material & ";"
  36.  
  37. Set Rs2 = Db.OpenRecordset(queryString2, dbOpenDynaset)
  38.  
  39. If Not (Rs2.BOF And Rs2.EOF) Then
  40. Rs2.MoveLast
  41. Rs2.MoveFirst
  42.  
  43.  
  44.   Quantity = Rs2.Fields("InStock").Value
  45.  
  46.  
  47.      If totalKg > Quantity Then
  48.         valid = 0
  49.      Else
  50.         valid = 1
  51.      End If
  52.  
  53.      Select Case valid
  54.  
  55.      Case 0
  56.             strMsg = " Order cannot currently be completed" & _
  57.                         vbCrLf & " Please verify you have enough " & material & "inventory to complete this order."
  58.             MsgBox strMsg, vbInformation, "INVALID Raw Material Level"
  59.  
  60.      Case 1
  61.             Rs.MoveNext
  62.  
  63.      End Select
  64.  
  65. Loop
  66. End With
  67.  
  68.  
  69.    Rs.Close
  70.    Rs2.Close
  71.    Db.Close
  72.  
  73.    Set Rs = Nothing
  74.    Set Rs2 = Nothing
  75.    Set Db = Nothing
  76.  
  77. exit_check_quantity:
  78.     Exit Sub
  79.  
  80. err_check_quantity:
  81.     MsgBox Err.decsription
  82.     Resume exit_check_quantity
  83.  
  84. End Sub
  85.  
  86.  
Jul 21 '10 #1

✓ answered by NeoPa

NeoPa: Your Do...Loops, and If...End Ifs, and With...End Withs do not balance up correctly.

You need to get these sorted before continuing, and certainly before it will work.

Welcome to Bytes!
I would add :
  1. Line #75 closes CurrentDB. Not a good idea. If your code opens an object then it should close it. If it finds it already open, then it should leave it open. There may be exceptions to this rule, but they're rare.
  2. Line #81 is probably the offending property. You want Err.Description.
    Remember next time to include the line number though please.

9 8125
Ok, it looks like I forgot some Dim's. Fixed that but now my new error is "Loop without Do" Hmmm, I see a Do, but maybe I'm not closing or opening the loop properly?
Jul 21 '10 #2
NeoPa
32,556 Expert Mod 16PB
Your Do...Loops, and If...End Ifs, and With...End Withs do not balance up correctly.

You need to get these sorted before continuing, and certainly before it will work.

Welcome to Bytes!
Jul 21 '10 #3
Ok, looks like I've fixed the loop problems. Now, I am getting a message box popup that states, "Object Doesn't Support This Property or Method".

I'm guessing the problem is either with a type mismatch where I may be comparing a string to a number or vice versa. Or maybe somehow the fact that I am retrieving values from a Query is causing the problem?

Can anyone offer any insight?

The feedback so far is really appreciated.

Thanks!

Updated Code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Sub check_quantity()
  5. On Error GoTo err_check_quantity
  6.  
  7. Dim material As String
  8. Dim totalKg As Variant
  9. Dim queryString As String
  10. Dim queryString2 As String
  11. Dim Quantity As Variant
  12. Dim valid As Integer
  13. Dim strMsg As String
  14. Dim Rs As DAO.Recordset
  15. Dim Rs2 As DAO.Recordset
  16. Dim Db As DAO.Database
  17.  
  18. valid = 1
  19.  
  20. Set Db = CurrentDb()
  21.  
  22. queryString = "SELECT * FROM Query1 WHERE [ProductName] = '" & [Forms]![Batchsheet Subform]![ProductName] & "' AND [OrderID] = " & [Forms]![Batchsheet Subform]![OrderID] & ";"
  23.  
  24. Set Rs = Db.OpenRecordset(queryString, dbOpenDynaset)
  25.  
  26. If Not (Rs.BOF And Rs.EOF) Then
  27. Rs.MoveLast
  28. Rs.MoveFirst
  29.  
  30. With Rs
  31. Do While Not Rs.EOF
  32.  
  33. material = Rs.Fields("MaterialID").Value
  34. totalKg = Rs.Fields("Total (kg)").Value
  35.  
  36. queryString2 = "SELECT * FROM RawMaterials WHERE [ItemName] = '" & material & ";"
  37.  
  38. Set Rs2 = Db.OpenRecordset(queryString2, dbOpenDynaset)
  39.  
  40. If Not (Rs2.BOF And Rs2.EOF) Then
  41. Rs2.MoveLast
  42. Rs2.MoveFirst
  43.  
  44.   Quantity = Rs2.Fields("InStock").Value
  45.  
  46.  
  47.      If totalKg > Quantity Then
  48.         valid = 0
  49.      Else
  50.         valid = 1
  51.      End If
  52.  
  53.        Select Case valid
  54.  
  55.        Case 0
  56.               strMsg = " Order cannot currently be completed" & _
  57.                         vbCrLf & " Please verify you have enough " & material & "inventory to complete this order."
  58.               MsgBox strMsg, vbInformation, "INVALID Raw Material Level"
  59.               Exit Do
  60.  
  61.        Case 1
  62.               Rs.MoveNext
  63.  
  64.        End Select
  65.  
  66. End If
  67.  
  68. Loop
  69. End With
  70.  
  71. End If
  72.  
  73.    Rs.Close
  74.    Rs2.Close
  75.    Db.Close
  76.  
  77.    Set Rs = Nothing
  78.    Set Rs2 = Nothing
  79.    Set Db = Nothing
  80.  
  81. exit_check_quantity:
  82.     Exit Sub
  83.  
  84. err_check_quantity:
  85.     MsgBox Err.decsription
  86.     Resume exit_check_quantity
  87.  
  88. End Sub
  89.  
Jul 21 '10 #4
NeoPa
32,556 Expert Mod 16PB
The error message is good. Giving the line the error occurred on would also be a great help. The code box helps by numbering each line for you.
Jul 21 '10 #5
NeoPa
32,556 Expert Mod 16PB
NeoPa: Your Do...Loops, and If...End Ifs, and With...End Withs do not balance up correctly.

You need to get these sorted before continuing, and certainly before it will work.

Welcome to Bytes!
I would add :
  1. Line #75 closes CurrentDB. Not a good idea. If your code opens an object then it should close it. If it finds it already open, then it should leave it open. There may be exceptions to this rule, but they're rare.
  2. Line #81 is probably the offending property. You want Err.Description.
    Remember next time to include the line number though please.
Jul 21 '10 #6
Thanks NeoPa,

Unfortunately my error message didn't provide a line number, otherwise I would have definitely included it. Let me try your suggestions and get back to you.

Thanks so much for the help so far.
Jul 21 '10 #7
Ok, I corrected the typo with description but yet again I have another error. I'm getting an error that states: "Microsoft Office Access can't find the form 'BatchsheetSubform' referred to in a macro expression or Visual Basic code." Again, no error number or line number, but it's obvious which line is the offending one here.

I changed the name of the Batchsheet subform to BatchsheetSubform thinking that the space was causing a problem because VB was automagically using an uppercase 'S' for Subform. And my actual form uses a lower case 's'. Now I've just renamed the form to 'BatchsheetSubform both in VB and in the actual form.

I also added .[Form] to reference the control in the subform since I believe a subform is a control in itself and needs a .Form to access it's controls. I also did away with Db.close but that didn't help.

Updated Code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Sub check_quantity()
  5. On Error GoTo err_check_quantity
  6.  
  7. Dim material As String
  8. Dim totalKg As Variant
  9. Dim queryString As String
  10. Dim queryString2 As String
  11. Dim Quantity As Variant
  12. Dim valid As Integer
  13. Dim strMsg As String
  14. Dim Rs As DAO.Recordset
  15. Dim Rs2 As DAO.Recordset
  16. Dim Db As DAO.Database
  17.  
  18. valid = 1
  19.  
  20. Set Db = CurrentDb()
  21.  
  22. queryString = "SELECT * FROM Query1 WHERE [ProductName] = '" & [Forms]![BatchsheetSubform]![Form]![ProductName] & "' AND [OrderID] = " & [Forms]![Batchsheet Subform]![OrderID] & ";"
  23.  
  24. Set Rs = Db.OpenRecordset(queryString, dbOpenDynaset)
  25.  
  26. If Not (Rs.BOF And Rs.EOF) Then
  27. Rs.MoveLast
  28. Rs.MoveFirst
  29.  
  30. With Rs
  31. Do While Not Rs.EOF
  32.  
  33. material = Rs.Fields("MaterialID").Value
  34. totalKg = Rs.Fields("Total (kg)").Value
  35.  
  36. queryString2 = "SELECT * FROM RawMaterials WHERE [ItemName] = '" & material & ";"
  37.  
  38. Set Rs2 = Db.OpenRecordset(queryString2, dbOpenDynaset)
  39.  
  40. If Not (Rs2.BOF And Rs2.EOF) Then
  41. Rs2.MoveLast
  42. Rs2.MoveFirst
  43.  
  44.   Quantity = Rs2.Fields("InStock").Value
  45.  
  46.  
  47.      If totalKg > Quantity Then
  48.         valid = 0
  49.      Else
  50.         valid = 1
  51.      End If
  52.  
  53.        Select Case valid
  54.  
  55.        Case 0
  56.               strMsg = " Order cannot currently be completed" & _
  57.                         vbCrLf & " Please verify you have enough " & material & "inventory to complete this order."
  58.               MsgBox strMsg, vbInformation, "INVALID Raw Material Level"
  59.               Exit Do
  60.  
  61.        Case 1
  62.               Rs.MoveNext
  63.  
  64.        End Select
  65.  
  66. End If
  67.  
  68. Loop
  69. End With
  70.  
  71. End If
  72.  
  73.    Rs.Close
  74.    Rs2.Close
  75.    Db.Close
  76.  
  77.    Set Rs = Nothing
  78.    Set Rs2 = Nothing
  79.    Set Db = Nothing
  80.  
  81. exit_check_quantity:
  82.     Exit Sub
  83.  
  84. err_check_quantity:
  85.     MsgBox Err.Description
  86.     Resume exit_check_quantity
  87.  
  88. End Sub
  89.  
Jul 21 '10 #8
Ahhh, ok I had to make a few changes but I got it to work!! Thanks a lot for guiding me in the right direction. Really appreciate it!
Jul 21 '10 #9
NeoPa
32,556 Expert Mod 16PB
Well done for finding and fixing all the errors Amit.

I agree with your earlier comment. If no line number is reported, then you cannot pass it on. Always do so when you can though :)
Jul 22 '10 #10

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

Similar topics

5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
3
by: Peter | last post by:
Hi, I am trying to compile an existing project (originally c) in .NET (rename .c files to .cpp). After fixing some problems, here are the ones that I don't know how to deal with:...
0
by: Michael D. Reed | last post by:
I have a C++ program that I am trying to get to compile and link in VS2003. It is an old windows program that was built and maintained with older versions of VS and VC++. I made the necessary...
9
by: David | last post by:
I am getting the following error during run-time compilation or a rebuild all of my application: error BC30311: Value of type 'mynamespace.x' cannot be converted to 'mynamespace.x' This...
2
by: vfunc | last post by:
I get a compile error at line 285 of this demo error converting from void* to pollfd* Do I need to set some compiler flag ? I did not write this so I'm reluctant to change anything. 251...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
4
by: Light1 | last post by:
My form (frmERLogs) for this project (ER Log Book) has a combo box (cboMedicalRecordNumber) that I want to be able to double click on to bring up a new form (frmPatientInformation) when a name is not...
17
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
15
by: squrel | last post by:
Hello All.. I m having some problem here.. i m using VB6 and SQLServer 2000... i m getting this error " statement invalid outside type block" i m sending u the code and the bold line is where i m...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.