Results 1 to 20 of 20

Thread: [RESOLVED] RecordCount return -1

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    [RESOLVED] RecordCount return -1

    Hi all. Please take a look at code below. Just simple select the table but it return -1. What have to be fixed ? Please help...

    ### GLOBAL VARIABLE ###
    Code:
    Private db As ADODB.Connection
    Const DBNAME = "\DSDW2.mdb;"
    ### Sync Down BUTTON ###
    Code:
    Private Sub cmdSyncdown_Click()
    Dim ConnectionString As String
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & DBNAME
    
    Set db = New ADODB.Connection
    db.Open (ConnectionString)
    db.BeginTrans
    
    'CALL METHOD HERE THEN SET Success Flag
    Dim tmpQueryStr As String
    tmpQueryStr = "VALID SQL SELECT STATEMENT HERE"
    
    Dim result As Integer
    result = conn.Execute(tmpQueryStr).RecordCount '#### THIS LINE ... result = -1 ####
    
    If result > 0 Then
        tmpQueryStr = "MSACCESS TESTED SQL UPDATE STATEMENT HERE"
        ExecuteDB(tmpQueryStr)
    Else
        tmpQueryStr = "MSACCESS TESTED SQL INSERT STATEMENT HERE"
        ExecuteDB(tmpQueryStr)
    End If
    
    If Success Then
        db.CommitTrans
    Else
        db.RollbackTrans
    End If
    
    db.Close
    End Sub
    ### FUNCTION ###
    Code:
    Public Function ExecuteDB(ByVal QueryStr As String, ByRef db As ADODB.Connection) As ADODB.Recordset
        On Error GoTo ErrorHandling:
        Dim rs As New ADODB.Recordset
        Set rs = db.Execute(QueryStr)
        Set ExecuteDB = rs
        Exit Function
    ErrorHandling:
        MsgBox QueryStr
        Success = False
        Set ExecuteDB = Nothing
    End Function
    ### END OF CODES ###
    Last edited by ClOuD_Za; Oct 26th, 2009 at 02:21 PM. Reason: RENAME TOPIC TO RESOLVED

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

    Re: RecordCount return -1

    it means that you have a server side cursor.... there are records, but ADO cannot determine the exact number of records. If all you care about is if there are records, then as long as it is not 0, there are records.

    -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??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    Quote Originally Posted by techgnome View Post
    it means that you have a server side cursor.... there are records, but ADO cannot determine the exact number of records. If all you care about is if there are records, then as long as it is not 0, there are records.

    -tg
    Thank you for your reply.

    Would you mind to tell me which part of code do I have to chage ?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: RecordCount return -1

    Welcome to VBForums

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    In addition to what tg posted, if you use a Recordset object there is no reason for you to use .RecordCount at all - if you just want to check whether there are any records it is quicker (and uses less resources) to just check .EOF and .BOF, eg:
    Code:
    rs.Open "SELECT ...
    If Not(rs.EOF And rs.BOF) Then
      'there are records
    Else
      'no records
    End If
    I find it rather odd that you are running a Select statement but then ignore the actual data you get from it... it would almost certainly be more efficient (and cause fewer locking issues etc) to alter the Select statement slightly, and use a recordset.


    Quote Originally Posted by ClOuD_Za
    Would you mind to tell me which part of code do I have to chage ?
    This part:
    Code:
    If result > 0 Then

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    I changed some part of code into below

    vb Code:
    1. Dim result As New ADODB.Recordset
    2. result.ActiveConnection = db
    3. result.Open (tmpQueryStr)
    4.        
    5. If Not (result.EOF And rs.BOF) Then
    6. tmpQueryStr = "MSACCESS TESTED SQL UPDATE STATEMENT HERE"
    7.     ExecuteDB(tmpQueryStr)
    8. Else
    9.     tmpQueryStr = "MSACCESS TESTED SQL INSERT STATEMENT HERE"
    10.     ExecuteDB(tmpQueryStr)
    11. End If

    now ERROR : Object variable or With block variable not set

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

    Re: RecordCount return -1

    on what line? The only lines that aren't suspect for that error is 4, 8 & 11.... so which of the other 8 is the problem?

    -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??? *

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    OOPs.

    So Sorry TG. My mistake.

    At line 5 , I forgot to change "rs" to "result"

    I re-test again and came up with new error.

    "The database engine could not lock table because it is already in use by another person or process" on LINE 10

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

    Re: RecordCount return -1

    Take a CLOSE look at this line and think about what you told me about changing RS to RESULT.... then look at the line part by part...

    If Not (result.EOF And rs.BOF) Then

    To help with the other problem of the lock, I'd need to see ExecuteDB .... I suspect it is opening it's own connection to the database, but you have one open already... you should open it once, and use that same connection as needed.

    -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??? *

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    vb Code:
    1. Dim rs As New ADODB.Recordset
    2. Set rs = db.Execute(QueryStr)

    db is opened and I thought rs is just receive recordset result from db.execute ...

    I can't see any open new connection in the ExecuteDB Method.

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: RecordCount return -1

    Maybe this would help:

    Code:
    ...
    
    Dim tempRs as ADODB.Recordset
    Set tempRs = conn.Execute(tmpQueryStr)
    
    If Not (tempRs.EOF And tempRs.BOF) Then
        tmpQueryStr = "MSACCESS TESTED SQL UPDATE STATEMENT HERE"
        ExecuteDB(tmpQueryStr)
    Else
        tmpQueryStr = "MSACCESS TESTED SQL INSERT STATEMENT HERE"
        ExecuteDB(tmpQueryStr)
    End If
    
    ...
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: RecordCount return -1

    Quote Originally Posted by ClOuD_Za View Post
    vb Code:
    1. Dim rs As New ADODB.Recordset
    2. Set rs = db.Execute(QueryStr)

    db is opened and I thought rs is just receive recordset result from db.execute ...

    I can't see any open new connection in the ExecuteDB Method.
    On a side note, we should avoid New keyword when declaring objects in VB (VB6 and lower). This unnecessarily increases the overhead on the program as it checks for a valid instance of the variable on each use.

    Instead we should do like this:
    Code:
    Dim rs As ADODB.Recordset
    Set rs = db.Execute(QueryStr) 
    
    'or
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    rs.Open QueryStr ...
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    Thank you , I dint know that.

    Let me edit the code to be closer to real one that having problem. PLEASE USE BELOW CODE TO DISCUSS LATER

    vb Code:
    1. Dim result As ADODB.Recordset
    2. Set result = db.Execute(tmpQueryStr)
    3.        
    4. If Not (result.EOF And result.BOF) Then
    5.     tmpQueryStr = "MSACCESS TESTED SQL UPDATE STATEMENT HERE"
    6.     ExecuteDB(tmpQueryStr)
    7. Else
    8.     tmpQueryStr = "MSACCESS TESTED SQL INSERT STATEMENT HERE"
    9.     IncrementStr = "ALTER TABLE dbo_" & TableName & " ALTER COLUMN ID COUNTER(" & ID_No & ",1)"
    10.        
    11.     ExecuteDB(IncrementStr)
    12.     ExecuteDB(tmpQueryStr)
    13. End If

    The error "The database engine could not lock table because it is already in use by another person or process" is actually occured at the line "ExecuteDB(IncrementStr)". ( LINE 11 )

    The reason why I have to ALTER is I need to INSERT data ( which ID was set ) into the table which its ID is AUTO-INCREMENT.
    Last edited by ClOuD_Za; Oct 26th, 2009 at 01:29 PM.

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: RecordCount return -1

    Try this:
    (not tested code)
    Code:
    Private Sub cmdSyncdown_Click()
        Dim ConnectionString As String
        ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & DBNAME
        
        Set Db = New Adodb.Connection
        Db.Open (ConnectionString)
        Db.BeginTrans
        
        'CALL METHOD HERE THEN SET Success Flag
        Dim tmpQueryStr As String
        tmpQueryStr = "VALID SQL SELECT STATEMENT HERE"
        
        Dim result As Boolean, tempRs As Adodb.Recordset
        Set tempRs = conn.Execute(tmpQueryStr)
        result = Not (tempRs.EOF And tempRs.BOF)
        tempRs.Close
        
        If result Then
            tmpQueryStr = "MSACCESS TESTED SQL UPDATE STATEMENT HERE"
            ExecuteDB (tmpQueryStr)
        Else
            tmpQueryStr = "MSACCESS TESTED SQL INSERT STATEMENT HERE"
            ExecuteDB (tmpQueryStr)
        End If
        
        If Success Then
            Db.CommitTrans
        Else
            Db.RollbackTrans
        End If
        
        Db.Close
    End Sub
    But this still seems like too heavy on the database.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    "Heavy on Database" is ok because this database is used only in a PC.

    Have you looked at my latest post ?

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

    Re: RecordCount return -1

    The reason why I have to ALTER is I need to INSERT data ( which ID was set ) into the table which its ID is AUTO-INCREMENT.
    Woah! No you don't. that's the point of the AutoIncrement... you don't have to do anything with it, for it or anything... it'll increment on it's own.

    -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??? *

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    Quote Originally Posted by techgnome View Post
    Woah! No you don't. that's the point of the AutoIncrement... you don't have to do anything with it, for it or anything... it'll increment on it's own.

    -tg
    Let me explain it.

    1. I have a hundred of records in table ( ASSUMING THAT LATEST ID = 100 )
    2. I have to download records from server ( somewhere ) and INSERT INTO this database which ID start from 1.

    How could I do "2." If not delete records after that alter table to have ID = next record's ID then insert ?

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

    Re: RecordCount return -1

    if you already have records 1- 100.... why would you want to re-insert record ID 1? if it's a new record, it should get 101... which it will automatically. Otherwise, shouldn't it be an update?

    If your database isnt' the source of the IDs in the first place, then the field shouldn't be an auto increment, but should simply be a number, at which point, you can then isnert it w/o needing to alter the table.

    -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??? *

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    Quote Originally Posted by techgnome View Post
    if you already have records 1- 100.... why would you want to re-insert record ID 1? if it's a new record, it should get 101... which it will automatically. Otherwise, shouldn't it be an update?

    If your database isnt' the source of the IDs in the first place, then the field shouldn't be an auto increment, but should simply be a number, at which point, you can then isnert it w/o needing to alter the table.

    -tg
    reasons...
    1.this table has to be linked with other table at runtime.

    2.1 We have a lot of PCs running this APP and we have only 1 Server
    2.2 Every PCs have to Sync its stored data to Server
    2.3 After Sync Up then Sync Down all data from Server ( which also came from other PC ) to its database

    3. This database is designed by my customer. They don't allow me to change it. So Sad

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    21

    Re: RecordCount return -1

    Except the line of ALTER. Now Code is working great.

    Is there a way to mark a thread as SOLVED and mark a post as SOLUTION ?

    sry , I'm new to here

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] RecordCount return -1

    You can mark the thread as Resolved by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

    However, it doesn't seem to me as if this is fully resolved, as there are things which could almost certainly be done in a better way... While you have replied to tg's post, you haven't actually given a valid justification for the method you are using - especially the desire to alter the table at all during the process.

Tags for this Thread

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