Results 1 to 16 of 16

Thread: Here's My Problem... "Refresh ADO" (RESOLVED)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005

    Exclamation Here's My Problem... "Refresh ADO" (RESOLVED)

    I use VB6 on WinXP - Using Access97 mdb.

    SEE SCREENSHOT BELOW!

    Here is what I have run into. I have learned ADO (code only - no controls) fairly quickly, but I have run into a snag...

    1) I open a db connection-recordset on Form_Load
    2) It fills in a datagrid
    3) All my Test buttons (plain 3D grey ones) all work fine.

    GREAT UP TO NOW!

    THEN I ADDED A FILTER! (not so good)

    On the right "LEDGER TOTALS" I filter totals from the db table (not the datagrid) to get my amounts... works fine!

    BUT...

    When I try to Add a record I get the ADO error.

    Here is the code for the filter...
    VB Code:
    1. Dim Inc As Integer, IncBal As Currency
    2. IncBal = "0.00"
    3. DoEvents
    4. Set adoPrimaryRS = New Recordset
    5. adoPrimaryRS.Open "SELECT * FROM Register", db
    6. adoPrimaryRS.Filter = "Type = 'Income'"
    7. For Inc = 1 To adoPrimaryRS.RecordCount
    8.     adoPrimaryRS.AbsolutePosition = Inc
    9.     IncBal = IncBal + adoPrimaryRS.Fields("Credit").Value
    10. Next Inc
    11. lbIncome.Caption = Format(IncBal, "0.00")
    12. adoPrimaryRS.Filter = adFilterNone

    I have 3 of these block in my procedure. The reason I know this is the problem is because I disabled the procedure and I could Add a record fine.

    Then to prove my point I enabled the procedure again. But before clicking the Add button, I clicked my Refresh button... WORKS FINE!

    This is what I need. I need a way to refresh the main data connection from with in the above procedure.

    I can not use the "Refresh" button because it will undo any sorts, filters, finds and column widths that were edited.

    So basically I have to give my data connection back to my app on the main form.

    Any Ideas?

    Thanks

    The Screenshot below has been made smaller to save size. Grey buttons on top are for testing purposes.
    Last edited by epixelman; Dec 20th, 2003 at 03:38 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    oh... and by the way.

    The above procedure is called everytime I change or update a record.

    I need to refresh the dataconnection withour refreshing the datagrid.

  3. #3

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Oops..... sorry

  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    epixelman


    I am not getting it, what wrong with

    "SELECT Sum(Credit) FROM Register"

    and getting the total from the SumOfCredit field to get the total? What you are doing seems like a lot of code to get the same results.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Not sure what your asking?
    If you think I can do it better then could you post the entire code block of what you mean?

    I'm not having a problem with getting my totals, but with adding records after this procedure has been run.

    This procedure (code above) is meant only to read the data from the table and display it in the Ledger labels.

    Do I really need to RE-OPEN the db just to read?

    The REGISTER table is already opened. It is opened up when the Form_Load even happens and it is never closed until the app is completly closed ("End"). BTW my db is a Data SHAPE recordset.

    If you look at the screenshot (above), you will notice that the entire REGISTER table is opened and loaded into a datagrid. But the Ledger Totals are updated everytime I make a change to a record.

    Am I going about this all wrong?
    Last edited by epixelman; Dec 20th, 2003 at 08:13 AM.

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Sorry, I am not totally clear as to what you are doing.

    You say you open a recordset for the grid in Form_Load. I am assuming you are setting this to your adoPrimaryRs variable.

    The following line executes when you need to do the Filter. It uses the adoPrimaryRS variable as well

    adoPrimaryRS.Open "SELECT * FROM Register", db

    However, it opens a ReadOnly cursor (the default) and overwrites the recordset opened in Form_Load (again just an assumption).

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Yes...

    When I first start the app, it opens the database and loads the entire REGISTER table into the datagrid.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. 'set the database
    4. Set db = New Connection
    5. db.CursorLocation = adUseClient
    6. db.Open "PROVIDER=MSDataShape;Data PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    7.         App.Path & "\moneymatters.mdb;"
    8.  
    9. 'set the recordset
    10. Set adoPrimaryRS = New Recordset
    11. adoPrimaryRS.Open "SHAPE {select RecNbr,Date,Month,Type,Acct,Cleared,TransID,Category,Description,Debit,Credit,Gross,SplitAmt " & _
    12.     "from Register Order by RecNbr} AS ParentCMD APPEND " & _
    13.     "({select RecNbr,Date,Month,Type,Acct,Cleared,TransID,Category,Description,Debit,Credit,Gross,SplitAmt " & _
    14.     "FROM Register ORDER BY RecNbr} AS ChildCMD RELATE RecNbr TO RecNbr) " & _
    15.     "AS ChildCMD", db, adOpenDynamic, adLockOptimistic
    16.  
    17. 'Bind controls to recordset
    18. 'set the datasource of these controls to the database
    19. Set TxtRecNbr.DataSource = adoPrimaryRS
    20. Set TxtRegDate.DataSource = adoPrimaryRS
    21. Set cbRegMonth.DataSource = adoPrimaryRS
    22.  
    23. 'Bind recordset to datagrid
    24. Set grdDataGrid.DataSource = adoPrimaryRS.DataSource
    25.  
    26. 'now set the column widths
    27. SetRegGridWidth
    28.  
    29. End Sub

    The databse is never closed until I exit the app.

    I can Add, del, update etc... WORSK FINE.

    But I added the procedure (below) to just read the "already" opened db into the lables on the LEDGER TOTALS section of app. (see scrnshot)

    VB Code:
    1. Public Sub CalcLedger()
    2.  
    3. 'lbIncome
    4. Dim Inc As Integer, IncBal As Currency
    5. IncBal = "0.00"
    6. DoEvents
    7. Set adoPrimaryRS = New Recordset
    8. adoPrimaryRS.Open "SELECT * FROM Register", db
    9. adoPrimaryRS.Filter = "Type = 'Income'"
    10. For Inc = 1 To adoPrimaryRS.RecordCount
    11.     adoPrimaryRS.AbsolutePosition = Inc
    12.     IncBal = IncBal + adoPrimaryRS.Fields("Credit").Value
    13. Next Inc
    14. lbIncome.Caption = Format(IncBal, "0.00")
    15. adoPrimaryRS.Filter = adFilterNone
    16.  
    17. 'NOTE there is more to this procedure but this is one block. All the other blocks rae the same.
    18.  
    19. End Sub

    I get the error (ADO error) because I think that when it filters (above code) the "already open" db gets messed up with it some how.

    The above code IS NOT the databse connection (primaryADOrs) it is just a procedure I use to just read (filter) some info from the database to fill in the LEDGER info.

    Why am I getting this error? This error will not let me add a new record unless I refresh the db connection.

    The ERROR happens when I try to add a new record. The reading filter for the ledger works works fine. But the Error did not occure until I add this procedure. So somehow this procedure is messing up the db connetion or recordset.

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The CalcLedger procedure is using the same recordset variable as the Form_Load (adoPrimaryRS). It creates a new recordset (readonly) with a different query. You don't see anything wrong with that?

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    epixelman


    The whole block of code...
    VB Code:
    1. Set dbtable = New Recordset
    2. dbtable.Open "SELECT Sum(Credit) FROM Register", db
    3. lbIncome.Caption = Format(dbtable!SumOfCredit, "0.00")
    4. set dbtable= Nothng
    To add a record use the SQL Insert statement

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Originally posted by randem
    epixelman


    The whole block of code...
    VB Code:
    1. Set dbtable = New Recordset
    2. dbtable.Open "SELECT Sum(Credit) FROM Register", db
    3. lbIncome.Caption = Format(dbtable!SumOfCredit, "0.00")
    4. set dbtable= Nothng
    To add a record use the SQL Insert statement
    Where's the "Income" Filter?

    This is just totaling all Credit.
    I have to select the the Credit if the TYPE field is "Income".

    I have Refunds that use the Credit field. The TYPE field holds "Income, Expense, Refund and OpenBal"

    So my filter should be...
    If TYPE = Income Then Get the CREDIT value

    Will your idea work with the added filter?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Originally posted by brucevde
    The CalcLedger procedure is using the same recordset variable as the Form_Load (adoPrimaryRS). It creates a new recordset (readonly) with a different query. You don't see anything wrong with that?
    Ohhh....

    I didn't realize you have to have a different variable for the Recordset.

    Is that gonna effect the SHAPE function of ADO?

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Originally posted by brucevde
    The CalcLedger procedure is using the same recordset variable as the Form_Load (adoPrimaryRS). It creates a new recordset (readonly) with a different query. You don't see anything wrong with that?
    THAT IS IT! IT WORKS GREAT!!!

    Thank You - Thank You!!

    I just created an another variable for adoPrimaryRS.open and called it LedgerData.open

    So does that mean I can have many recordsets open at once?

  14. #14
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    So does that mean I can have many recordsets open at once?
    Absolutely. However, don't go crazy. Only return the records you need. For instance, your Filter recordset is not neccessary, which is what randem was trying to show you.

    Let the database do the work where possible.

    VB Code:
    1. strSQL = "SELECT Sum(Credit) As SumOfCredit FROM Register Where Type = 'Income'"
    2.  
    3. Set dbtable = New Recordset
    4.  
    5. dbtable.Open strSQL,db
    6.  
    7. lbIncome.Caption = Format(dbTable!SumOfCredit, "0.00")
    8.  
    9. dbTable.Close
    10. set dbTable= Nothng

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Posts
    1,005
    Thanks!

    Everyone has been a great help!

    All is working fine!

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    epixelman

    With a little imagination....

    "SELECT Sum(Credit) FROM Register where Type='Income'",

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