Unwanted data showed by crystal report - vb.net

i'am using this formula for parameters (record selection) :
if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort};
if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin};
if {?hal} <> "" then {ArrivingTrips.hall}={?hal};
if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim};
{?d1} = {ArrivingTrips.arrivalDate};
the problem now, crystal report just looks at d1 and dosent care for another parameters even if the "if" expression is true so it shows unwanted data, why that ?
is it wrong syntax ?
Note : i send the parameters values by my vb.net code, not by Crystal parameters input panel.
Thanks.

local timevar t;
local stringvar air;
local stringvar port;
local stringvar halll;
if {?tim} <> "" then t:= ctime({?tim}) else t:= ctime({ArrivingTrips.arrivalTime});
if {?airlin} <> "" then air:= {?airlin} else air:= {ArrivingTrips.airlines};
if {?customport} <> "" then port:= {?customPort} else port:= {ArrivingTrips.port};
if {?hal} <> "" then halll:= {?hal} else halll:= {ArrivingTrips.hall};
{ArrivingTrips.airlines} = air and {?d1}= {ArrivingTrips.arrivalDate} and port={ArrivingTrips.port} and halll = {ArrivingTrips.hall} and (t)=ctime({ArrivingTrips.arrivalTime});
This is the solution, now , when "if condition" returns false result, report will care only for true conditions because the false one will will return the same value of a field so it's like no condition :)
Thanks for every one tried to help.

({?d1} = {ArrivingTrips.arrivalDate})
AND (If {?customPort} <> "" Then {ArrivingTrips.port} = {?customPort} Else TRUE)
AND (If {?airlin} <> "" Then {ArrivingTrips.airlines} = {?airlin} Else TRUE)
AND (If {?hal} <> "" Then {ArrivingTrips.hall}={?hal} Else TRUE)
AND (If (hasvalue({?tim})) Then {ArrivingTrips.arrivalTime}={?tim} Else TRUE)

You are writing your selection formula as single statements, so only the last statement in line gets evaluated. You should also have an "else" for each "if" condition, otherwise your statements will be prone to error and may show undesired data.
(if {?customPort} <> "" then {ArrivingTrips.port} = {?customPort} else {other.condition}) OR
(if {?airlin} <> "" then {ArrivingTrips.airlines} = {?airlin} else {other.condition}) OR
(if {?hal} <> "" then {ArrivingTrips.hall}={?hal} else {other.condition}) OR
(if (hasvalue({?tim})) then {ArrivingTrips.arrivalTime}={?tim} else {other.condition}) OR
({?d1} = {ArrivingTrips.arrivalDate})
This formula will evaluate if any of the conditions are met. If you need a combination of conditions met, then you will have to join them by using AND.

Related

'If' statement with multiple conditions over multiple lines in VB.net

I am checking the existence of two files and after that setting a condition but I am getting an error message that the syntax is incorrect when I add the AND
If (System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_Attendance.xls").Length > 0
and System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_EP.xls").Length > 0) Then
Dts.Variables("VCountAPAListFile").Value = True
Else
Dts.Variables("VCountAPAListFile").Value = False
End If
But if I check just one file without using AND statement, it works fine.
Add an underscore _ before the line terminates to indicate a line break.
If (System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_Attendance.xls").Length > 0 _
And System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_EP.xls").Length > 0) Then
Dts.Variables("VCountAPAListFile").Value = True
Else
Dts.Variables("VCountAPAListFile").Value = False
End If
You need to either put the line break after the 'And' or use a line continuation character (underscore). Also, it's usually better to use 'AndAlso' in modern VB.
If condition _
AndAlso otherCondition Then
DoThing()
Else
DoOtherThing()
End If
Or
If condition AndAlso
otherCondition Then
DoThing()
Else
DoOtherThing()
End If
Depending on the version, the "And" should be on the first line with a _ at the end.
If (System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_Attendance.xls").Length > 0 And _
System.IO.Directory.GetFiles(CStr(Dts.Variables("VNetworkFolderName").Value), "Z_EP.xls").Length > 0) Then
Dts.Variables("VCountAPAListFile").Value = True
Else
Dts.Variables("VCountAPAListFile").Value = False
End If
Calling GetFiles twice is a bad idea. It will iterate through all the folders in the directory twice! You would be better off using File.Exists instead:
Dim folderName As String = CStr(Dts.Variables("VNetworkFolderName").Value
Dim filename1 = Path.Combine(folderName, "Z_Attendance.xls")
Dim filename2 = Path.Combine(folderName, "Z_EP.xls")
Dts.Variables("VCountAPAListFile").Value = File.Exists(filename1) AndAlso File.Exists(filename2)

multiple criteria for an if statement

I have a form with 3 text boxes, txt_customeracc, txt_customername, txt_customercontact
These 3 text boxes are optional and by default the text boxes will have "N/A" displaying on form load, but if a user enters information into one of them I want them to enter the information in the other two boxes also.
The code I am using is below
If txt_customername.Text <> "" Or txt_customername.Text <> "N/A" Or
txt_customercontact.Text <> "" Or txt_customercontact.Text <> "N/A" And
txt_customeracc.Text = "" Or txt_customeracc.Text = "N/A"
Then error1 += vbNewLine & "Please enter a correct Customer Account Number"
So from the above code I am expecting that if a user enters information in either the txt_customername or the txt_customercontact text boxes but not in the txt_customeracc box the warning should then appear but currently the warning message is displaying regardless of whether information is or isn't entered in any of the boxes. Can anyone tell me what I am doing wrong?
What is operator precedence ?
Your main problem here is that you have an issue with operator precedence. What is that ?
It is exactly the same issue as when doing calcuations, multiplication comes first, then comes addition. Well in VB .NET, And operator comes before Or, so what you have written in your code is evaluated as follow :
If txt_customername.Text <> "" Or
txt_customername.Text <> "N/A" Or
txt_customercontact.Text <> "" Or
(txt_customercontact.Text <> "N/A" And txt_customeracc.Text = "") Or
txt_customeracc.Text = "N/A"
Then
error1 += vbNewLine & "Please enter a correct Customer Account Number"
End If
Since this is not really what you want, let's build this together :
if customername OR customercontact is filled up
AND
customeracc is empty
That would give us :
if (
(txt_customername.Text <> "" Or txt_customername.Text <> "N/A") 'CustomerName is filled up
Or
(txt_customercontact.Text <> "" Or txt_customercontact.Text <> "N/A") 'Customer Contact is filled up
)
And
(txt_customeracc.Text = "" Or txt_customeracc.Text = "N/A") 'Customer account is empty
Then
'Do whatever
End If
Make it better, call a function
Another problem here is readability, this code may have errors because it's hard to read, so hard to debug.
What we can do is build a function that will check if a textbox is empty :
Private Function IsEmpty(Tb As Textbox) As Boolean
'Here we return true if tb.Text is empty or contains "N/A"
Return Tb.Text = "" Or Tb.Text = "N/A"
End Function
So that would make this a bit more readable :
if (Not IsEmpty(txt_customername) Or Not IsEmpty(txt_customercontact)) 'CustomerName or Customer Contact is filled up
And IsEmpty(txt_customeracc) 'Customer account is empty
Then
'Do whatever
End If
Make it better (2), Compare the strings
As stated by zaggler in his comment, here we don't use String Comparison. What if a user starts typing, then decides to put it back to N/A and writes it lowercase ("n/a") ? Well, we will make a mistake, believing that he did fill up the Textbox and you will end up searching for user "n/a" in your database, which is not a very good idea...
So let's compare the String, make our function even better :
Private Function IsEmpty(Tb As Textbox) As Boolean
'Here we return true if tb.Text is empty or contains "N/A" (or "n/a")
Return Tb.Text = "" Or (String.Compare(Tb.Text, "N/A", True) = 0)
End Function
Note
You can see here the advantage of functions. I wrote it because I didn't want to change to String.Compare() six times... Whenever you have the same code twice, it should be a function...
If you want to give the user a proper error message telling him what he has missed to fill out you have to split the if statement into several parts.
First check if all text boxes contain any valid data.
If not you can skip further checks directly.
If one textbox contains data check each and set error accordingly.
If (txt_customername.Text = "" OrElse txt_customername.Text = "N/A") AndAlso
(txt_customercontact.Text = "" OrElse txt_customercontact.Text = "N/A") AndAlso
(txt_customeracc.Text = "" OrElse txt_customeracc.Text = "N/A") Then
'No Error exit sub
Exit Sub
End If
'This part is only reached if one textbox contains data
If (txt_customername.Text = "" OrElse txt_customername.Text = "N/A") Then
error1 += vbNewLine & "Please enter a correct Customer Name"
End If
If (txt_customercontact.Text = "" OrElse txt_customercontact.Text = "N/A") Then
error1 += vbNewLine & "Please enter a correct Customer Contact"
End If
If (txt_customeracc.Text = "" OrElse txt_customeracc.Text = "N/A") Then
error1 += vbNewLine & "Please enter a correct Customer Account Number"
End If
As you can see I also recommend using short circuit OrElse and AndAlso which gives a littttttttle performance.
You could count the number of filled fields.
Dim numberFilled As Integer = 0
If txt_customername.Text <> "" And txt_customername.Text <> "N/A" Then
numberFilled += 1
End If
If txt_customercontact.Text <> "" And txt_customercontact.Text <> "N/A" Then
numberFilled += 1
End If
If txt_customeracc.Text <> "" And txt_customeracc.Text <> "N/A" Then
numberFilled += 1
End If
If numberFilled = 1 Or numberFilled = 2 Then
error1 += vbNewLine & "Please enter a correct Customer Account Number"
End If
Personally I would have a function IsValueEmpty that would check:
Function IsValueEmpty(ByVal value As String) As Boolean
If String.IsNullOrEmpty(value) Or value = "N/A" Then
Return True
End If
Return False
End Function
Could also Trim.
You could have the relevant message part stored in the Tag property of each textbox and use Linq :
Dim customerTextBoxes = {txt_customeracc, txt_customername, txt_customercontact}
Dim messages = Aggregate customerTextBox In customerTextBoxes
Where customerTextBox.Text = "" OrElse customerTextBox.Text = "N/A"
Select $"Please enter a correct {customerTextBox.Tag}")
Into ToArray
Then just check it's length against the initial one and if they're not equal aggregate the message for display
If customerTextBoxes.Length <> messages.Length Then error1 = String.Join(Environment.NewLine, messages)

Adding another condition to this conditional if statement

I have this if statement right here:
If lbl1.Text <> "Good" Or lbl2.Text <> "Good" Or lbl3.Text <> "Good" Then
MsgBox("Something.")
Exit Sub
End If
This works fine, but I also need to attach another condition to it, but for some reason I am drawing a blank on it. I need it to also pass that it is ok for lbl2 & lbl3 to be an empty string. In order words, if lbl1.text = "Good" then it is ok for lbl2 & lbl3 to be empty, therefore it will not exit sub.
If you look at it from the other way around, what you're saying is that if any one of the labels says "Good", then don't enter this statement, so, you could say enter this statement if that is not the case.
In other words:
If Not(lbl1.Text = "Good" Or lbl2.Text = "Good" Or lbl3.Text = "Good") Then
MessageBox.Show("Something.")
End If
Hope that does the trick!
What about a statement like this?
If Not ((lblA.Text = "Good" OrElse lblA.Text = String.Empty) AndAlso _
(lblB.Text = "Good" OrElse lblB.Text = String.Empty) AndAlso _
(lblC.Text = "Good" OrElse lblC.Text = String.Empty)) Then
MessageBox.Show("Something.")
End If

Two If statements inside each other in VBA

I tried to write two If statements inside each other in VBA, but it gives me wrong answer. When I debug it, it shows that the program doesn't go through the ElseIf and keeps going with the else of the first If.
How can I have two If statements working properly inside each other?
Code:
If ConfigBox.Value <> ... Then
If ListBox1.Value = ... Then
DO SOMETHING
Else
DO SOMETHING
End If
ElseIf ListBox1.Value = ... Or ListBox1.Value = ... Then
DO SOMETHING
Else
DO SOMETHING
End If
Your nesting is a bit off. You would want to do something like this:
If ConfigBox.Value <> ... Then
If ListBox1.Value = ... Then
Code
ElseIF ListBox1.Value = ... Or ListBox1.Value = ... Then
Code
Else
Code 'if ListBox1.Value doesn't meet above criteria
End If
Else
Code 'if ConfigBox criteria is not met. You could start another nested If for the ListBox1 here too.
End If
try with below
If ((ConfigBox.Value <> "1") And (ListBox1.Value = "2")) Then
'Do Something
ElseIf ((ConfigBox.Value <> "1") And (ListBox1.Value <> "2")) Then
'Do Something
ElseIf ListBox1.Value = "3" Or ListBox1.Value = "4" Then
'Do Something
Else
'Do Something
End If

creating a loop around my select Case

At present, I have a functioning Select Case that states that if a textbox is blank then it is to be highlighted red, but it only seems to be highlighting one of the textboxes. For instance, if 2 textboxes are left blank, it only highlights the first on it comes across.
Select Case True
Case Me.CustName = ""
Me.CustName.BackColor = &H8080FF
Case Me.RegAddress = ""
Me.RegAddress.BackColor = &H8080FF
Case Me.PostInput = ""
Me.PostInput.BackColor = &H8080FF
Case Me.Landline = ""
Me.Landline.BackColor = &H8080FF
Case Me.Contact = ""
Me.Contact.BackColor = &H8080FF
Case Me.DOBInput = ""
Me.DOBInput.BackColor = &H8080FF
End Select
Being new to VBA, my only thinking is to create a loop round my current code that state (loop until x,y or z is <> "") but I can't seem to figure out how to do this.
Any advice will be greatly appreciated.
Select Case runs the code block following the first matching Case statement only. If you need to check each of your conditions regardless, you should write them as individual If statements instead:
If Me.CustName = "" Then Me.CustName.BackColor = &H8080FF
If Me.RegAddress = "" Then Me.RegAddress.BackColor = &H8080FF
If Me.PostInput = "" Then Me.PostInput.BackColor = &H8080FF
....
You are using Select Case for the wrong purpose. Its purpose is to test a single expression and execute one branch based on the value of that expression.
What you need to do is test each of your text boxes individually, using if statements:
If Me.CustName = "" Then Me.CustName.BackColor = &H8080FF
'etc.