IIF-Else statement Never Reachs Else Condition - vba

Im using this custom Code in SQL Reporting Services. The problem is it NEVER reachs the false condition. If the condition is NOT true, the function won't return "false". It works fine when the value is True (prints "X") , otherwise I get an #Error .
I'm calling the function from a textbox :
= IIF(Code.CodeExist(Variables!MyCode.Value) = true, "X", "")
function CodeExist( ByVal Codigo As String) as Boolean
dim i as integer
dim Centinela as integer
i = 0
Centinela = 0
for i = 0 To Report.Parameters!CodeList.Count()
if Report.Parameters!CodeList.Value(i) = Codigo Then
Centinela = 1
Exit For
End If
Next
If Centinela = 1 Then
Return true
Else
Return false // IT NEVERS RETURN FALSE even if Centinela = 0
End If
End function
I don't know what's happening. Thanks in advance.

VBA has no "Return" statement. To return a value you'd use (eg)
....
If Centinela = 1 Then
CodeExist = True
Else
CodeExist = False
End If
End Function
or (much tidier):
....
CodeExist = (Centinela = 1)
End Function

Related

visual basic .net Function have error return

So the error is BC42353 ( Function ValidateInputFields doesn't return a value on all code paths. Are you missing a Return statement? ) I'm getting this error twice. I put exclamation points on the lines that it says the errors are on.
Private Function CekBarcode(sID As String, cost As Long, price As Long, ByVal vslist As DataGridView) As Boolean
Dim i As Long
Try
For i = 1 To vslist.Rows.Count - 1
If sID = vslist.Rows(i - 1).Cells(1).Value Then
' CekBarcode = True
vslist.Rows(i - 1).Cells(10).Value = vslist.Rows(i - 1).Cells(10).Value + 1
Return True
Exit For
Else
Return False
End If
Next
Catch
Return False
End Try
End Function
Private Function CekBarcode(sID As String, cost As Long, price As Long, ByVal vslist As DataGridView) As Boolean
Dim result As Boolean = False
'maybe to add this below line /check is there more than one row in grid
If vslist.Rows.Count < 2 Then Return result
Try
For i = 1 To vslist.Rows.Count - 1
If sID = vslist.Rows(i - 1).Cells(1).Value.ToString() Then
' CekBarcode = True
vslist.Rows(i - 1).Cells(10).Value = vslist.Rows(i - 1).Cells(10).Value + 1
result = True
Exit For
End If
Next
Catch
result = False
End Try
Return result
End Function
You don't seem to use either cost or price and you probably want to specify ByRef on the DGV to update it.
Private Function CekBarcode(sID As String, ByRef vslist As DataGridView) As Boolean
Dim result As Boolean = False
Try
For Each row As DataRow In vslist.Rows
If row.Item(1).Equals(sID) Then
row.Item(10) = CInt(row.Item(10)) + 1
result = True
Exit For
End If
Next row
Catch
'result = False ' Not required - result is only changed within the If
End Try
Return result
End Function
Or you can dispense with the result variable and Exit For:
Private Function CekBarcode(sID As String, ByRef vslist As DataGridView) As Boolean
Try
For Each row As DataRow In vslist.Rows
If row.Item(1).Equals(sID) Then
row.Item(10) = CInt(row.Item(10)) + 1
Return True
End If
Next row
Catch
' Fall through to return False
End Try
Return False
End Function

Filter ListView data with CheckBox inputs

I'm trying to provide a way to filter data within a ListView using CheckBox controls. Checking a box will add or remove the selected attribute from the ListView.
The current code:
For i = 0 To RecCount
Filter = True
'RecordDat array populated here
'filter the record by checkbox selection
If ckbComplete.Checked = True And Len(RecordDat(7)) = 0 Then
Filter = False
ElseIf ckbOpen.Checked = True And Len(RecordDat(7)) > 0 Then
Filter = false
ElseIf ckbApps.Checked = True And strings.Left(RecordDat(0).ToString, 4) <> "APPS" Then
Filter = False
ElseIf ckbContract.Checked = True And Strings.Left(RecordDat(0), 3) <> "SOP" Then
Filter = false
End If
If Filter = False Then GoTo SkipItem
' More code adding the item to the list view and formatting
SkipItem:
Next
At the moment I can filter only once per array index, i.e. I can filter ckbComplete and ckbApps but not ckbcomplete, ckbApps and ckbContract.
I managed to figure out a solution by trial and error and wish to share what I found.
In order to filter the data, each variable (in this case, the column value) required its own filter statements, each which must be encased in an If ELSE statement.
The resulting code was as follows:
Dim Filter As Boolean = True
For i = 0 To RecCount
Filter = False
'RecordDat initialisation
Filter = FilterPass(RecordDat)
If Filter = False Then Continue For
Dim itm As ListViewItem
itm = New ListViewItem(RecordDat)
Healthchart.ListView1.Items.Add(itm)
'Continue formatting and input of data into listview
Next
The FilterPass functions then follow:
Private Function FilterPass(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbComplete.Checked = True Then
If Len(RecordDat(7)) > 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
If Healthchart.ckbOpen.Checked = True Then
If Len(RecordDat(7)) = 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
Return filter
End Function
Private Function FilterPass2(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbApps.Checked = True Then
If Strings.Left(RecordDat(0).ToString, 4) = "APPS" Then
filter = True
End If
End If
If Healthchart.ckbContract.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "SOP" Then
filter = True
End If
End If
If Healthchart.ckbTech.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "ATS" Then
filter = True
End If
End If
If Healthchart.ckbDes.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "DES" Then
filter = True
End If
End If
Return filter
End Function
Private Function FilterPass3(ByVal RecordDat() As String)
Dim filter As Boolean = True
Dim SubDate As Date = Date.ParseExact(RecordDat(1).ToString, "dd/MM/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
If Healthchart.ckbLast30.Checked = True Then
If DateAdd(DateInterval.Day, -30, Now) > SubDate Then
filter = False
End If
End If
If Healthchart.ckb7Days.Checked = True Then
If DateAdd(DateInterval.Day, -7, Now) > SubDate Then
filter = False
End If
End If
Return filter
End Function
One can then add as many 'filters' as required by adding an additional If Filter = true then into the FilterPass function.

vba select values in listview and show in textbox

I want to display the value of 3rd column instead of 1st column. Pleae advise as to whan changed is required.
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
For i = 1 To ListView16.ListItems.Count
If (ListView16.ListItems(i).Selected) Then
If (Not blnFoundFirstItem) Then
TextBox118.Text = ListView16.ListItems(i).Text
blnFoundFirstItem = True
Else
TextBox118.Text = ListView16.ListItems(i).Text
End If
End If
Next i
Use ListSubItems() :
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
For i = 1 To ListView16.ListItems.Count
If (ListView16.ListItems(i).Selected) Then
If (Not blnFoundFirstItem) Then
TextBox118.Text = ListView16.ListItems(i).ListSubItems(3).Text
blnFoundFirstItem = True
Else
TextBox118.Text = ListView16.ListItems(i).ListSubItems(3).Text
End If
End If
Next i

Visual Basic - function to check if number is binary or not

Im code newbie and im stuck with this code. It appears that i always get True as response from this function. What am i doing wrong ?
Private Function binary() As Boolean
Dim number, temp As Integer
Dim status As Boolean
TextBox1.Text = number
status = True
While (True)
If (number = 0) Then
Exit While
Else
temp = number Mod 10
If (temp > 1) Then
status = False
Exit While
End If
number = number / 10
End If
End While
Return status
End Function
You have your assignment the wrong way around:
TextBox1.Text = number
With this, number will always be 0, its initial value, so your While loop exits immediately, every time. It should be:
number = Convert.ToInt32(TextBox1.Text)
Or better yet, pass it in as a parameter to the function:
Private Function binary(number as Integer) As Boolean
Dim temp As Integer
Dim status As Boolean
status = True
While (True)
If (number = 0) Then
Exit While
Else
temp = number Mod 10
If (temp > 1) Then
status = False
Exit While
End If
number = number / 10
End If
End While
Return status
End Function
Then:
Dim isBinary as Boolean
isBinary = binary(Convert.ToInt32(TextBox1.Text))

Function giving warning saying no return values

Function LoginPass() As Boolean
Dim LogPass As New waxClass
Dim Ldt As DataTable = LogPass.LoginPass(LCase(UserName_TextBox.Text),
LCase(UserPass_TextBox.Text))
If Ldt.Rows.Count > 0 Then
waxClass.LMUser = Ldt.Rows(0).Item("Username").ToString
'Utility.LMUserID = Ldt.Rows(0).Item("UserID").ToString
Return True
End If
End Function
When I try to use the above function it gives a warning saying:
Function 'LoginPass' doesn't return a value on all code
paths. Are you missing a 'Return' statement?
Your function requires a Boolean return value.
You do not have a return value if Ldt.Rows.Count <= 0, so that is where the warning is coming from. Add and Else Return False and you should be fine.
If Ldt.Rows.Count > 0 Then
waxClass.LMUser = Ldt.Rows(0).Item("Username").ToString
'Utility.LMUserID = Ldt.Rows(0).Item("UserID").ToString
Return True
Else
Return False
End If