Error SQL in VBA Access 2010 - sql

I'm trying to write a query in MS Access 2010 in order to use it to print a report, but it gives me "missing parameter" error in "set qd" line, hereunder is the code i wrote, can you please help me and tell me what is wrong with my code:
`Private Sub Command5_Click()
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSql As String
Dim strFrom, strTo As String
strFrom = [Forms]![FrmPrintSelection]![txtFrom]
strTo = [Forms]![FrmPrintSelection]![txtTo]
strSql = "SELECT tblInvoiceHead.CustomerNumber,
tblCustomers.AccountName,tblCustomers.Address,
tblCustomers.Phone1, tblCustomers.Phone2," _
& "tblCustomers.Mobile1, tblCustomers.Mobile2, tblInvoiceHead.InvoiceNumber,
tblInvoiceHead.InvoiceDate, tblInvoiceHead.TotalInvoice," _
& "tblInvoiceHead.CashDiscount, TblInvoiceDetails.Item, TblInvoiceDetails.Unit,
TblInvoiceDetails.Qtn, TblInvoiceDetails.Price," _
& "TblInvoiceDetails.[Discount%], TblInvoiceDetails.CashDiscount,
TblInvoiceDetails.NetUnitPrice, TblInvoiceDetails.TotalPrice, tblInvoiceHead.InvoiceType" _
& "FROM (tblCustomers INNER JOIN tblInvoiceHead ON tblCustomers.AccountNumber =
tblInvoiceHead.CustomerNumber) INNER JOIN TblInvoiceDetails" _
& "ON tblInvoiceHead.InvoiceNumber = TblInvoiceDetails.InvoiceNumber" _
& "WHERE (((tblInvoiceHead.InvoiceNumber) Between " & strFrom & " And " & strTo & "))"
Set qd = CurrentDb.CreateQueryDef("RepInv", strSql)
Set rs = qd.OpenRecordset
'DoCmd.OpenQuery "repinv", strSql
Reports!repinvoicetest.RecordSource = "repinv"
DoCmd.OpenReport "repinvoicetest", acViewPreview
End Sub
`

Usually the error "missing parameter" means that you spelled one of your columns wrong. If you take your sql and paste it into a new query (temp, don't save) and run it, the misspelled column will pop up a window asking you to provide a value for that "parameter" (because MSAccess is assuming that you never would misspell a column name).
In your query above, you might have copy/pasted it wrong, but if not, then you don't have enough spaces between your words as you continue them on the next line. For instance, your SQL string would end up having some stuff in it like "InvoiceTypeFROM", because you didn't have an extra (necessary) space in there.
Try this query instead:
strSql = "SELECT tblInvoiceHead.CustomerNumber, " _
& " tblCustomers.AccountName,tblCustomers.Address, " _
& " tblCustomers.Phone1, tblCustomers.Phone2, " _
& " tblCustomers.Mobile1, tblCustomers.Mobile2, tblInvoiceHead.InvoiceNumber, " _
& " tblInvoiceHead.InvoiceDate, tblInvoiceHead.TotalInvoice, " _
& " tblInvoiceHead.CashDiscount, TblInvoiceDetails.Item, TblInvoiceDetails.Unit, " _
& " TblInvoiceDetails.Qtn, TblInvoiceDetails.Price, " _
& " TblInvoiceDetails.[Discount%], TblInvoiceDetails.CashDiscount, " _
& " TblInvoiceDetails.NetUnitPrice, TblInvoiceDetails.TotalPrice, " _
& " tblInvoiceHead.InvoiceType " _
& " FROM (tblCustomers INNER JOIN tblInvoiceHead " _
& " ON tblCustomers.AccountNumber = tblInvoiceHead.CustomerNumber) " _
& " INNER JOIN TblInvoiceDetails " _
& " ON tblInvoiceHead.InvoiceNumber = TblInvoiceDetails.InvoiceNumber " _
& " WHERE (((tblInvoiceHead.InvoiceNumber) Between " & strFrom & " And " & strTo & "))"
Notice how I added a lot of unncessary spaces at the begining and end of each line. All of those extra spaces will be ignored. However, if there are too few, then you will get errors. It is a simple trick that I stick-with.

Related

Error executing a dynamic query with inner join (VBA) to dynamic tables

I'm sorry for my English ;)
I have consulted a lot on the internet but I cannot find the solution
I must create a dynamic query, with a function.
This code works for me if the tables are not linked, but are in the same bbdd ACCESS 2016.
But I need them to be in another bbdd.
Can you help me?
It returns me that data is missing.
The query is made in access and modified in vba, adding the variables.
Ano and Trimestre are numerical, the others are text.
Public Function PRUEBA_INNER(ByVal TRIMESTRE As String,ByVal ANO As String) As Boolean
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim StrSQL As String
Set db = CurrentDb
StrSQL = "SELECT " & _
"PERSONAS.NOMBRE, " & _
"PERSONAS.MAIL, " & _
"PERSONAS.[NUMR_COLEG], " & _
"DATOS_" & ANO & ".ANO, " & _
"DATOS_" & ANO & ".TRIMESTRE " & _
"FROM " & _
"PERSONAS " & _
"INNER JOIN DATOS_" & ANO & " " & _
"ON PERSONAS.[NUMR_COLEG] = DATOS_" & ANO & ".NUMR_COLEG " & _
"GROUP BY " & _
"PERSONAS.NOMBRE, " & _
"PERSONAS.MAIL, " & _
"PERSONAS.[NUMR_COLEG], " & _
"DATOS_" & ANO & ".ANO, " & _
"DATOS_" & ANO & ".TRIMESTRE " & _
"HAVING (((DATOS_" & ANO & ".ANO)=" & ANO & ")" & _
" AND ((DATOS_" & ANO & ".TRIMESTRE)=" & TRIMESTRE & "))"
Set rs = db.OpenRecordset(StrSQL)
Do Until rs.EOF
debug.print rs!nombre
rs.MoveNext
Loop
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing
End Function
Debug.print:
SELECT
PERSONAS.NOMBRE,
PERSONAS.MAIL,
PERSONAS.[NUMR_COLEG],
DATOS_2018.ANO,
DATOS_2018.TRIMESTRE
FROM
PERSONAS
INNER JOIN
DATOS_2018
ON
PERSONAS.[NUMR_COLEG] = DATOS_2018.NUMR_COLEG
GROUP BY
PERSONAS.NOMBRE,
PERSONAS.MAIL,
PERSONAS.[NUMR_COLEG],
DATOS_2018.ANO,
DATOS_2018.TRIMESTRE
HAVING (((DATOS_2018.ANO)=2018) AND ((DATOS_2018.TRIMESTRE)=2))
Error message:
nº 3061
Few parameters.
1 expected

SQL UPDATE function syntax error

I am attempting to write an UPDATE function based on values that will be input into dropdown boxes and text boxes by a user
My code is as follows:
Private Sub cmdUpdate_Prices_Click()
Dim SQL_string As String
SQL_string = _
"UPDATE tblPrice " & _
"SET [P/N] = " & Me.txtPartNumber.Column(0) & ", " & _
"Type = " & Me.txtPriceType.Column(0) & ", " & _
"Region = " & Me.txtRegion.Column(0) & ", " & _
"Price = " & Me.txtPrice.Value & ", " & _
"WHERE ID = " & Me.txtID.Value & ";"
DoCmd.RunSQL SQL_string
End Sub
I keep getting a syntax error in the UPDATE function and I'm not sure where it is.
Any help is appreciated. Thanks!

vba sql statement error "missing operator"

I can't get it work in VBA althought it works within access.
Which is the mistake?
Private Sub Command517_Click()
Dim SQL As String
SQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) = [Forms]![MacroAttività]![Text511]) " & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)=[Forms]![MacroAttività]![Text513])); "
DoCmd.RunSQL SQL
End Sub
Form variables have to be read from outside of the SQL statement, so my solution closes the SQL statement adds the form variable and then reopens the SQL quotation. I have added string qualifiers(single quotes) around the form variables; assuming they are strings by the use of "text".
varSQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) ='" & [Forms]![MacroAttività]![Text511] & "')" & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)='" & [Forms]![MacroAttività]![Text513] & "')); "
SQL is a reserved word so change your SQL variable name to something like varSQL. (Update: not a reserved word)
currentdb.execute and domcd.runSQL only work for action queries as far as I am aware. you cant execute a select query using these methods. try the following:
dim rs as recordset
dim varSQL as string
varSQL = "SELECT..."
set rs=currentdb.openrecordset(varSQL,dbOpenDynaset)
me.[text1] = rs.fieldname
'send recordset values to form
rs.close

item cannot be found in the collection corresponding to the requested name or ordinal

My code gives the following error. How can I correct this?
Item cannot be found in the collection corresponding to the requested name or ordinal
ElseIf Me.chkItem.Checked = True Then
Dim CheckNumber As String = ""
Dim CheckRef As String = ""
dsvoucheritem.Clear()
DSVoucher_Expense.Clear()
DSVoucher_Check.Clear()
Try
Me.lstCV.Items.Clear()
strDiscount = Nothing
rec.Open("select billpaymentcheckline.txnnumber, billpaymentcheckline.txndate" _
& ", billpaymentcheckline.payeeentityreffullname" _
& ", billpaymentcheckline.amount, billitemline.itemlineitemreffullname" _
& ", billitemline.memo" _
& ", billpaymentcheckline.appliedtotxndiscountamount" _
& ", billpaymentcheckline.appliedtotxnrefnumber, billpaymentcheckline.bankaccountreffullname" _
& ", billpaymentcheckline.appliedtotxndiscountaccountreffullname" _
& ", billpaymentcheckline.appliedtotxntxndate, billpaymentcheckline.appliedtotxnamount" _
& ", billpaymentcheckline.refnumber, account.AccountNumber from (billitemline inner join" _
& " billpaymentcheckline on billitemline.refnumber=billpaymentcheckline.appliedtotxnrefnumber) left outer join" _
& " account on billitemline.APAccountreflistid=account.listid where" _
& " billpaymentcheckline.bankaccountreflistid='" &Me.lblBankID.Text & "' and" _
& " billpaymentcheckline.refnumber between '" & CInt(Me.txtRefFR.Text)
& "' and '" & CInt(Me.txtRefTO.Text) & "'", con, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
It's saying it doesn't recognize one of your column names. Double check all of them. You can also try removing fields until you find the culprit.
memo is a reserved word in Access SQL.
Try billitemline.[memo]

Update function with case SQL and VBA

I want to try to have a SQL function to update mine table and put a date in the column, I'm using a update function with a case, but I get the error that an operator is missing.
but I can't find the error, does anybody know where it is?
Public Function Add_date( _
ByVal startDate As String, _
ByVal strTableName As String, _
ByVal strFieldName As String, _
ByVal strNummeringField As String) _
As Boolean
Dim strSql As String
strSql = "ALTER TABLE " & strTableName & " ADD " & strFieldName & " date"
DoCmd.RunSQL strSql
strSql = "UPDATE " & strTableName & " SET " & strFieldName & " = CASE WHEN " & strNummeringField & " < 25 THEN '23-07-1991' ELSE '01-01-01' END"
MsgBox strSql
DoCmd.RunSQL strSql
End Function
Jet/ACE (the MS Access db engine) does not support CASE...WHEN. The equivalent for ternary operations is IIF (immediate if). Also, date delimiters are #, not '. Try this instead:
strSql = " UPDATE " & strTableName & _
" SET " & strFieldName & " = " & _
" IIf(" & strNummeringField & " < 25, #23-07-1991#, #01-01-01#)"
Also, you may run into trouble formatting your dates as DD-MM-YYYY, regardless of your regional settings. See International Dates in Access for more information.
One possibility is that the strings representing the table and column name contain invalid characters. Try enclosing them in square brackets:
strSql = "ALTER TABLE [" & strTableName & "] ADD [" & strFieldName & "] date"
DoCmd.RunSQL strSql
strSql = "UPDATE [" & strTableName & "] SET [" & strFieldName & "] = CASE WHEN [" & strNummeringField & "] < 25 THEN '23-07-1991' ELSE '01-01-01' END"