Multiple SSRS IIF statement on one line - reporting-services

I have a report where I need to accomplish 3 tasks in one query.
=IIF (Fields!Name.Value = " "," ", & vbcrlf & Name)
=IIF (Fields!Lastname.Value="" ," ", & vbcrlf & lastname)
=IIF(Fields!Middle.Value = " "," ", & vbcrlf & Middle)
So what I'm trying to do it run those queries on one line and output the name, lastname and middle name on different lines
=IIF(Fields!Name.Value is nothing," ", & vbcrlf & Name, IIF(Fields!Lastname.Value is nothing," ", & vbcrlf & lastname), IIF(Fields!Middle.Value is nothing," ", & vbcrlf & Middle))
It doesn't seem to run, it just prints False.
Can someone please help? Thanks

This should work...
=IIF(LEN(Fields!FirstName.Value) = 0 , "", Fields!FirstName.Value + vbcrlf )
+ IIF(LEN(Fields!MiddleName.Value) = 0 , "", Fields!MiddleName.Value + vbcrlf )
+ IIF(LEN(Fields!LastName.Value) = 0 , "", Fields!LastName.Value )
Here's some sample data with the results. It includes names that are NULL and empty strings and handles both by testing the length of the field.
If you want blank lines so it always returns 3 lines then move the + vbcrlf to outside the closing parentheses on each each.

Related

INSERT INTO table with Foreign Key from Form

I am trying to create a Form that is used to manually enter data in certain scenarios. Most data is input from CSV files which is working fine. I have 4 tables, Part , Assembly , MachineOrder , and Job. I was able to write code for entering into the base table, Part, from the Form no problem. The issue now is entering data into the Assembly and MachineOrder tables where the Parts are being referenced by their PID autonumber field and the Assemblies are being referenced by their AID autonumbered field. I have tried many different kinds of methods to perform this of which you can see a bit of in my commented out code. What is there is what I believe to be my closest to correct code thus far with the error now being that Access asks me for the parameter value of rPID even though it is finding the value in the Dlookup function fine. I'm assuming the same is true for the rAID section as well.
Otherwise I'm getting errors of Key Violations when using the INSERT then UPDATE method you see commented out.
The form is called HOTEntry
Any advice on what my problem may be is greatly appreciated, I'm a student and this is my first time trying to use what I've learned in a professional application so any and all constructive criticism is wanted! Apologies if this is a rather specific question but I could really use the help on this since I've been working on it for two days to no avail...
My code:
Sub HOTParts2()
Dim rPID As Integer
Dim rAID As Integer
Dim dbs As DAO.Database
Dim sqlstr1 As String
Dim sqlstr2 As String
Dim sqlstr3 As String
Dim sqlstr4 As String
Set dbs = CurrentDb
'sqlstr1 = "INSERT INTO Assembly ( PID, ModelNum, ModelRev, ModelDescription ) " _
' & "SELECT (PID,Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes)" _
' & "FROM Part " _
' & "WHERE Part.PartName = Forms!HOTEntry!txtPartName AND Part.Config = Forms!HOTEntry!txtConfigEntry AND Part.Rev = Forms!HOTEntry!txtRevEntry"
sqlstr1 = "INSERT INTO Assembly ( ModelNum, ModelRev, ModelDescription,PID ) " _
& "VALUES (Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes," & "rPID" & ");"
'
'sqlstr2 = "UPDATE Assembly " _
' & "SET PID =" & rPID & " " _
' & "WHERE Assembly.ModelNum = Forms!HOTEntry!txtHotModel And Assembly.ModelDescription = Forms!HOTEntry!txtHotDes And Assembly.ModelRev = Forms!HOTEntry!txtHotRev;"
'
'sqlstr3 = "INSERT INTO MachineOrder ( AID, Serial, CustName ) " _
' & "SELECT (AID,Forms!HOTEntry!txtHotSerial, Forms!HOTEntry!txtHotCust)" _
' & "FROM Assembly" _
' & "WHERE Assembly.Model=Forms!HOTEntry!txtHotModel And ModelDescription= Forms!HOTEntry!txtHotDes And ModelRev = Forms!HOTEntry!txtHotRev; "
sqlstr3 = "INSERT INTO MachineOrder (Serial, CustName, AID ) " _
& "VALUES (Forms!HOTEntry!txtHotSerial, Forms!HOTEntry!txtHotCust," & "rAID" & ");"
'
'sqlstr4 = "UPDATE MachineOrder " _
' & "SET AID =" & rAID & " " _
' & "WHERE AID IS NULL;"
rPID = DLookup("PID", "Part", "PartName = " & "'" & Forms!HOTEntry!txtPartName & "'" & " And " & "Config = " & "'" & Forms!HOTEntry!txtConfigEntry & "'" & " And " & "Rev = " & "'" & Forms!HOTEntry!txtRevEntry & "'")
DoCmd.RunSQL sqlstr1
'DoCmd.RunSQL sqlstr2
rAID = DLookup("AID", "Assembly", "ModelNum = " & "'" & Forms!HOTEntry!txtHotModel & "'" & " And " & "ModelDescription = " & "'" & Forms!HOTEntry!txtHotDes & "'" & " And " & "ModelRev = " & "'" & Forms!HOTEntry!txtHotRev & "'")
DoCmd.RunSQL sqlstr3
'DoCmd.RunSQL sqlstr4
End Sub
Well, if you want to use the looked up rPID and rAID in a query, you need to do more than just set them in VBA. You can either manually fill them in in your SQL statement, use a parameter and a QueryDef and fill in the parameter in your QueryDef, or put the DLookUp inside your SQL statement.
Going with the first approach here, only unquoted rPID in your initial statement, and put it after rPID was set.:
rPID = DLookup("PID", "Part", "PartName = " & "'" & Forms!HOTEntry!txtPartName & "'" & " And " & "Config = " & "'" & Forms!HOTEntry!txtConfigEntry & "'" & " And " & "Rev = " & "'" & Forms!HOTEntry!txtRevEntry & "'")
sqlstr1 = "INSERT INTO Assembly ( ModelNum, ModelRev, ModelDescription,PID ) " _
& "VALUES (Forms!HOTEntry!txtHotModel, Forms!HOTEntry!txtHotRev, Forms!HOTEntry!txtHotDes," & rPID & ");"
DoCmd.RunSQL sqlstr1

I need to replace the value for a running variable

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

Check if Exist or Not then Insert Update

I need to check if the data exist if the data didn't exist then I'll be able to insert and update but if the data exist the messagebox will show that the data already exist but when I tried to add same data that already exist it still add and no messagebox shown that it already exist.
here's my code
If jobtitle <> "" And businessunit <> "Please Select" And division <> "Please Select" And subdivision <> "Please Select" And classification <> "Please Select" And subclassification <> "Please Select" Then
insrtResult = UpdateInsDelRecord("UPDATE EMP_MASTERTBL SET JOBTITLE = '" & jobtitle & "' " & _
"WHERE MASTERID = '" & empID & "'" & _
";" & _
"INSERT INTO EMPGROUP_TBL(MASTERID, BUSINESS_UNIT, " & _
"DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " & _
"('" & HandleQuote(empID) & "', " & _
"'" & businessunit & "' ," & _
"'" & division & "' ," & _
"'" & subdivision & "' ," & _
"'" & classification & "' ," & _
"'" & subclassification & "')")
If Not insrtResult Then
MessageBox("alert('Error Ocurred While Inserting a Data.')")
Else
MessageBox("alert('Successfully Added.')")
End If
Else
MessageBox("alert('Data Already Exist.')")
End If
what could be the problem to my code? Thanks in advance.
For SQL Server check the MERGE query, i used this to perform an "upsert" on my own db, it also seems to be performing fast enough.
You can chose a condition and perform different operations whether it returns true or false (e.g. if ID already exists then update, else insert)
For MySql i think there are solutions much more easier to write, wich unfortunatly i can't remember at the moment
https://msdn.microsoft.com/en-us/library/bb510625.aspx

Runtime Error 3134 - Syntax Error in INSERT INTO Statement

I'm trying to fix it about hour but it's not work Please help me :(
CurrentDb.Execute "INSERT INTO match_day( home_team, away_team, date, time, home_score, away_score, stadium) " & _
" VALUES('" & Me.textHT & "','" & Me.textAT & "',#" & Me.textDATE & "#,#" & Me.textTime & "#," & Me.textHS & "," & Me.textAS & ",'" & Me.textSTD & ",')"
Are the fields for date and time considered reserved words and should be wrapped in brackets or ticks to qualify it as the column name...
..., [date], [time], ...
But I think it is most likely the trailing final comma before your final closing ) of the inserted values making it look like it wants another field to be inserted.
Me.textSTD & ",')"
change to
Me.textSTD & "')"
I ran into a similar error - thanks to this post I realised that I had used a reserved name "note" in a table ( instead of "notes").
StrSQL = "INSERT INTO option_notes ( OPTION_ID , USER_ID , [NOTE] ) VALUES ( " & currID & " , " & currUserID & " , '" & currNote & "' ) ; "
CurrentDb.Execute StrSQL
I ended up changing the field name - however, wrapping the field name with [ ] allowed the code to execute correctly.

How to combine mutiple column with comma separator?

I have 11 columns as Note1,Note2,Note3,......Note11. I have write a query like this to combine
SELECT DormData.BuildingID,
DormData.DormRoomID,
DormData.Item,
DormData.Result,
DormData.InspectorID,
DormData.Date,
DormData.Qty,
DormData.Section,
(Note1 & " , "
& Note2 & ", "
& Note3 & " , "
& Note4 & " , "
& Note5 & " , "
& Note6 & " , "
& Note7 & ", "
& Note8 & ", "
& Note9 & ", "
& Note10 & ", "
& Note11) AS Notes,
DormData.Comments,
DormData.Resident
FROM DormData;
It works and combine my records but problem is that it is not necessary that all the notes
columns have values.suppose that if in a row there is values in only Note1 and Note5 then it gives output like not1,,,,note5. but I want it show "Note1,Note5"
How can I fix this?
You could use an Iif statement in each 'Note1 * ","' section to check for null values.
Iif(IsNull(Note1), Note1, Note1 & ",")
I think that should work.
The key is IIF() aka immediate if.
For example, on the orders table in the Northwind sample database:
IIF(orders.ShipRegion IS NOT NULL, orders.ShipRegion & ',', ''
Or a more complete query:
SELECT
orders.OrderID, orders.CustomerID, orders.EmployeeID,
orders.ShipVia, orders.Freight,
(orders.ShipName & ',' & orders.ShipCity & ',' & IIF(orders.ShipRegion IS NOT NULL, orders.ShipRegion & ',', '') & orders.ShipPostalCode & ',' & orders.ShipCountry) AS Expr1
FROM orders
WHERE orders.[OrderID]=10282;
If you want to go the vba function route, the following function will do the job:
Function JoinStrings(Delimiter As String, _
ParamArray StringsToJoin() As Variant) As String
Dim v As Variant
For Each v In StringsToJoin
If Not IsNull(v) Then
If Len(JoinStrings) = 0 Then
JoinStrings = v
Else
JoinStrings = JoinStrings & Delimiter & v
End If
End If
Next v
End Function
You would call it like this:
JoinStrings(", ", Note1, Note2, Note3, Note4, Note5, Note6, Note7)
You can also use a trick with how Null expressions and concatenation works in Access:
Note1 & (", " + Note2) & (", " + Note3)...
When concatenating text, Access treats Null as it were an empty string. But if you're "adding", the Null would cause the expression inside the parentheses to result in Null. As long as your notes aren't numeric, this will work.
A modification to TheOtherTimDuncan' solution, which will work well to concatenate two (or may be three) Notes. Use IIF() to have delimiter or blank based on whether Note1 is Null. It could be like:
Note1 & (IIF(Note1 Is Null, "",", ") + Note2) & (IIF((Note1 & Note2) Is Null, "",", ") + Note3)...