473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Next not being executed in a For Each statment

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
8 1258
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
5
by: Louis | last post by:
I'm running the following code but it keeps failing because Select isn't a part of the action query method: Private Sub Command2_Click() Dim SQLStr As String SQLStr = "SELECT...
1
by: Me, Myself, and I | last post by:
First off, i apologize if my terminology is off... I am currently in a project that is basically a front-end to a database. In coding this, I am taking into account that it has the *potential*...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
1
by: zeebiggie | last post by:
Good morning I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt...
4
esimond
by: esimond | last post by:
Hi All ! Just joined this big community, and a BIG Swiss Hello in there ! Having recently switched from VB to C#, I indeed still have to discover all the powerful sides of that great...
19
by: kimiraikkonen | last post by:
Hi, I want to find out if there's difference between "On Error Resume Next" error handler and leaving "catch" block empty in a try-catch-end try block to ignore exceptions which i don't approve of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
11
by: fniles | last post by:
In VB 6 I can do the following: Sub MySub on error goto Err1 : --all my codes are here : --say this is where the error occurs : --this is where resume next will bring me after Err1 exit sub...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.