MS Access SQL Too Few Parameters: Expected 2 - sql

In MS Access 2010, I have the following query which randomly orders the data and puts it in a new sheet. Before I added in the WHERE, it was working, but now I get an error telling me:
Too few parameters: Expected 2.
Does anybody know how I could fix this?
sqlQuery = "SELECT [My_Sheet].* " & _
" INTO My_New_Sheet" & _
" FROM [My_Sheet] " & _
" WHERE [Some_Field] = [Some_Possible_Value_For_The_Field] " & _
" ORDER BY Rnd(-(100000*[Some_Other_Field])*Time())"
Debug.Print sqlQuery
CurrentDb.Execute sqlQuery
Where [Some_Possible_Value_For_The_Field] is comes from [My_Sheet]
Note that this is Access SQL

sqlQuery = "SELECT [My_Sheet].* " & _
" INTO My_New_Sheet" & _
" FROM [My_Sheet] " & _
" WHERE [Some_Field] = '" & [Some_Possible_Value_For_The_Field] & "'" & _
" ORDER BY Rnd(-(100000*" & [Some_Other_Field] & ")*Time())"
Debug.Print sqlQuery
CurrentDb.Execute sqlQuery
When you use a form variable, the value has to be read from outside of the SQL statement. Hence why we close the statement with double quote, add the field value, and then continue by opening the with a double quotes again.
Notice that you need to keep the field qualifiers. In this case I assumed your first field was a string which requires the single quote qualifiers and the second variable as an integer which doesn't require qualifiers.

Related

Access asking me to enter a parameter value

I'm running an SQL query through VB in Microsoft Access for form to add records to a table. However, it keeps asking me to insert parameter value, when they are already present in the form.
Private Sub AddPart_Click()
Dim strSQL As String
strSQL = "INSERT INTO Part VALUES (" & Me.IdPartPrimary.Value & ", " & Me.NamePartPrimary.Value & ", " & Me.BrandPartPrimary.Value & ", " & Me.ModelPartPrimary.Value & ", " & Me.FunctionPartPrimary.Value & ", -1, " & Me.FatherPartPrimary.Value & ", " & Me.ProviderPartPrimary.Value & ", " & Me.AmountPartPrimary.Value & ");"
DoCmd.RunSQL strSQL
End Sub
I already checked for spelling mistakes and there were none. Also, this happens with every field. If I don't insert a parameter value and cancel instead, the record still gets added, only after I close and reopen the table lots of times.
If fields are text type, need text delimiters - apostrophe will serve. I assume comboboxes have a number value from a hidden foreign key column. Value property does not need to be specified as it is the default.
With Me
strSQL = "INSERT INTO Part " & _
" VALUES (" & .IdPartPrimary & ", '" & _
.NamePartPrimary & "', '" & .BrandPartPrimary & "', '" & _
.ModelPartPrimary & "', '" & .FunctionPartPrimary & "', -1, " & _
.FatherPartPrimary & ", " & .ProviderPartPrimary & ", " & .AmountPartPrimary & ")"
End With
AFAIK, Access cannot execute multi-action SQL statements but SQL injection is still possible. If you want to explore use of Parameters in VBA, review How do I use parameters in VBA in the different contexts in Microsoft Access?
Another alternative to avoid SQL injection is to open a recordset, use its AddNew method to create a new record, and set value of each field. DAO.Recordset AddNew

ms-access run-time error 3075 extra ) in vbasql

Code
strSQL = "SELECT tblHS_area_fields.hsaf_id " _
& "FROM tblHS_area_fields " _
& "WHERE (((tblHS_area_fields.hs_area_id)=" & hs_area_id & ") AND ((tblHS_area_fields.hsf_id)=13))"
Set rs = db.OpenRecordset(strSQL)
Errors
The error when trying to run from a Form is:
Extra ) in query expression '(((tblHS_area_fields.hs_area_id)=" &
> hs_area_id & ") AND ((tblHS_area_fields.hsf_id)=13))'
Getting an error from immediate window:
Compile error: expected: line number or label or statement or end of statement
All fields are numbers.
What is wrong with the VBA code and SQL statement?
Just remove all brackets in your sql:
strSQL = "SELECT tblHS_area_fields.hsaf_id " & _
"FROM tblHS_area_fields " & _
"WHERE tblHS_area_fields.hs_area_id = " & hs_area_id & " AND tblHS_area_fields.hsf_id = 13 "
In this case you don't need the brackets.
Not an answer, but too long for a comment:
Next time, add a Debug.Print strSql, then create a query in SQL view, copy you SQL statement from the debug window (ctrl+G) and paste you statement there.
Or just paste it in Visual Studio...
You should then quickly see the issue(s)
Consider to debug-print and log your SQL before executing: Debug.print(strSQL).
Opening brackets must match closing ones
Some break-down helps recognizing and matching, correct is:
strSQL = "SELECT hsaf_id" _
& " FROM tblHS_area_fields" _
& " WHERE (" _
& "( hs_area_id =" & hs_area_id & ")" _
& " AND ( hsf_id = 13 )" _
& ")"
For SQL templates you could also use string replace function or string-templating with a custom-function. See VBA string interpolation syntax.

How to assign SQL MAX(date) to a VBA variable

I am attempting to insert records into a table(cohort) based off of a date(backdate). The backdate variable in VBA should first grab the max date from the SQL query to later be used in the INSERT INTO query in the WHERE clause.
When I attempt to run the function I get a type mismatch error when I try to declare backdate. My code is below.
Dim backdate As Date
Dim sqlString As String
backdate = "SELECT MAX(letter.report_date) " & _
"FROM fadav_letter_recipients as letter ;"
sqlString = "INSERT INTO cohort(person_id, report) " & _
"SELECT letter.person_id, letter.report_type " & _
"FROM fadav_letter_recipients as letter " & _
"WHERE " & backdate & " > letter.report_date;"
DoCmd.RunSQL sqlString
Any help is appreciated
Lookup the value:
backdate = DMax("report_date", "fadav_letter_recipients")
Then format the date value as a date expression:
"WHERE #" & Format(backdate, "yyyy\/mm\/dd") & "# > letter.report_date;"
you get the type mismatch error because you declared backdate as "Date" and you later tried to assign a "string" value to it
There is also a conception error in your code , you need to use a "recordset" object to get what you want
Could you try this code ?:
Dim rec As ado.recordset
Dim sqlString As String
Dim backdate As Date
set rec= currentdb.openrecordset ("SELECT MAX(letter.report_date) " & _
"FROM fadav_letter_recipients as letter ;")
backdate=rec(0)
sqlString = "INSERT INTO cohort(person_id, report) " & _
"SELECT letter.person_id, letter.report_type " & _
"FROM fadav_letter_recipients as letter " & _
"WHERE #" & backdate & "# > letter.report_date;"
DoCmd.RunSQL sqlString

Correctly assigned parameter valuecan not be used

I want to get a cmb-value before the entry is changed, by using ".oldValue". The value is correctly assigned (according to debugger), but running the SQL Access is asking for a manual entry. Doing the entry manually works fine, so the remaining code should be fine.
My Code:
Dim CategoryNameBeforeChange As String
CategoryNameBeforeChange = Forms!frmCategory!txtCategoryName.OldValue
SQL = "UPDATE CategoryTbl " & _
"SET CategoryTbl.CategoryName = Forms!frmCategory!txtCategoryName " & _
"WHERE (CategoryTbl.CategoryName = CategoryNameBeforeChange);"
Any idea what went wrong here?
Any help is greatly appreciated!
Try this:
SQL = "UPDATE CategoryTbl " & _
"SET CategoryTbl.CategoryName = Forms!frmCategory!txtCategoryName " & _
"WHERE (CategoryTbl.CategoryName = " & CategoryNameBeforeChange & ");"
I suspect that CategoryName is text, in this case it should be
SQL = "UPDATE CategoryTbl " & _
"SET CategoryTbl.CategoryName = Forms!frmCategory!txtCategoryName " & _
"WHERE (CategoryTbl.CategoryName = '" & CategoryNameBeforeChange & "');"

data type mismatch error with OpenRecordSet

Using Access 2007. I'm trying to write a VBA function that will construct a query from the table name, fields, and values that I pass as parameters. I keep getting a "Run-time error '3464': Data type mismatch in criteria expression."
Here's the code:
Function getPrimaryFromForeign(db As Database, table As String, field As String, _
value As Long, _
field2 As String, value2 As Long) As Long
Dim sStr As String
Dim istr As String
Dim rs As Recordset
sStr = "select * from " & table & " where " _
& field & "='" & value & "' and " & field2 & "='" & value2 & "'"
istr = "insert into " & table & "(" & _
field & "," & field2 & ") values ('" & value & "','" & value2 & "')"
Set rs = db.OpenRecordset(sStr)
If rs.RecordCount < 1 Then
db.Execute (istr), dbFailOnError
Set rs = db.OpenRecordset(sStr)
End If
getPrimaryFromForeign = rs("id")
End Function
The error occurs at the line:
Set rs = db.OpenRecordset(sStr)
I think it has something to do with the variable types of Value and Value2. But I've checked them using typename(), and they're both Long when OpenRecordSet() is called. The query is on a table where both of those fields are of type Number. So why is there a type mismatch?
This can occur if you have both DAO and an ADO in you reference library.
In this case declaration order in both your Dim statements and in the references window matters.
1) "You must reference and use both DAO and ADO Recordset objects, dimension the objects explicitly as follows:
Dim adoRS As ADODB.Recordset
Dim daoRS As DAO.Recordset"
2) "Make sure that the reference for the DAO object library has a higher priority in the References dialog box, or clear the reference to Microsoft ActiveX Data Objects."
see Here:
https://support.microsoft.com/en-us/help/181542/you-receive-a-type-mismatch-error-when-you-run-the-openrecordset-metho
I haven't used Access in a long time, but I think that the problem is because you're enclosing Value and Value2 in single quotes in your SQL statement, and not enclosing the fields in the WHERE clause in brackets.
Try it like this:
sStr = "select * from " & table & " where [" _
& field & "] = " & value & " and [" & field2 & "] = " & value2
istr = "insert into " & table & "([" & _
field & "], [" & field2 & "]) values (" & value & "," & value2 & ")"