MS Access vba query with recordset too few parameters

I need help debugging this code:
Dim db As DAO.Database
Dim rsGlassing As DAO.Recordset
Dim rsTbl_Time_Temp As DAO.Recordset

Set db = CurrentDb

strSelect = "SELECT DISTINCT Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time AS Gtime  FROM Glassing_Yield  WHERE (((Glassing_Yield.PN) = [Forms]![Glassing]![cbo_part_number]))  GROUP BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time, Glassing_Yield.Furnace, Glassing_Yield.Comments  HAVING (((Glassing_Yield.Etch_Lot) Between [Forms]![Glassing]![cbo_begin_etch] And [Forms]![Glassing]![cbo_end_etch]) AND ((Glassing_Yield.Furnace)=2))  ORDER BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time;"

Set rsGlassing = db.OpenRecordset(strSelect)

it causes the error:  Too few parameters, expected 3.

I've Googled and Googled.  I'm stuck.

I think I understand it is in the statement:
HAVING (((Glassing_Yield.Etch_Lot) Between [Forms]![Glassing]![cbo_begin_etch] And [Forms]![Glassing]![cbo_end_etch]) AND ((Glassing_Yield.Furnace)=2))

Help appreciated.

Thanks,
Brooks
gbnortonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupCommented:
Try this change to your strSelect (note the changes to the form references):


strSelect = "SELECT DISTINCT Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time AS Gtime  FROM Glassing_Yield  WHERE (((Glassing_Yield.PN) = Eval([Forms]![Glassing]![cbo_part_number])))  GROUP BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time, Glassing_Yield.Furnace, Glassing_Yield.Comments  HAVING (((Glassing_Yield.Etch_Lot) Between EVAL([Forms]![Glassing]![cbo_begin_etch])  And EVAL([Forms]![Glassing]![cbo_end_etch])) AND ((Glassing_Yield.Furnace)=2))  ORDER BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time;"

Open in new window

0
mbizupCommented:
If the above doesn't work, using Eval, then try separating your form references out like this (I'm assuming PN and Etch lot are *numeric* data:


strSelect = "SELECT DISTINCT Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time AS Gtime  FROM Glassing_Yield  WHERE (((Glassing_Yield.PN) = " &  [Forms]![Glassing]![cbo_part_number] & "))  GROUP BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time, Glassing_Yield.Furnace, Glassing_Yield.Comments  HAVING (((Glassing_Yield.Etch_Lot) Between " & [Forms]![Glassing]![cbo_begin_etch] & " And " & [Forms]![Glassing]![cbo_end_etch] & ") AND ((Glassing_Yield.Furnace)=2))  ORDER BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time;"

Open in new window


If they are TEXT, you would need single quote delimeters as follows:

strSelect = "SELECT DISTINCT Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time AS Gtime  FROM Glassing_Yield  WHERE (((Glassing_Yield.PN) = '" &  [Forms]![Glassing]![cbo_part_number] & "'))  GROUP BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time, Glassing_Yield.Furnace, Glassing_Yield.Comments  HAVING (((Glassing_Yield.Etch_Lot) Between '" & [Forms]![Glassing]![cbo_begin_etch] & "' And '" & [Forms]![Glassing]![cbo_end_etch] & "') AND ((Glassing_Yield.Furnace)=2))  ORDER BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time, Glassing_Yield.Date_Time;"

Open in new window


If any are dates, you would use hash mark delimiters (#)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gbnortonAuthor Commented:
The text option worked perfectly.  You are my hero.
Thanks,
Brooks
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jeffrey CoachmanMIS LiasonCommented:
1. Odd, you are missing all three of your SQL parameters...
So, ...just to be sure, ....
Is the form that the SQL needs ([Forms]![Glassing]), open when you run the code?

2. I always view any SQL before running/using it (to look for any obvious syntax errors), ...so do this before opening the recordset and see what you get:
    msgbox strSelect
...or use this to view the SQL in the VBA immediate window
   degug.Print strSelect

3. Also try Hard-coding the values (just to see if the SQL is valid)...

JeffCoachman
0
mbizupCommented:
Glad to help!
0
Dale FyeCommented:
gb,

When building these SQL strings in code, you should generally not imbed the reference to the control on the form, you should actually insert the value of that control in the SQL.  That is what mbizup has done in her examples above.  The challenge comes in knowing where to put your breaks and where to embed quotes (or single quotes) within the text.  Although quite simple once you have mastered the technique, properly placing quotes around text (or # around dates) is one of the most common questions I see here on EE (and it can be complicated by the fact that occassionally the text you want to include in your SQL may contain embedded quotes).

I use a function to take some of the complexity out of this:
Public Function fnWrap(WrapWhat as Variant, Optional WrapWith as string = """") as string

    fnWrap = WrapWith & Replace(NZ(WrapWhat, ""), WrapWith, WrapWith & WrapWith) & Wrapwith

End Function

Open in new window

This code uses a quotation mark to wrap the text by default, but will accept a single quote or #, depending on what you want to do with it.  Assuming that your [cbo_part_number] field is a string, and that your [cbo_begin_etch] And [cbo_end_etch] fields are numeric (doesn't look like these are dates), I would do something like the following.  I've left the Comments field out of the Group By because it is not included in the SELECT clause and comments are usually memo fields and will slow down your query performance.  I've also removed the DISTINCT clause as that is is redundant with the Group By clause.  I'm also now quite sure why you are repeating the [Date_Time] field in your SELECT clause (just my thoughts)
strSelect = "SELECT Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time" _
                & ", Glassing_Yield.Date_Time AS Gtime  " _
          & "FROM Glassing_Yield  " _
          & "WHERE Glassing_Yield.PN = " & fnWarp([Forms]![Glassing]![cbo_part_number]) _
          & " GROUP BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time" _
                    & ", Glassing_Yield.Date_Time, Glassing_Yield.Furnace " _
          & "HAVING Glassing_Yield.Etch_Lot Between " & [Forms]![Glassing]![cbo_begin_etch] _
          & " AND " & [Forms]![Glassing]![cbo_end_etch] " _
          & " AND Glassing_Yield.Furnace=2 " _
          & "ORDER BY Glassing_Yield.Etch_Lot, Glassing_Yield.Date_Time" _
                    & ",Glassing_Yield.Date_Time;"

Open in new window

I also suspect that the query would run quicker if you moved the HAVING clause into the WHERE clause but am uncertain.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.