I have an SSRS expression that i need to change the values in based on a passed parameter.
="Double Coat " &
IIF((First(Fields!rr_paintpayout.Value, "Market") = "Task-Based") OR (First(Fields!rr_paintpayout.Value, "Market") = "Hourly"),
IIF(Fields!rr_taskpayoutaddonoption.Value = "Floorplan Configuration",
Fields!rr_1x1.Value & " - "& Fields!rr_1x4.Value**,
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",
Fields!rr_fixedamount.Value))),
IIF(First(Fields!rr_paintpayout.Value, "Market") = "Variable Percent",
IIF(Fields!rr_variablepercentaddonselection.Value = "Sq Ft Variable Pricing",
Fields!rr_fixedamount.Value & " sq. ft.",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",
Fields!rr_fixedamount.Value))), 0.00))
My parameter is an int and based on either 1,2,3 or 4. I need to change the third line of the expression. I have tried to wrap the entire expression in an IIF and I was not able to run the report. What im thinking is
="Double Coat " &
IIF((First(Fields!rr_paintpayout.Value, "Market") = "Task-Based") OR (First(Fields!rr_paintpayout.Value, "Market") = "Hourly"),
IIF(Fields!rr_taskpayoutaddonoption.Value = "Floorplan Configuration",
IIF((Parameters.Bedrooms.Value =1 )
Fields!rr_1x1.Value & " - "& Fields!rr_1x4.Value)),
IIF((Parameters.Bedrooms.Value =2 )
Fields!rr_2x1.Value & " - "& Fields!rr_3x4.Value)),
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",
Fields!rr_fixedamount.Value))),
IIF(First(Fields!rr_paintpayout.Value, "Market") = "Variable Percent",
IIF(Fields!rr_variablepercentaddonselection.Value = "Sq Ft Variable Pricing",
Fields!rr_fixedamount.Value & " sq. ft.",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",
Fields!rr_fixedamount.Value))), 0.00))
which is basically adding the Parameters.Value right at the top. Has anyone had any success or a better way to change what expression is being used?
When I took your Expression in notepad and checked bracktes, it did not matched.
I reworked your expression and I could find 1 false expression been missing from your expression. You might want to check that. Below is the expression I created from your example.
="Double Coat " & IIF(First(Fields!rr_paintpayout.Value, "Market") = "Task-Based" OR First(Fields!rr_paintpayout.Value, "Market") = "Hourly",
IIF(Fields!rr_taskpayoutaddonoption.Value = "Floorplan Configuration",
IIF(Parameters.Bedrooms.Value =1,
Fields!rr_1x1.Value & " - "& Fields!rr_1x4.Value,
IIF(Parameters.Bedrooms.Value =2 ,
Fields!rr_2x1.Value & " - "& Fields!rr_3x4.Value,
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_taskpayoutaddonoption.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",Fields!rr_fixedamount.Value)))),
IIF(First(Fields!rr_paintpayout.Value, "Market") = "Variable Percent",
IIF(Fields!rr_variablepercentaddonselection.Value = "Sq Ft Variable Pricing",
Fields!rr_fixedamount.Value & " sq. ft.",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Quantity",
Fields!rr_fixedamount.Value & " each",
IIF(Fields!rr_variablepercentaddonselection.Value = "Fixed Hourly",
Fields!rr_fixedamount.Value & " hourly",Fields!rr_fixedamount.Value))),
false)),
0.00)
Related
Need help with this code...looking to append four records with a running variable
For sec_ref = 1 To 4
qrysecscr = "v_secscr_" & sec_ref
qrysecscr_val = Eval(qrysecscr)
inssql1 = " Insert into sur_sec_score (Survey_ID, Section_ID, Section_Score) values "
inssql1 = inssql1 & "(""" & v_survey_id & """, " & sec_ref & ", " & qrysecscr_val & ")"
'CurrentDb().Execute inssql1, dbFailOnError
MsgBox inssql1
Next
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.
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] & "'")
Context:
I made a Vacation tracking module in my database. The employees can request advanced hours take off in a current year period and it will be deducted from their next years period.
The Case:
I am trying to make a continuous loop through the recordset of employees to see if they have been awarded advanced hours and if yes add it to the vacation hours they have.
The Problem:
I have the logic down but I can't get it to loop through each employee on my continuous form.
With Me.RecordsetClone
While Not .EOF
adv = DLookup("advhours", "dbo_employees", "[empid] = txtempid")
vac = DLookup("vhrs", "dbo_employees", "[empid] = txtempid")
adate = DLookup("advdate", "dbo_employees", "[empid] = txtempid")
andatestart = DLookup("anndate", "dbo_employees", "[empid] = txtempid")
andateend = DateAdd("yyyy", AgeSimple([andatestart]), [andatestart])
anddateend = DateAdd("yyyy", 1, andateend)
morehours = DLookup("totalvachrs", "dbo_employees", "[empid] = txtempid")
sum = adv + vac
If adv > 0 And adate > DateAdd("yyyy", AgeSimple([andatestart]), [andatestart]) And adate < anddateend And morehours = 0 Then
morehours = 1
' sets the flag to 1 if true.
DoCmd.RunSQL "UPDATE dbo_employees " & "SET dbo_employees.totalvachrs='" & morehours & "' " & "WHERE dbo_employees.empid=" & txtempid & ";"
'increment vacation hours
DoCmd.RunSQL "UPDATE dbo_employees " & "SET dbo_employees.vhrs='" & sum & "' " & "WHERE dbo_employees.empid=" & txtempid & ";"
End If
'after Vac hours have been updated..
If morehours = 1 And adate < andateend Then
' puts vacation hours back down to where they were.
vac = vac - adv
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.vhrs='" & _
vac & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
adv = 0
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.advhours='" & _
adv & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
morehours = 0
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.totalvachrs='" & _
morehours & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
End If
Debug.Print txtempid ' CTRL G to see
If Not .EOF Then .MoveNext
Wend
End With
MsgBox "after loop: " & txtempid
I have also tried this as well, but i get drop changes dialogue in access
Set rs = Me.RecordsetClone
rs.MoveFirst
Do While Not rs.EOF
Me.Bookmark = rs.Bookmark
adv = DLookup("advhours", "dbo_employees", "[empid] = txtempid")
vac = DLookup("vhrs", "dbo_employees", "[empid] = txtempid")
adate = DLookup("advdate", "dbo_employees", "[empid] = txtempid")
andatestart = DLookup("anndate", "dbo_employees", "[empid] = txtempid")
andateend = DateAdd("yyyy", AgeSimple([andatestart]), [andatestart])
anddateend = DateAdd("yyyy", 1, andateend)
morehours = DLookup("totalvachrs", "dbo_employees", "[empid] = txtempid")
sum = adv + vac
If adv > 0 And adate > DateAdd("yyyy", AgeSimple([andatestart]), [andatestart]) And adate < anddateend And morehours = 0 Then
morehours = 1
DoCmd.RunSQL "UPDATE dbo_employees " & "SET dbo_employees.totalvachrs='" & morehours & "' " & "WHERE dbo_employees.empid=" & txtempid & ";"
DoCmd.RunSQL "UPDATE dbo_employees " & "SET dbo_employees.vhrs='" & sum & "' " & "WHERE dbo_employees.empid=" & txtempid & ";"
End If
If morehours = 1 And adate < andateend Then
vac = vac - adv
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.vhrs='" & _
vac & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
adv = 0
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.advhours='" & _
adv & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
morehours = 0
DoCmd.RunSQL "UPDATE dbo_employees " & _
"SET dbo_employees.totalvachrs='" & _
morehours & "' " & _
"WHERE dbo_employees.empid=" & txtempid & ";"
End If
Me.Bookmark = rs.Bookmark
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
DotyDot,
I think that your problem is a basic misunderstanding of how recordsets and forms (and continuous forms) work.
Regardless of whether the form is a regular form (one record displayed at a time) or a continuous form (multiple records displayed at a time), the ability and limitations in referencing values (fields) on the form and/or use the recordset does not change.
You can't "loop" through a continuous form in code and reference the controls on the form (txtempid) and get the value from each record, as you are trying to do.
Anytime you reference a control on a form, you will only get the value for that control associated with the current record.
Looping through a recordset, even if you instantiate the recordset from the form recordsetclone, will NOT change the current record pointer on the form.
Your second code example is closest to something that will work, but still needs some work. First, when working with a recordset in code, you will most likely NOT reference the form controls, you will reference the field values directly from the recordset. I don't know your table structure, but here is an example that should get you on the right track.
I assume that this code will run on a button click, or some other event where you are wanting to evaluate all employees.
Dim rst as DAO.Recordset
Set rst = Me.RecordsetClone
Do While Not rst.EOF
adv = DLookup("advhours", "dbo_employees", "[empid] = " & rst("empid"))
vac = DLookup("vhrs", "dbo_employees", "[empid] = " & rst("empid"))
adate = DLookup("advdate", "dbo_employees", "[empid] = " & rst("empid"))
andatestart = DLookup("anndate", "dbo_employees", "[empid] = " & rst("empid"))
andateend = DateAdd("yyyy", AgeSimple(rst("andatestart")), rst("andatestart"))
anddateend = DateAdd("yyyy", 1, rst("andateend"))
morehours = DLookup("totalvachrs", "dbo_employees", "[empid] = " & rst("empid"))
sum = adv + vac
If adv > 0 And adate > DateAdd("yyyy", AgeSimple(rst("andatestart")), rst("andatestart")) And adate < anddateend And morehours = 0 Then
morehours = 1
rst.Edit
rst("totalvachrs") = morehours
rst("vhrs") = sum
rst.Update
End If
If morehours = 1 And adate < andateend Then
vac = vac - adv
rst.Edit
rst("vhrs") = vac
adv = 0
rst("advhours") = adv
morehours = 0
rst("totalvachrs") = morehours
rst.Update
End If
rst.MoveNext
Loop
Set rst = Nothing
'Refresh the screen
Me.Requery
You don't need to use DoCmd.RunSQL, you can edit the data directly in the Recordset via code.
Please note that in your example code, anyplace where you appeared to be referencing a control on the form
[andatestart]
I changed it to reference that field name in the recordset
rst("andatestart")
This code will loop through every record on your form and perform the calculations and updates you were doing.
FYI, in your original code, by setting the form bookmark (Me.Bookmark)
to the recordset Bookmark, you were forcing the form to move the
current record pointer through each record, but that is really not the
best way to accomplish what you are trying to do, IMO
I hope this helps.
I'm trying to write an SQL query in VBA but it's not working. Not sure why, its a simple SELECT query. Below is my SQL query:
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,
tbl_Contacts.idSite, tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone, tbl_Contacts.involvement,
tbl_Contacts.Taken" _ & "FROM tbl_Contacts " _
& "WHERE (((tbl_Contacts.role)= 'Pharmacist') AND
((tbl_Contacts.involvement)=True) AND ((tbl_Contacts.Taken)=False)); "
Cheers guys
Most probably the error is because of spacing issue in your query. You need a space as shown below
& " FROM tbl_Contacts " _
^---- Here
Otherwise, your query string looks like
SELECT TOP 1 tbl_Contacts.ID, tbl_Contacts.idSite,
tbl_Contacts.role, tbl_Contacts.name,
tbl_Contacts.email, tbl_Contacts.phone,
tbl_Contacts.involvement, tbl_Contacts.TakenFROM tbl_Contacts
^-- ERROR Here
Why so many brackets?
When using VBA to hit a DB, I always like to structure my SQL query so it looks exactly like I would enter it into an SQL Query tool.
Run and have a look in the debug window (ctrl-g)
Much neater and as a result much easier to modify and trouble shoot:
Sub sql()
strSQLPharmContact = "SELECT TOP 1 tbl_Contacts.ID,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.idSite,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.role,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.name,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.email,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.phone,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.involvement,"
strSQLPharmContact = strSQLPharmContact & vbLf & " tbl_Contacts.Taken"
strSQLPharmContact = strSQLPharmContact & vbLf & "FROM tbl_Contacts "
strSQLPharmContact = strSQLPharmContact & vbLf & "WHERE tbl_Contacts.role= 'Pharmacist'"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.involvement = True"
strSQLPharmContact = strSQLPharmContact & vbLf & "AND tbl_Contacts.Taken = False"
Debug.Print strSQLPharmContact
End Sub