Access VBA error "Too few parameters. Expected 1" - sql

I'm trying to find out why this is wrong, while in the queries generator it works properly.
SELECT Count(Audi.Id) AS CuentaDeId FROM Audi
WHERE (((Len." & filtro & ") Between #" & Format(Me!fechamin, "mm/dd/yyyy") & "# And #" & Format(Me!fechamax, "mm/dd/yyyy") & "#))
With some dates slightly different It works, but I think that when It does not find any value in this Table, the error appears.
No idea if it's a problem of the Query design, or if there is another way to define it, or not. Any clue, anyone?
Thanks in advance!

Len is a function name in SQL. That is why Access asked for the parameter. Besides, as Parfait said, Len is used in your query as an alias but not referring to any other table/query.

Related

Why is my update query passing incorrect information to the table?

I have an SQL statement in VBA that when i run it, it updates my table with incorrect information. I've been struggling with this code for over a week trying workarounds and debugging but to no avail. I've searched online and found nothing even close to this.
DIM SQL as String
DIM periodStart as Date
DIM periodEnd as Date
periodStart = DateSerial(Year(Date), 12, 1)
periodEnd = DateSerial(Year(Date), 12, 15)
MsgBox "Period Start: " & periodStart & " Period End: " & periodEnd
SQL = "UPDATE EmpTime SET EmpTime.beginning = " & periodStart & " & EmpTime.ending = " & periodEnd & ";"
DoCmd.RunSQL SQL
The above code gives me a message box that shows me the periodStart and periodEnd variables are being built properly but then when i look to the table, the information is not the same as the Message box.
MsgBox
Table
Why is this happening and what can I do to fix it/avoid it ?
What I think is happening here is that your SQL is shaking out to be:
UPDATE EmpTime SET EmpTime.beginning = 12/1/2019, EmpTime.ending = 12/15/2019;
Access is not super amazing at guessing your intentions when you just send it math problems like this. Because it doesn't recognize your first date as a properly formatted string (12/01/2019 would be more appropriate) it is making the educated guess that you literally wanted to divide 12 by 1 by 2019. Which results in a decimal, or a very early time of the first date that MS Access can record: 12/30/1899 (like 12:05am, but there is no time dimension in play so it's dropped).
Instead try:
UPDATE EmpTime SET EmpTime.beginning = #" & Format(periodStart, "mm/dd/yyyy") & "# & EmpTime.ending = #" & Format(periodEnd, "mm/dd/yyyy") & "#;"
This does two things:
It formats (using the Format() function) your date into something access will recognize on its own.
It surrounds the date in # which is the microsoft office-y way of saying "This is explicitly a date, treat it as such or throw an error". Which is a much better scenario then "Guess what I meant when I send you this math/date"
Lastly, as Gordon mentions, and I also HIGHLY recommend is to switch this code over to use parameterized inputs in your SQL. here is a good write up of what that looks like. This solves two issues in your current code
Your malformed date would most likely error on being assigned to the correctly typed parameter before the SQL was executed alerting you that you have a bad date. (no guessing what went wrong and no bad data hitting your database)
You are protected from SQL Injection by users of your workbook. I assume this is not a super important facet of your workbook/application though since this is probably an internal company or personal thing and everyone using it can be trusted, but I am always in favor of hardening your code as best as possible since it's just good practice.
SQL = "UPDATE EmpTime SET EmpTime.beginning = #" & periodStart & "#, EmpTime.ending = #" & periodEnd & "#"

VBA MACRO Make Variable Range

I really struggled in making this work.
I have below line of code:
ThisWorkbook.Worksheets("Diags").Range("$A1:$Y50000")
I am successful if I will convert the last number after $Y as below:
ThisWorkbook.Worksheets("Diags").Range("$A1:$Y" & MyVariable)
, but how to integrate also a variable after $A? I tried below but does not work..
ThisWorkbook.Worksheets("Diags").Range("$A1"& MyVariable2":$Y" & MyVariable1)
Appreciate your help.
Thanks, Brandon
You are missing an & behind MyVariable2, and you have an extra 1 behind $A. Try
ThisWorkbook.Worksheets("Diags").Range("$A" & MyVariable2 & ":$Y" & MyVariable1)

Again, variable in where clause

Using access 2010, windows 7, SQL Server
Can't get the hang of this. Have an SQL query that was generated in the qbe grid then put in VBA. The version that runs has a literal Transaction_Table.Account_Number and looks like:
"WHERE (((dbo_Transaction_Table.Sku)=""Transfer"")
AND ((dbo_Transaction_Table.Description) Like ""%TO%"")
AND ((dbo_Transaction_Table.Account_Number)=""655812""));"
But when I try to replace the literal with the contents of a text box :
"WHERE (((dbo_Transaction_Table.Description) Like ""%Transfer To%"")
AND ((dbo_Transaction_Table.Account_Number)=& Chr$(34) & Me.accntNo & Chr$(34)));"`
I get a syntax err (missing operator) in query expression
(((dbo_Transaction_table.Description) like "%Transfer To%")
And ((dbo_Transaction_Table.Account_Number)= & Chr$(34) & Me.accntNo & Chr$(34))))`
It sounds like you're just missing quotes between the constant string and the injected values
"WHERE (((dbo_Transaction_Table.Description) Like ""%Transfer To%"")
AND ((dbo_Transaction_Table.Account_Number)=" & Chr$(34) & Me.accntNo & Chr$(34) & "));"
Although you might look into using parameters instead. I'm not an expert on doing those from VBA but there should be plenty of examples out there.

Access/VBA and SQL WHERE clause format issue

I have read a huge pile of problems and solutions, and I just can't figure out what I'm doing wrong.
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & BounceDate & "#;"
DoCmd.RunSQL bncSql
_BounceMaster.DateCheck is in Date/Time format, which I think may be the issue, but I can't figure out what different format it should be in, or how to get there. As best as I can tell, BounceDate is correct - even using CDate didn't make a idfference. I have gotten both data mismatch errors, and currently, with code as above, I am getting syntax errors. What am I doing wrong?
I suppose the problem comes from date formatting. The BounceDate variable is DateTime type, so when you concatenate with string type variable, VBA automatically casts DateTime variable into String type using date format from your regional settings.
As I correctly remember, SQL interpreter from MS Access feels comfortable only with mm/dd/yyyy date format, so please try this:
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & Format(BounceDate, "mm/dd/yyyy") & "#;"
DoCmd.RunSQL bncSql
It should be
DELETE FROM _BounceMaster
not
DELETE _BounceMaster.* FROM _BounceMaster
You should be using parameterized queries, as your code is subject to SQL injection attack.

dd/mm automatically got changed to mm/dd

I coded something using Date statement in Access VBA. It was working fine until the start of this month, but now I am seeing that the Date has automatically changed the format from dd/mm/yyyy to mm/dd/yyyy. Has anyone else encountered the same problem?
The default Access SQL date format, regardless of locale, is mm/dd/yyyy. If you use an invalid date format, it will 'helpfully' try to convert that to a valid date for you.
So, if you use '30/09/2008', it will recognize you're using dd/mm/yyyy, and convert it appropriately. However, a value like '10/01/2008' is a valid mm/dd/yyyy value to begin with, so it will not be converted, and stored incorrectly in case you actually meant dd/mm/yyyy....
The solution is to always convert your date values to a mm/dd/yyyy string prior to using them in Access SQL statements. You have to be a bit careful here, as using VBA date format masks may not work entirely as you'd expect on non-US locales (e.g. 'helpfully' interpreting "mm/dd/yyyy" as "the localized short date format"), so please test carefully using your particular Access/VBA version.
Access requires a date to be unambiguous. It is generally recommended that you use yyyy/mm/dd, regardless of locale. For example:
strSQL="SELECT SomeDate FROM tblT WHERE SomeDate=#" & Format(DateVar, "yyyy/mm/dd") & "#"
Try this code:
stLinkCriteria = "[ProjectDate] Between #" & Format(CDate(Me![txtDateFrom]), "mm/dd/yyyy") & "# And #" & Format(CDate(Me![txtDateTo]), "mm/dd/yyyy") & "#"
It works for me.
This works:
sentenciaSQL = "UPDATE Numeraciones " & _
"SET Valor = " & Valor & ", " & _
"Fecha = #" & **Format(fecha,"mm/dd/yyyy HH:nn:ss") & "#, " &** _
"Id_Usuario = " & Id_Usuario & _
" WHERE Nombre = '" & Nombre & "'"
I have been very successful using the datevalue() function. When getting dates from unbound controls it seems to be clever enough to interpret the format "dd/mm/yyyy" correctly. Thus, a Jet SQL query like
"Select * from DateTable where StartDate = datevalue(" & me!TxtStartDate & ");"
seems to work every time.
I was experiencing same issue while trying to build a SQL string through VBA.
My locale settings use dd/mm/yyyy and I was trying to put into SQL statement data taken from an unbound textbox in a form.
The issue was due to the fact I was declaring, let's say, varMyDate as "date" type, so the engine reverted the format back even after the format.
Since you are really building a string the logical and proper data type is "string", and that solved the problem.