Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Location
    MidWest
    Posts
    6

    Unanswered: rs.move??? glitch

    I recently posted a message about Type Mismatch Error. I got some code to work on a test DB.
    Now when I recreate that code in any other DB I get a freaky glitch. The rs.MovePrevious or rs.Movenext seem to run in a continuos loop.
    Example:
    I am in the second record and I enter information in either of my Debit or Deposit fields and when I get to the Balance field, which is where my code is, the move previous and next flash bewteen thefirst and second record. Here is my code.

    Private Sub Balance_GotFocus()
    On Error GoTo Error_Balance_GotFocus

    Dim rs As DAO.Recordset
    Dim x As Currency
    Set rs = Me.Recordset

    rs.MovePrevious
    If Not rs.BOF Then
    x = x + rs!Balance.Value
    Else
    x = 0
    End If

    rs.MoveNext

    Me!Calc = x
    If IsNull(Deposit) Then
    Balance = Calc - Debit
    Else
    Balance = Calc + Deposit
    End If

    Exit_Balance_GotFocus:
    Exit Sub

    Error_Balance_GotFocus:
    MsgBox Err.Description
    Resume Exit_Balance_GotFocus

    End Sub

    If anyone see's the error, let me know, or if there is some setting I need to change. It freaks me out that when ever I use the rs.Move aoptions now, the system freaks out.

    TIA

    Elevtro

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    rs.MovePrevious
    If Not rs.BOF Then
    x = x + rs!Balance.Value
    Else
    x = 0
    End If
    rs.MoveNext
    ...would be an endless loop. recordset-position-wise it is functionally the same as:
    rs.MovePrevious
    'no moves
    rs.MoveNext 'taking you back where you started

    ...except there is no loop, so it executes once only

    note also that when you define your recordset, it's default starting position is the first record (if record(s) exist, else .BOF and .TOF simultaneously). as a consequence:

    after :
    set rs = blah, you are on the first record (if record(s) exist) .
    and after:
    rs.moveprevious, you are .BOF ALWAYS if there are records, else error.

    therefore:
    -with records in rs, you always hit .BOF after your first .moveprevious so your IF NOT .BOF never executes, you are always in ELSE
    -with no records, you are .BOF before you start, and error after the .moveprevious

    i can only guess what you are trying to do. maybe step thru the recordset doing some sort of running total. although you "shouldn't" store calculated values in a table, your application will maybe run faster if you do it this way rather than calculating the running total "dynamically" when you need it.

    so... having warned you that what you are doing is "bad", here is how you do it:


    Code:
    dim priorValue as double
    set rs = ....starting with rs set to something useful
    with rs
       .movefirst 'you are already there, but let's be pedantic
       priorValue = !Balance 'get the initial value
       .movenext 'you have nothing else to do on the first record 
       do while not .eof 'now the loop thru each record except the first
          priorValue = priorValue + !Balance
          .movenext
       loop
    end with
    note:
    -no need to check for .BOF with this route
    -use of WITH to save typing and keep things neater
    -use of a variable instead of hopping backwards & forwards thru the recordset

    sorry that i don't understand what you are trying to achieve so i can't add the (possibly corrected) other stuff you have in your example.

    last, gotfocus can be a dangerous event unless you take the focus away at the start of your _gotfocus event.

    izy

  3. #3
    Join Date
    Sep 2003
    Location
    MidWest
    Posts
    6

    Thumbs up

    The first thing I don't under stand is, how is it looping? I don't know if I have been staring at this code too long or what, but I don't see a loop.

    Second, Thank you.
    I am trying to create a dynamicly updated ledger. I would settle for a non dynamic version which is what I have a semi working copy of at home. My goal is to figure out the real code that will not eat processor time while keeping all of my balances current. From my initial deposit to my last record. If for some reason I had to change a previous record, I would love for it to update all records with their new totals. That way I would not have to go through every record to update each one individually, but I know that could take a lot of time to process.

    I will try you code and see if it does what I am intending it to do. I am new at this and have never needed to use a database in the past, but a couple months ago I got a job that the perfect solution was creating some databases and I love it. I love what can be done with them so I am trying to learn everything I can about them. I am poor so I can't buy the couple books I want, but this website has been a HUGE help.

    I will let you know if the code works for me.

    Thanks a bunch,
    Elevtro

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    i don't see a loop in your code either!

    is "something" repeatedly triggering the _gotfocus (either by directly calling the routine or by repeatedly setting the focus to [Balance])

    put
    msgbox "Here we go again!"
    as the first line of your _gotfocus event to convince yourself that any loop is caused outside of the _gotfocus event

    izy

  5. #5
    Join Date
    Sep 2003
    Location
    MidWest
    Posts
    6
    I removed my code and started over and got it to work...Sort of. It works when I go from the first record manually through each record, but if I close it and add a new record it doesn't pull the balance from the correct record.
    I also added a button with your code, but I don't think it is working correctly.
    I have attached the dbase so you can take a look at it and see what you think is wrong and so you will know what I am trying to accomplish.
    Sorry it to so long to reply, I have been busy.

    Elevtro
    Attached Files Attached Files

Posting Permissions

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