Function to Count the number of times a button is clicked

Discussion in 'Microsoft Access VBA Modules' started by Steve, Jul 10, 2008.

  1. Steve

    Steve Guest

    I put this code into a module to count the number of times a button is
    clicked. Access calls this function when the button is clicked. I keep
    getting error code 3021 when it runs and it says "Either the BOF or EOF is
    true, or the current record has been deleted. Requested operation requires a
    current record. The VB editor highlights the part "MyCount = MySet!Count"
    when it opens. I copied this code verbatim from the web (except changing the
    table/field names). I want this to increase the "count" by one each time the
    button is clicked. What am I doing wrong? The table and field names are
    correct... I've checked that several times.

    Function CheckCount()
    Dim MySet As ADODB.Recordset
    Dim MyCount As Integer
    Set MySet = New ADODB.Recordset
    MySet.Open "tblCategoryCount", CurrentProject.Connection, , adLockOptimistic
    MyCount = MySet!Count
    MySet!Count = MyCount + 1
    MySet.Update
    End Function
     
    Steve, Jul 10, 2008
    #1
    1. Advertisements

  2. Steve

    Steve Guest

    That worked. Thanks.

    "Chris O'C via AccessMonster.com" wrote:

    > Don't use a recordset for this. Use sql.
    >
    > Public Function CheckCount()
    > Dim conn As ADODB.Connection
    >
    > Set conn = CurrentProject.Connection
    > conn.Execute "UPDATE tblCategoryCount " _
    > & "SET [Count] = [Count] + 1;"
    > conn.Close
    > End Function
    >
    > You shouldn't use reserved words as field names. Count is a reserved word.
    >
    > Chris
    > Microsoft MVP
    >
    >
    > Steve wrote:
    > >I put this code into a module to count the number of times a button is
    > >clicked. Access calls this function when the button is clicked. I keep
    > >getting error code 3021 when it runs and it says "Either the BOF or EOF is
    > >true, or the current record has been deleted. Requested operation requires a
    > >current record. The VB editor highlights the part "MyCount = MySet!Count"
    > >when it opens. I copied this code verbatim from the web (except changing the
    > >table/field names). I want this to increase the "count" by one each time the
    > >button is clicked. What am I doing wrong? The table and field names are
    > >correct... I've checked that several times.
    > >
    > >Function CheckCount()
    > >Dim MySet As ADODB.Recordset
    > >Dim MyCount As Integer
    > >Set MySet = New ADODB.Recordset
    > >MySet.Open "tblCategoryCount", CurrentProject.Connection, , adLockOptimistic
    > >MyCount = MySet!Count
    > >MySet!Count = MyCount + 1
    > >MySet.Update
    > >End Function

    >
    > --
    > Message posted via http://www.accessmonster.com
    >
    >
     
    Steve, Jul 10, 2008
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allen Browne

    Re: Which button was clicked in detail area of endless form

    Allen Browne, Jul 4, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    0
    Views:
    535
    Allen Browne
    Jul 4, 2003
  2. Adam

    Counter to count number of times a report's been printed

    Adam, Jan 24, 2004, in forum: Microsoft Access VBA Modules
    Replies:
    4
    Views:
    341
  3. Guest

    Function In Query Called Multiple Times

    Guest, Aug 15, 2006, in forum: Microsoft Access VBA Modules
    Replies:
    3
    Views:
    322
    Michel Walsh
    Aug 16, 2006
  4. Guest

    Audio when command button is clicked

    Guest, Sep 22, 2006, in forum: Microsoft Access VBA Modules
    Replies:
    4
    Views:
    337
    Guest
    Sep 26, 2006
  5. Guest

    Limit Number of Times Program Opens

    Guest, Nov 28, 2006, in forum: Microsoft Access VBA Modules
    Replies:
    1
    Views:
    185
    Fred Boer
    Nov 28, 2006
Loading...

Share This Page