Results 1 to 27 of 27

Thread: Getting RecordCount [Resolved] (almost)

  1. #1

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Getting RecordCount [Resolved] (almost)

    In my query, i was using .recordcount to set the records. now I wanted to get a count in a msgbox, so I printed it, but found out that it was always -1.
    Is there a way to get the count accurately, or do I have to do a separate query?
    Last edited by dglienna; Feb 22nd, 2005 at 06:43 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Getting RecordCount

    The -1 tells you that your current cursor type, lock type, etc do not support the
    recordcount property. How are you opening your rs?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    VB Code:
    1. With oDoc.MailMerge
    2.         .MainDocumentType = wdMailingLabels
    3.         .OpenDataSource Name:=App.Path & "\MyData.mdb", LinkToSource:=True, Connection:="TABLE MyTemp", _
    4.          SQLStatement:="Select [First Name], [Last Name], [Mailing Address], City, State, Zip From [MyTemp]"
    5.     End With
    6.     With oDoc.MailMerge.DataSource
    7.         .FirstRecord = 1
    8.         .LastRecord = .RecordCount
    9.         RecCount = .RecordCount
    10.     End With

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Getting RecordCount

    Use the .RecordCount in your With block and save the value to a variable. Then you can use it
    later on in your msgbox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    .RecordCount says "Method or Data Method not found"

    I placed it below the sql statement.

    RecCount = .RecordCount

    EDIT: .Datasource.Record produced the same -1
    Last edited by dglienna; Feb 21st, 2005 at 03:33 PM.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Getting RecordCount

    But doesnt it work for the .LastRecord = .RecordCount?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    nope. just works with the -1

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Getting RecordCount

    It works for mine so I double checked in the help file and this its what I found.
    If Microsoft Word cannot determine the number of records in a data source, the RecordCount property will return a value of -1
    So it may depend on the way datasource was setup or if its a complex query???
    What is your datasource? .odc?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    I've used this very non-standard trick at times to arrive at a recordcount - if the number of rows in the table is low - it won't matter that this is a ugly query...

    Code:
    Select [First Name], [Last Name], [Mailing Address], City, State, Zip
    ,(Select Count(*) From [My Temp]) From [MyTemp]
    Thus getting the recordcount as a column in the recordset itself...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    It actually works for the complex query that isn't part of the mailmerge. (the one that sends email or printer output) I guess that I'll have to do a separate query to get the count. That's what I was trying to avioid if it wasn't necessary.

    Thanks

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    If you are going to go the separate query route, then my technique guarantees that the recordcount will truly match the row count in the "data" recordset. If you do two separate queries, then you could run the risk of row counts changing during the operation. We would have that happen in our applications - they load lots of rows in a given day!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    How do I refer to the Count() field in my RS ? I forgot that I had already done it with the complex query. Now I need it for the simple query. If I can add the count like szlammy said, I'd like to do that.

    EDIT: I have this

    VB Code:
    1. rs.Open "Delete TempTable .* from TempTable ", cn, adOpenKeyset, adLockOptimistic, adCmdText
    2.  
    3.   rs.Open "INSERT INTO TempTable (RecCount) " & _
    4.      "SELECT  Count(*)" & _
    5.      "From FarmingTemp", cn, adOpenKeyset, adLockOptimistic, adCmdText
    6.   RecCount = rs.Fields(1)

    So how do I get it back out. I've also tried RecCount = rs.fields("RecCount") but that returns an error.
    Last edited by dglienna; Feb 21st, 2005 at 04:37 PM.

  13. #13

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    I think I figured it out:

    VB Code:
    1. rs.Open "select RecCount from TempTable", cn, adOpenKeyset, adLockOptimistic, adCmdText
    2.  
    3.   RecCount = rs.Fields("RecCount")

  14. #14

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    Still like to know how to use the example that slammy suggested.

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    Quote Originally Posted by dglienna
    Still like to know how to use the example that slammy suggested.
    I am not familiar with the WORD object you are using - it appears that you don't have access to the RS from the code you posted...

    Where is the execute or open of the recordset?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    word takes care of it all.

    it I do a separate query, how do I access the count()? Where is it placed?
    The only way I could get a count is my later code that placed it into a seperate field in a new table. If I use it like you did? How do I retrieve it?

    I think that I see that you are putting it into an extra field in the table? I'm not sure, though.

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    Code:
    rs.Open "SELECT  Count(*) From FarmingTemp" _
        , cn, adOpenKeyset, adLockOptimistic, adCmdText
    RecCount = rs.Fields(0)
    OR

    Code:
    rs.Open "SELECT  Count(*) "RecCount" From FarmingTemp" _
        , cn, adOpenKeyset, adLockOptimistic, adCmdText
    RecCount = rs.Fields("RecCount")

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    No, I meant from THIS CODE:
    VB Code:
    1. Select [First Name], [Last Name], [Mailing Address], City, State, Zip
    2. ,(Select Count(*) From [My Temp]) From [MyTemp]

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    Actually - I didn't mean to leave the FROM as FARMINGTEMP - the FROM should be the real table you are getting this data from.

    There is no need for a temporary table in this example.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    Quote Originally Posted by dglienna
    No, I meant from THIS CODE:
    VB Code:
    1. Select [First Name], [Last Name], [Mailing Address], City, State, Zip
    2. ,(Select Count(*) From [My Temp]) From [MyTemp]
    In that code it's the 6th column in the recordset - but I don't see from that WORD object code you showed above where you actually get the RECORDSET in the VB side of things...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  21. #21

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount

    Does it store it is every record, or the last one?
    When I query a recordset, does it use the last one if there is more than one?
    If I run a query again, and the old count was in there, and I added the new count, when I do a query to select *, would that use the second (last) one?

    I think I'm missing something concerning RS vs flatfile. In a flat file, I can go to record two to see all new info. How does this relate?

  22. #22
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount

    Quote Originally Posted by dglienna
    Does it store it is every record, or the last one?
    When I query a recordset, does it use the last one if there is more than one?
    If I run a query again, and the old count was in there, and I added the new count, when I do a query to select *, would that use the second (last) one?

    I think I'm missing something concerning RS vs flatfile. In a flat file, I can go to record two to see all new info. How does this relate?
    Yes - it really stores the value in each and every row - that's why I said it was non-conformist (I've been accused of that before )

    When you run the query again, it's a freshly calculated sub-query value.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  23. #23

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount [Resolved] (almost)

    My RecCount had about 8 records in it, from each time I ran, and put in 1 record. I ran a query 8 times. The table had only the count field in it.

    I just started deleting everything each time I ran the query, but I still wanted ask.

    I added RecCount to TempTable, without deleting the values each time.

    When i ran the query, would it always return the last record/field?

  24. #24
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Getting RecordCount [Resolved] (almost)

    I'm not understanding what you mean by ran it about 8 times and what you mean by deleting everything.

    Are you creating a TEMP TABLE with the rows you want to process? Could you show some real code so I can grasp this a bit better?

    I'm not a VBA person - so maybe I'm confusing something in regard to that.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  25. #25

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount [Resolved] (almost)

    I just used count() to get a record count, and put it into a table.
    Here:
    VB Code:
    1. rs.Open "INSERT INTO TempTable (RecCount) " & _
    2.      "SELECT  Count(*)" & _
    3.      "From FarmingTemp", cn, adOpenKeyset, adLockOptimistic, adCmdText

    After I ran it, I had 8 records in the table, each with a different number of records in it. Then, I was getting the number from the table:

    VB Code:
    1. rs.Open "select RecCount from TempTable", cn, adOpenKeyset, adLockOptimistic, adCmdText

    I wanted to know what this was returning? Was it the last record, or the first?
    I just wanted to know a general rule of thumb as to how it all worked.

    Now, all is good, as I delete all records before I write any, so the problem won't arise again. I just wonder what would come up if I didn't delete all of the records each time.

    Was it the first record, or the last record?

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Getting RecordCount [Resolved] (almost)

    The "SELECT Count(*)" part gets you the number of records in the FarmingTemp table. So you
    should have 8 in your RecCount field for all 8 records.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting RecordCount [Resolved] (almost)

    Hmm. That makes sense, but I thought they were different numbers, and selecting the count only added one record. I'm going to do it again to see exactly what is happening. Thanks.

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