Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Statement Invalid Outside Type Block
Damion Brown 
    
2 years ago
I have two List box that I want to use to filter a query that is already created but keep running into errors, most recently "Statement invalid outside Type block". I got it to work for a form but not the actualy query. Anyone able to assist? Much appreciated.

Details   Dim Qry_Def As QueryDef
   Dim DB As Database
   Dim P As String
   L As String
   PL As String 'Use for SQL statement
   X As Variant
    
    
    'Parameter selection starts off as Blank in listbox
     P = ""
    For Each X In Lst_Parameter.ItemsSelected
    If P <> "" Then P = P & ", "
    P = P & """" & Lst_Parameter.ItemData(X) & """"
    Next
    L = ""
    For Each X In Lst_Location.ItemsSelected
    If L <> "" Then L = L & ", "
    L = L & """" & Lst_Location.ItemData(X) & """"
    Next
    If P = "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults"
    Else
    If P = "" And L <> "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE LocationID IN (" & L & ")"
    Else
    If P <> "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ")"
    Else
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ") AND LocationID IN (" & L & ")"
    End If
    End If
    End If
    
   Set DB = CurrentDb()
    Set Qry_Def = DB.Query.Defs("Qry_SamplesResults")
    Qry_Def.SQL = PL
    Qry_Def.Close
    DoCmd.OpenQuery "Qry_SamplesResults", acViewNormal, acEdit

Damion Brown OP  @Reply  
    
2 years ago
Just realized that I didn't add "Dim" to some of the variables. But that alone doesn't fix the issue.
Kevin Robertson  @Reply  
          
2 years ago
The only thing jumping out at me is the If block.
Consider using ElseIf

    If P = "" And L = "" Then
        PL = "SELECT * FROM Qry_SamplesResults"
    ElseIf P = "" And L <> "" Then
        PL = "SELECT * FROM Qry_SamplesResults WHERE LocationID IN (" & L & ")"
    ElseIf P <> "" And L = "" Then
        PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ")"
    Else
        PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ") AND LocationID IN (" & L & ")"
    End If


If Then
Richard Rost  @Reply  
          
2 years ago
Indenting is important. Makes it much easier to see problems. But in your case:

L As String

Is not a valid command. You either need the DIM before it, or move it to the end of another line, like

Dim X as String, L as String

Richard Rost  @Reply  
          
2 years ago
Also

DB.Query.Defs

Should be

DB.QueryDefs
Damion Brown OP  @Reply  
    
2 years ago
Thanks Richard and Steve, I made both sets of corrections. Richard, your code worked amazing to create the form filter, I was trying to see how to modify it as a query criteria. Defintely going to purchase the more advance courses as I go. Now that I have made the changes above, I get run-tim error: Circular reference based by 'Qry_SamplesResults'. Feels so close!

   Dim Qry_Def As QueryDef
   Dim DB As Database
   Dim P As String
   Dim L As String
   Dim PL As String
   Dim X As Variant
    P = ""
   For Each X In Lst_Parameter.ItemsSelected
    
    If P <> "" Then P = P & ", "
    P = P & """" & Lst_Parameter.ItemData(X) & """"
    Next
    L = ""
    For Each X In Lst_Location.ItemsSelected
    If L <> "" Then L = L & ", "
    L = L & """" & Lst_Location.ItemData(X) & """"
    Next
    If P = "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults"
    ElseIf P = "" And L <> "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE LocationID IN (" & L & ")"
    ElseIf P <> "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ")"
    Else
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ") AND LocationID IN (" & L & ")"
    End If
            
    Set DB = CurrentDb()
    Set Qry_Def = DB.QueryDefs("Qry_SamplesResults")
    Qry_Def.SQL = PL
    Qry_Def.Close
    DoCmd.OpenQuery "Qry_SamplesResults", acViewNormal, acEdit
John Davy  @Reply  
         
2 years ago
Hi Damion
I recommend that you try indenting your code. It will help you in the long run.

John
Damion Brown OP  @Reply  
    
2 years ago
Thanks, John. I will look into practicing good database fundamentals. Any thoughts on how I can address the circular reference error?
Richard Rost  @Reply  
          
2 years ago
It looks like you're on the right track, but the circular reference error may be occurring because you're trying to modify the same query (Qry_SamplesResults) that you're also using as a data source. This can create conflicts in Access, especially when the query is both modified and executed in the same procedure. One way to avoid this is by using a temporary query or a new query for the modified criteria, instead of directly modifying Qry_SamplesResults. Here's a possible approach to resolve the issue:

DetailsDim Qry_Def As QueryDef
Dim DB As Database
Dim P As String
Dim L As String
Dim PL As String
Dim X As Variant
P = ""

' Build the Parameter string
For Each X In Lst_Parameter.ItemsSelected
    If P <> "" Then P = P & ", "
    P = P & """" & Lst_Parameter.ItemData(X) & """"
Next

' Build the Location string
L = ""
For Each X In Lst_Location.ItemsSelected
    If L <> "" Then L = L & ", "
    L = L & """" & Lst_Location.ItemData(X) & """"
Next

' Build the SQL string based on selected values
If P = "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults"
ElseIf P = "" And L <> "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE LocationID IN (" & L & ")"
ElseIf P <> "" And L = "" Then
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ")"
Else
    PL = "SELECT * FROM Qry_SamplesResults WHERE Parameter IN (" & P & ") AND LocationID IN (" & L & ")"
End If

Set DB = CurrentDb()

' Create a new temporary query to avoid circular reference
On Error Resume Next
DB.QueryDefs.Delete "Qry_TempSamplesResults"
On Error GoTo 0

Set Qry_Def = DB.CreateQueryDef("Qry_TempSamplesResults", PL)

DoCmd.OpenQuery "Qry_TempSamplesResults", acViewNormal, acEdit



And note the proper indentation. MUCH easier to read.
Damion Brown OP  @Reply  
    
2 years ago
This worked out great!

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 8/8/2026 3:03:47 AM. PLT: 1s