By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,640 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,640 IT Pros & Developers. It's quick & easy.

Next not being executed in a For Each statment

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" wrote:
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.

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
Can you please show me how I can let the function return true if the account
is already there?

"Abubakar" wrote:
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" wrote:
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.

Nov 21 '05 #4

P: n/a
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

"Abubakar" wrote:
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" wrote:
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.

Nov 21 '05 #5

P: n/a
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" wrote:
Can you please show me how I can let the function return true if the account
is already there?

"Abubakar" wrote:
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" wrote:
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.

Nov 21 '05 #6

P: n/a
"Belee" <Be***@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.com...
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
Nov 21 '05 #7

P: n/a
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.acctNum ber) 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" <Be***@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.com... 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.

Nov 21 '05 #8

P: n/a
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) <> -1 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

"Belee" <Be***@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.com... 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.

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.