Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment Counter + Display Counter on Form

Status
Not open for further replies.

swk003

IS-IT--Management
Feb 10, 2004
86
GB
Hi, i am attempting to add a counter to an input form and display the current counter value as the user continues to update records. I have some basic code which seems to work BUT I would like to store the counter value in a table field so that if the form is closed and reopened the value reverts back to the same counter value and not 1!!here's the code;

Me!txtCounter = Me!txtCounter + 1

Does anyone have any useful code for a newbie?

thanks

Simon
 
You need a 'setup' table


Create a table tblSetup
Add field SetUpId
field type Yes/No
default value Yes
set to Primary Key
( This ensures the table can never have more than one record in it )

Next field
CounterValue of type LongInteger



Then on the form
In the Form Open event procedure

Dim rst as Recordset (code here depends on which version of Access you are running )

txtCounter = rst!CounterValue





And in the Form.close event proc you do the reverse
Dim rst As Recordset
etc..

rst!CounterValue = txtCounter
rst.Update


But, BUT, BUT

If you increment the counter for each record - then why not just display the number of records in the table ?


Or add an autonumber field to the data table that you're adding records to and then display the value of the autonumber ?



'ope-that-'elps.




G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
And what happens in MultiUser mode ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi G LS

Have tried the first option you kindly suggetsed and have set up form_open and close events. When i attempt to open the form i get a runtime error - obj variable or with block variable not set. Here's the code:

'Form Close event procedure
Private Sub Form_Close()
'And in the Form.close event proc you do the reverse
Dim rst As Recordset
'etc..

rst!CounterValue = txtCounter
rst.Update

End Sub

'Form Open event procedure
Private Sub Form_Open(Cancel As Integer)
Dim rst As Recordset '(code here depends on which version of Access you are running )

txtCounter = rst!CounterValue

End Sub

any ideas?

thanks

Simon
 
the error "obj variable or with block variable not set"

indicates an error in the set up of the rst.

What code did you actually put in place of

Dim rst As Recordset
'etc..



and in place of

Dim rst As Recordset '(code here depends on which version of Access you are running )


????




G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top