Results 1 to 17 of 17

Thread: [RESOLVED] FindNext temination

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Resolved [RESOLVED] FindNext temination

    Please could someone help me understand how to terminate a loop using the FindNext method in VBA. I'm trying to search through sets of records in a recordset to add up some values when my search criteria are met, so want the FindNext loop to terminate when the search criteria are no longer met. I'm sure this is a very simple request but can't work out what to do. All pointers appreciated.

    Thank you.

    PHB

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: FindNext temination

    Quote Originally Posted by PHB010108
    how to terminate a loop
    Wouldn't you use the Exit keyword? Such as Exit For?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FindNext temination

    What does the FindNext method in VBA have to do with VB.NET?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by Fromethius
    Wouldn't you use the Exit keyword? Such as Exit For?
    Don't think so, need to know when the FindNext completes ....

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by jmcilhinney
    What does the FindNext method in VBA have to do with VB.NET?
    Don't know. Are you telling me I'm asking in the wrong place? or are you trying to be helpful?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FindNext temination

    Are you asking for help with VB.NET code? If not then you're in the wrong forum, given that this is the VB.NET forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by jmcilhinney
    Are you asking for help with VB.NET code? If not then you're in the wrong forum, given that this is the VB.NET forum.

    Yes I'm asking for help with VB.NET code. The FindNext method seems to be valid for working with RecordSets in the VB.NET environment.

    Can you help?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FindNext temination

    Well it's got nothing to do with VBA then. It's a member of the Recordset class.

    In what context are you using a Recordset? It would generally be advisable to forgo ADO altogether and use ADO.NET instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by jmcilhinney
    Well it's got nothing to do with VBA then. It's a member of the Recordset class.

    In what context are you using a Recordset? It would generally be advisable to forgo ADO altogether and use ADO.NET instead.

    Thanks. I've got a (large) set of records with multiple entries having the same date. I'm trying to subtotal the value of a particular field for the current date then write a record back to the record set for the next date using the subtotalled value, ie a simple bit of summation SQL won't work because each date sub-total is dependent on the previous date.

    I'm therefore trying to use FindNext to find all records for a particular date so that they can be subtotalled. The problem I'm having is in understanding how to know when the FindNext "fails", ie does not find another record for the current date, ie so that I'm able to know to do the calculations/write the new record for the next date.

    Hope that makes sense? - that you can help.

    Many thanks.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: FindNext temination

    Well, if you truly are using .NET, then you should be using ADO.NET and not ADO ... Then you would be able to do something like this:

    Code:
    For Each dRow As DataRow In myDataTable
    .
    .
    .
    'Do a calculation with dRow
    .
    .
    .
    Next
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: FindNext temination

    Lose the recordset, pull out the value in a DataTable. Use the DataTable.Compute method to sum up all the values where the date column has the date you need.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: FindNext temination

    Why not do the whole thing in one query? Aren't you just getting the SUM of one field WHERE date = x? That could be written in a single SQL statement and returned using ExecuteScalar. However, if there are other needs for the records other than just the sum, then the datatable solution might well be better.
    My usual boring signature: Nothing

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FindNext temination

    If you need to do multiple calculations then you'd get all your data into a DataTable and then use its Compute method, e.g.
    vb.net Code:
    1. Dim sum As Integer = myDataTable.Compute("SUM(Amount)", "[Date] = #1/01/2008#")
    Follow the Data Access link in my signature to see how to get the data into the DataTable in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by Shaggy Hiker
    Why not do the whole thing in one query? Aren't you just getting the SUM of one field WHERE date = x? That could be written in a single SQL statement and returned using ExecuteScalar. However, if there are other needs for the records other than just the sum, then the datatable solution might well be better.
    Do indeed need other records which will be created as part of the partial summation of previous dates so seems like datatable is the way to go.

    Thanks for your response.

  15. #15

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by techgnome
    Well, if you truly are using .NET, then you should be using ADO.NET and not ADO ... Then you would be able to do something like this:

    Code:
    For Each dRow As DataRow In myDataTable
    .
    .
    .
    'Do a calculation with dRow
    .
    .
    .
    Next


    -tg
    Thank you for your response

  16. #16

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    8

    Re: FindNext temination

    Quote Originally Posted by jmcilhinney
    If you need to do multiple calculations then you'd get all your data into a DataTable and then use its Compute method, e.g.
    vb.net Code:
    1. Dim sum As Integer = myDataTable.Compute("SUM(Amount)", "[Date] = #1/01/2008#")
    Follow the Data Access link in my signature to see how to get the data into the DataTable in the first place.

    Fantastic help - much appreciated, especially the Data Access link, then further links to the connection strings - fantastic!

    (used to be a "classic" Fortran programmer working on giant geophysical modelling simulators so VB is a bit of a learning curve for me - will come back to you if get stuck if you don't mind)

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] FindNext temination

    I should point out that Compute returns an Object reference because it can return different types. That means that with Option Strict On, which it should be, you would need to cast the result as its actual type, e.g.
    vb.net Code:
    1. Dim sum As Integer = CInt(myDataTable.Compute("SUM(Amount)", "[Date] = #1/01/2008#"))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width