DSum with tempvars returning error - ms-access

I have some VBA code that sets a TempVar:
TempVars!ThisQtr = quarter
TempVars!LastQtr = lastQuarter
TempVars!LastYr = lastYear
TempVars!ThisYr = currentYear
Great! I know that it does set it as I have checked multiple times.
now my problem:
I'm trying to use this in a query
DSum("[SumBase]","CompareUnionQuery","[AU] = '" & [AU] & "' AND [GRP_ID] = " & [GRP_ID] & " AND [ACCOUNT] = '" & [ACCOUNT] & "'" & " AND [Fiscal_Year] = " & [TempVars]![ThisYr] & " AND [QTR]= " & [TempVars]![ThisQtr])
which this does work when I change out the TempVars with values. Can anyone help me with this?
I have tried to put a single quote around them and that doesn't work.
this expression does work:
DSum("[SumBase]","CompareUnionQuery","[AU] = '" & [AU] & "' AND [GRP_ID] = " & [GRP_ID] & " AND [ACCOUNT] = '" & [ACCOUNT] & "'" & " AND [Fiscal_Year] = 2015 AND [QTR]= '3'")

Compare the last pieces of your DSum expressions ...
AND [QTR]= '3'")
AND [QTR]= " & [TempVars]![ThisQtr])
You reported #1 works and #2 triggers the "Data type mismatch" error. So add single quotes before and after the TempVar value ...
AND [QTR]= '" & [TempVars]![ThisQtr] & "'")

Related

Syntax error in UPDATE Statement can't find the solution

I'm having some trouble updating my table in access through the following code:
Update = "UPDATE formation " & _
"SET AREA = '" & Me.AREA & "', " & _
"TYPE = '" & Me.TYPE & "', " & _
"TEAM = '" & Me.TEAM & "', " & _
"PRIORITY = '" & Me.PRIORITY & "', " & _
"PROCESS = '" & Me.PROCESSO & "', " & _
"NUMBER = " & Me.NumForm.Value & "', " & _
"SUPERVISOR = '" & Me.SUPERVISOR & "', " & _
"Cc = '" & Me.mailCc & "', " & _
"STATUS = 'SUBMITED', " & _
"USER_UPDATE = '" & newFormationUser & "', " & _
"DATA_STATUS_NOW = '" & Now() & "' " & _
"WHERE ID = " & Str(NewFormationID)
DoCmd.SetWarnings False
DoCmd.RunSQL Update
DoCmd.SetWarnings True
NumForm is a ComboBox of numbers 1 to 15.
I've tried to redo query, redo code, remake table with no luck in solving.
Read over and over again to find errors in typo or even in code.
Can you guys help me?
Thank you,
Diogo
There are many things wrong with that statement, but they can all be fixed using parametrization.
First, the missed quote, everyone saw that.
Second, you're setting a column equal to Now(), but not using the proper date formatting, and not using date delimiters (#).
Third, you're using apostrophes to delimit strings, but not testing if the strings themselves contain apostrophes, inviting unpredictable and hard to diagnose errors.
Also, Number is a keyword, that should be enclosed in [] brackets.
You should use parametrization, then most of these errors can't occur:
Dim qd As DAO.QueryDef
Set qd = CurrentDb.CreateQueryDef("", "UPDATE formation " & _
"SET AREA = ?, " & _
"TYPE = ?, " & _
"TEAM = ?, " & _
"PRIORITY = ?, " & _
"PROCESS = ?, " & _
"[NUMBER] = ?, " & _
"SUPERVISOR = ?, " & _
"Cc = ?, " & _
"STATUS = 'SUBMITED', " & _
"USER_UPDATE = ?, " & _
"DATA_STATUS_NOW = ? " & _
"WHERE ID = ?")
qd.Parameters(0) = Me.AREA
qd.Parameters(1) = Me.TYPE
qd.Parameters(2) = Me.TEAM
qd.Parameters(3) = Me.PRIORITY
qd.Parameters(4) = Me.PROCESSO
qd.Parameters(5) = Me.NumForm.Value
qd.Parameters(6) = Me.SUPERVISOR
qd.Parameters(7) = Me.mailCc
qd.Parameters(8) = newFormationUser
qd.Parameters(9) = Now()
qd.Parameters(10) = NewFormationID
qd.Execute

Open Report Use Three Combobox Filter

I have 3 combobox to filter data, then i want to open in command button to print preview..
Problems I have if i used 2 combobox it works but when i used three combobox the report is blank...there's any problems with the code?? here's the code i used :
Dim strWhereCondition As String
strWhereCondition = "[month] = '" & Me.cbmonth.Value _
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Debug.Print strWhereCondition
DoCmd.OpenReport "Month List", View:=acViewPreview, _
WhereCondition:=strWhereCondition
and the requery for my form
SELECT *
FROM MyTable
WHERE (month = Forms!nameForm!cbmonth
OR Forms!MeForm!cbwork IS NULL)
And (works = Forms!nameForm!cbwork
OR Forms!nameForm!cbwork IS NULL)
AND (works = Forms!nameForm!cbwork2
OR Forms!nameForm!cbwork2 IS NULL);
can anyone help?
Month is a number, so try:
strWhereCondition = "[month] = " & Me.cbmonth.Value _
& " and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Edit: Month is text.
However, [Work] is supposed to match two potentially different values:
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Should most likely be:
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [SomeOtherField] = '" & Me.cbwork2.Value & "'"

Ms Access SQL VBA query error no 3075

In my Access application, I am trying to concat existing field value with new value and I have code as below written in VBA, I am getting error 3075 "syntax error missing operator"
sUser = UserNameWindows
' MsgBox sUser
currenttime = Format(Now(), "dd/mm/yyyy hh:mm:ss")
SqlQuery = "UPDATE tbl_AllRequests " & _
" SET [Status]= 'Archived' " & _
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
" WHERE [ID] in (" & selectedIDs & ")"
'" SET [History] = concat([History], 'Archived on " & currenttime & " by '" & sUser & "'&')'&" & _
'data=concat(data, 'a')
'SqlQuery = "UPDATE tbl_AllRequests SET Status = 'Archived' WHERE ID in (17, 11)"
Debug.Print "this is new one " & SqlQuery
DoCmd.RunSQL SqlQuery, True
I am getting error on below line of code.
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
If I remove the code after 'Archived on' it works.
Thanks
First correct this:
currenttime = Format(Now(), "dd/mm/yyyy hh:nn:ss")
Then, use only single quotes around your fixed text:
", [History] = ([History] + CHR(13) + CHR(10)) & 'Archived on '" & currenttime & "' by '" & sUser & "" & _
Also, it makes no sense first to format Now to a string expression, then convert (with errors) to a date value by wrapping it in octothorpes (#..#), which - when concatenated with the strings - will be casted once more to a string.

MSAccess: Null vs '' - driving me crazy

I have a form with 2 embedded subforms.
The on-click event on subform_1 sets the recordsource of subform_2 referencing 5 attrs from the selected subform_1 record (all TEXT fields).
strSQL = "SELECT Table_1.* FROM Table_1 " & _
"WHERE (((Table_1.Attr_A)= '" & Forms![mainform]![subform_1]![attr1] & "') " & _
"AND ((Table_1.Attr_B) = '" & Forms![mainform]![subform_1]![attr2] & "') " & _
"AND ((Table_1.Attr_C) = '" & Forms![mainform]![subform_1]![attr3] & "') " & _
"AND ((Table_1.Attr_D) = '" & Forms![mainform]![subform_1]![attr4] & "') " & _
"AND ((Table_1.Attr_E) = '" & Forms![mainform]![subform_1]![attr5] & "'));"
Forms![mainform]![subform_2].Form.RecordSource = strSQL
My issue is that some records may have a NULL value among the 5 required attrs, which is a valid condition. The surrounding '' when the subform_1 value is NULL is resulting in (0) records in the collection.
Any suggestions to effectively handle the NULL condition in subform_1?
If you were considering only Table_1.Attr_A, I think you're saying you want this ...
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE (t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'"
If that is correct, add an AND for the next condition based on Attr_B.
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE " & _
"(t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'" & _
" AND (t1.Attr_B & '') = '" & Forms![mainform]![subform_1]![attr2] & "'"
And continue from there by adding the remaining conditions.
I believe the Nz function can also be of use:
"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE Nz(t1.Attr_A) = '" & Forms![mainform]![subform_1]![attr1] & "'"

DLookup as ControlSource through VBA code

The below is in my control source and works:
=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",DLookUp("[CustomerSKU]","ItemCustomerSKU","ItemNumber = '" & Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" & Forms!GARF!customer & "' AND Company = '" & Forms!GARF!company & "' AND MarketingSeason = '" & DLookUp("[Code]","IDtoCode","ID = " & Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")
I would like to assign it to another control source through VBA code like such:
Forms![GARF]![GARFsubForm]![SKU].ControlSource = (the above code)
Forms![GARF]![GARFsubForm]![SKU] is a column in a datasheet.
UPDATE:
With Remou's help I was able to come up with the below working solution:
The code is on the Form_Load event:
Forms![GARF]![GARFsubForm]![SKU].ControlSource =
"=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>'',"
& "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '""
& Forms!GARF!GARFsubForm![finished_good_#] & ""' AND Customer = '" _
& Forms!GARF!customer & "' AND Company = '" _
& Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
& " AND MarketingSeason = '"
& DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id) & "'"")" & ",'')"
There are a few problems. This is what is returned by your string (broken into lines for clarity):
=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",
DLookUp([CustomerSKU],ItemCustomerSKU,"ItemNumber = '"
& Forms!GARF!GARFsubForm![finished_good_#]
& " ' AND Customer = '" & Forms!GARF!customer
& "' AND Company = '" & Forms!GARF!company
& "' AND MarketingSeason = '" & DLookUp([Code],IDtoCode,"ID = "
& Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")
As you can see, these two are not quoted and they need to be:
DLookUp([CustomerSKU],ItemCustomerSKU,
There is an extra space here:
& " ' AND Customer = '" & Forms!GARF!customer
Is finished_good_# text similarly season_id?
It is often possible to use single quote ' instead of "", especially if you escape any occurrence of ' in the text with two ' eg ='O''Reilly'
EDIT
You are doing this in code, so it should be possible to make life easier, for example:
A = DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id)
B = "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '" _
& Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" _
& Forms!GARF!customer & "' AND Company = '" _
& Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
& " AND MarketingSeason = '" & A & "'"")"
s = "=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>''," & c & ",'')"
Debug.Print s