CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    12

    Problem with VB6 and msaccess query

    I am writing a code which extracts data from a msaccess database to create certain report. For this purpose I wrote couple of queries and tried them in msaccess. One of the query has multiple occurance of 'avg()' function in it. This query runs fine in msaccess but gives error in vb6. I am using a cursor to fetch the data.

    Following is my code :
    Code:
    Public Function get_avg_in_table(ByRef dbcon As ADODB.Connection, ByVal eventid As Integer, ByVal scriptid As Integer) As Integer
        Dim recordset_avg As ADODB.Recordset
        Dim create_cmd As ADODB.Command
        Dim iLoop As Integer
        Set create_cmd = New ADODB.Command
       
        Set recordset_avg = New ADODB.Recordset
        recordset_avg.Source = "SELECT Breakdown_meter.[Event ID],Avg(Breakdown_meter.Value),Avg(Breakdown_meter.Connection) FROM Breakdown_meter WHERE Breakdown_meter.[Script ID]=26 group by Breakdown_meter.[Event ID]"
        
        With recordset_avg
            .CursorLocation = adOpenStatic
            .CursorType = adOpenDynamic
            .LockType = adLockOptimistic
            Set .ActiveConnection = dbcon
        End With
        
        'This is where i get error "unable to open the cursor error"
        recordset_avg.Open
        
        MsgBox recordset_avg.RecordCount
        
        For iLoop = 0 To (recordset_avg.RecordCount - 1)
            Debug.Print recordset_avg.Fields(0).Value & "     " & recordset_avg.Fields(1).Value
            recordset_avg.MoveNext
        Next
        recordset_avg.Close
    
    End Function

    I want to average around 8 columns.
    can anyone please help?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Problem with VB6 and msaccess query

    .CursorLocation = adOpenStatic ?

    Try

    .CursorLocation=adUseClient

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Problem with VB6 and msaccess query

    CursorLocation can only be set to one of these values

    adUseClient
    adUseServer

    You have mistakenly written adOpenStatic which is why your code is not working.

  4. #4
    Join Date
    Mar 2009
    Posts
    12

    Unhappy Re: Problem with VB6 and msaccess query

    Tried both adUseClient and adUseServer. still not working.

    I get following error;

    Runtime error '-2147467259(80004005)':
    Method 'Open of object ' _Recordset failed

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Problem with VB6 and msaccess query

    Well you got past one error, It was the cursor that was giving the error before and now you are getting a recordset error. I do not use the method you are using to open the recordset so I can not tell if you have something out of wack here, hopefully someone else who uses this method will chime in.

  6. #6
    Join Date
    Mar 2009
    Posts
    12

    Re: Problem with VB6 and msaccess query

    Quote Originally Posted by DataMiser View Post
    Well you got past one error, It was the cursor that was giving the error before and now you are getting a recordset error. I do not use the method you are using to open the recordset so I can not tell if you have something out of wack here, hopefully someone else who uses this method will chime in.
    can you tell me about your method? i can change my code accordingly.
    a small code snippet would be really helpful.

  7. #7
    Join Date
    Mar 2009
    Posts
    12

    Re: Problem with VB6 and msaccess query

    Its resolved. One of my columns had space at the end.

    Thanks for the info but Datamiser please tell me your method.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Problem with VB6 and msaccess query

    I normally use the single line method for opening the recordset.

    Code:
    Dim db As New Connection
    Dim RS As New Recordset
    
    db.CursorLocation = adUseClient
    db.Open PioneerCN
    
    RS.Open "select * from specieslist order by description", db, adOpenStatic, adLockOptimistic

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

Featured