Next not being executed in a For Each statment

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code and it is not passing through the Next statement:

Private Function IsItemAlreadyAdded() As Boolean
Dim drMyRow As DataRow

With Me
For Each drMyRow In .dsTOB.Tables("TempOB").Rows
If .acctNumber = drMyRow("AccountNo") Then
Return True
Else
Return False
End If
Next
End With

End Function

I call the function in the following code to make sure that an item added
already in the temporary table is not added again:

Private Sub AddToTempOpenBalance()
With Me
Try

If .IsItemAlreadyAdded = True Then
Dim msg1 As New MessageDialogForm
msg1.DialogCaption = "Data Entry"
msg1.DialogMessage = "Please the account is already added"
msg1.ShowDialog()
.txtOpenBalance.Focus()

Else

If .IsAllFieldsNotComplete = True Then
Dim msg As New MessageDialogForm
msg.DialogCaption = "Data Entry"
msg.DialogMessage = "Please complete all fields and
continue"
msg.ShowDialog()
.txtOpenBalance.Focus()

Else


Dim pcRow As DataRow = .dtTempOB.NewRow()
pcRow("Date") = .dtpDate.Value

If .rbOtherAccounts.Checked = True Then
pcRow("AccountNo") = .otherAcctNo
End If

If .rbBankAccounts.Checked = True Then
pcRow("AccountNo") = .bankAcctNo
End If

If .chkCredit.CheckState = CheckState.Checked Then
pcRow("CreditBalanceBF") = .txtOpenBalance.Text
End If

If .chkDebit.CheckState = CheckState.Checked Then
pcRow("DebitBalanceBF") = .txtOpenBalance.Text
End If

Me.dtTempOB.Rows.Add(pcRow)

End If
End If

Catch ex As Exception
Dim msg As New MessageDialogForm
msg.DialogCaption = "Add Record"
msg.DialogMessage = "An error of type " &
ex.GetType().ToString() & _
" occured while adding record to temporary table."
msg.ShowDialog()
End Try
End With
End Sub

The "For Each Next" code is not able to loop through the temporary table to
compare the acctNumber with account numbers whether it is already in the
temporary table.

The acctNumber variable is the current account number selected by the user
which is being compared with the various account numbers in the temporary
table.
 
when you write:
Return True
it is equal to saying "stop here, get out of the function, and who ever
called me handover the value True to it."
When you write:
Return False
it means same as above but with value False.
So ofcourse when you do a return in the If and the Else statement, your
function will never reach the Next.

Hope that helps :-)
Abubakar.
http://joehacker.blogspot.com
 
Belee,

About your question I have nothing to add to the answer from Abubakar,
however you use consequently "With Me."

This is an only confusing statement, because this is standard so never
needed.

And now I am typing: about your procedure I assume that what you want is
something as (check yourself the condition):

Private Function IsItemAlreadyAdded() As Boolean
Dim drMyRow As DataRow
For Each drMyRow In dsTOB.Tables("TempOB").Rows
If .acctNumber <> drMyRow("AccountNo") Then
Return True
Next
Return False
End Function

I hope this helps?

Cor
 
Can you please show me how I can let the function return true if the account
is already there?
 
Thank you so much for the idea. I moved the if statment out of the for each
next loop and it works now.

Thank you again
 
your code should be something like:
Private Function IsItemAlreadyAdded() As Boolean
Dim drMyRow As DataRow

With Me
For Each drMyRow In .dsTOB.Tables("TempOB").Rows
If .acctNumber = drMyRow("AccountNo") Then
Return True
''''''else => let the loop continue
End If
Next
''''if we have reached after next this mean accountNo was
''''not found so return false
Return False
End With

End Function

Hope the helps.
Abubakar.
http://joehacker.blogspot.com
 
Belee said:
I have the following code and it is not passing through the Next statement:

Try this out:

Private Function IsItemAlreadyAdded() As Boolean
Dim drMyRow As DataRow
Dim returnValue as Boolean = False ' not needed, but clear

With Me
For Each drMyRow In .dsTOB.Tables("TempOB").Rows
If .acctNumber = drMyRow("AccountNo") Then
returnValue = True
Exit For
End If
Next
End With

Return returnValue
End Function

The single exit point for the function adds clarity, and can help you avoid
this pitfall in the future...

Best Regards,

Andy
 
Belee,
In addition to the other comments:

Is AccountNo the primary key of the DataTable?

I would make AccountNo the primary key of the DataTable then its simply a
matter of using the Contains function.

Something like:
Private Function IsItemAlreadyAdded() As Boolean
Return Me.dsTOB.Tables("TempOB").Rows.Contains(me.acctNumber)
End Function

To make AccountNo the primary key of the DataTable you can use:

Me.dsTOB.Tables("TempOB").PrimaryKey = New DataColumn()
{Me.dsTOB.Tables("TempOB").Columns("AccountNo")}

Which is short hand for:
Dim keys(0) As DataColumn
keys(0) = Me.dsTOB.Tables("TempOB").Columns("AccountNo")
Me.dsTOB.Tables("TempOB").PrimaryKey = keys

I would set the primary key once when I created the DataTable.

Note keys(0) says an array of 1 element, the 0 is upper bound not number of
elements.

Hope this helps
Jay
 
Belee,
I should add that you can use a DataView & DataView.Find if AccountNo is not
the primary key.

Something like:

Private viewTempOB As DataView

Public Sub New()
' create dsTOB
viewTempOB = New DataView(dsTOB.Tables(""))
viewTempOB.Sort = "AccountNo"
End Sub

Private Function IsItemAlreadyAdded() As Boolean
Return Me.viewTempOB.Find(Me.acctNumber) said:
End Function

The Find method returns -1 if the row is not found, otherwise it returns the
index of the row found.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top