MS-Access How to use iif in query criteria - sql

I have a search form containing a text box and I want to filter a field based on value greater than value entered in text box but return all the field when the text box is empty.
I wrote below code in query criteria for this purpose but got error
IIf(IsNull(TextBox); TheField; >TextBox))

When specified as the criteria for a particular field within the query builder in MS Access, the expression you have entered will likely be interpreted as the following where clause:
where TheField = IIf(IsNull(TextBox), TheField, TheField > TextBox))
Which will likely yield a data type mismatch error if TheField does not hold a boolean value, since the else branch will yield a where clause of:
where TheField = (TheField > TextBox)
Which will be either of the following:
where TheField = True
where TheField = False
Instead, I would suggest specifying the following as your SQL where clause:
where Forms!YourFormName!YourTextBox is null or TheField > Forms!YourFormName!YourTextBox

I would recommend:
where (textbox is null or thefield > textbox)

Related

msaccess db field is zero length string but vba isnull() function returns null

NULLs again!!!!
I have set a field to zero length string if there is nothing and this applies to about half of the records.
Running the sql statement:
select * from contacts WHERE organisation is null
return no rows as expected.
But the VBA
Debug.Print Len(Me.Organisation)
If IsNull(Me.Organisation) Then
Label50.Caption = "null"
Else
Label50.Caption = Len(Me.Organisation)
End If
shows all the records with zero length organisation as null rather than 0
The field is defined on the db as short text with the following parameters:
Why is VBA telling me it is null when it is clearly not?
Programmatically changing property of a control affects ALL instances of control. Use a textbox with expression in ControlSource instead of VBA changing label.
=IIf(IsNull(Organisation), "Null", Len(Organization))
Setting a field to allow ZLS does not guarantee field contains ZLS when there is no data. I NEVER allow ZLS in fields.
Debug.Print Len(Me.Organisation)
If Me.Organisation.text=" " Then
Label50.Caption = "null"
Else
Label50.Caption = Len(Me.Organisation)
End If

VB.Net - Can I check if a string IsNumeric, and if it is convert it to a number to be compared with, all within one IF statements condition?

I have a database with a column labeled PrintOnPage. It can hold the values "ALL", "LAST", or a specific page number (1,2,3, etc), they are all stored as a string.
In my code I have the following IF statement:
If ((PrintOnPage.ToUpper = "ALL") OrElse
(PrintOnPage.ToUpper = "LAST" And iPageNo = (_indexDatabase.GetField("LastPage").Trim) OrElse
(IsNumeric(PrintOnPage) And CInt(PrintOnPage) = iPageNo)) Then
The last condition of the IF statement breaks if PrintOnPage is a string that cannot be converted to an INT. Because PrintOnPage can be LAST and iPageNo != to the last page specified in the database until the loop it is in iterated to the proper page, this is an issue.
Even if IsNumeric is False the second part of the condition after the AND is still checked, and thus breaks. I am trying to avoid nesting to many IF statements if I can.
Is there anyway to solve this issue within the same IF statement?
You can use AndAlso in order to short-circuit your If evaluation, just like you're using the OrElse:
If ((PrintOnPage.ToUpper = "ALL") OrElse
(PrintOnPage.ToUpper = "LAST" AndAlso iPageNo = (_indexDatabase.GetField("LastPage").Trim) OrElse
(IsNumeric(PrintOnPage) AndAlso CInt(PrintOnPage) = iPageNo)) Then
This way, if the first part IsNumeric(PrintOnPage) fails, it won't try to evaluate the following part(s).

In VB.NET Query on what to do when field is empty

What I have is a form behind a login and I want to pull in a value from our database for the NameField IF that value exists, otherwise leave it blank for the user to fill in.
So I have the following if statement which works IF there is a value in our database for AliasName:
If _NameID <> Guid.Empty Then
Name.Text = (From c In _Data.CONSTITUENTs
Where c.ID = _NameID
Select c.KEYNAME).FirstOrDefault().ToString()
NameField.Text = (From atc In _Data.CODEs
Join a In _Data.AliasName On atc.ID Equals a.CODEID
Where atc.DESCRIPTION = "Default Name" AndAlso a.CONSTITUENTID = _NameID
Select a.NAME).FirstOrDefault().ToString()
End If
What I now need to know is how to add an if statement to read if there is a value that exists in the AliasName field and the description matches "Default Name", and if so, use that value, otherwise, allow the user to enter their own value.
Right now it is pulling in the value when it exists, but erroring out the page when it does not.

Dataview filtering vb.net

Wondering if I am doing this filter right with the dataview, it keeps throwing me this error
Additional information: Filter expression '80' does not evaluate to a Boolean term.
but here is the code
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = TextBox1.Text
DataGridView1.DataSource = table
To filter something on a DataVIew you need to specify the column on which the filter should be applied, the operator for the comparison and the value that you want to use for the filtering. It seems that you have given only the value "80".
For example, assuming the column of interest is named "NumberOfPieces" and you have typed 80 in the textbox
Dim table = DataSet1.Tables("network")
table.DefaultView.RowFilter = "NumberOfPieces = " & TextBox1.Text
DataGridView1.DataSource = table
This will filter the view with all the rows that have the value (a numeric value) equals to 80 in the column "NumberOfPieces". You could use other operators like Greater/Lesser Than ( >= <= ) or more complex construct that are well detailed in the MSDN page about the Expression property of the DataColumn object

Rowfilter Select syntax using a value stored in a variable

I am using VB.NET and I need to use Datatable.Select to get a row which has a matching value.
Consider I have Datatable_A, which has many rows. I am interested to get row/rows which matches my search criteria. In this case, I must get a row (RowA), which has AnswerA stored in Column_A.
I know I can easily use the method below to find the answer:
RowA = Datatable_A.Select("Column_A = 'AnswerA'")
However, value 'AnswerA' is stored in Variable_A.
I have tried using
RowA = Datatable_A.Select("Column_A = 'Variable_A'")
Unfortunately, I still could not get the datarow, whose Column_A = 'Answer A'.
I have studied the explanation in this website DataView RowFilter Syntax [C#], but I could not find any clue there.
Thank you.
If you want to select a subset of your datatable rows using the Select method and a variable that contains the value to filter your rows you need to write
Dim Variable_A = "MySearchCriteria"
Dim rowsSelected = Datatable_A.Select("Column_A = '" & Variable_A & "'")
This means that you want all the rows in which the Column_A (a string column) contains exactly the value "MySearchCriteria" stored in the Variable_A. The filter condition is created concatenating the literal string describing the name of the column and the operator with the content of the Variable_A. Since Column_A is assumed to be a string column then you need to encapsulate the Variable_A between single quotes.
And remember that Select returns an array of DataRow that match the filter expression.
For Each row in rowsSelected
Console.WriteLine(row("Column_A").ToString)
Next