Acc:2K OnOpen() Q

  • Thread starter Thread starter NoodNutt
  • Start date Start date
N

NoodNutt

G'day Ppl

A long time ago, someone was kind enough to provide me with a code to update
a counter control when the form opened.

e.g each time the form opened the counter control would increase by 1.

Something like
Me.Counter = Me.Counter + 1

not sure where it goes, tried BeforeUpdate() and OnOpen(), neither worked.

TIA
Mark.
 
G'day Ppl

A long time ago, someone was kind enough to provide me with a code to update
a counter control when the form opened.

e.g each time the form opened the counter control would increase by 1.

Something like
Me.Counter = Me.Counter + 1

not sure where it goes, tried BeforeUpdate() and OnOpen(), neither worked.

Is Counter a field in your form's recordsource? If not, then the value of Counter would (probably) be 0 everytime the
form opens.

I'd suspect you'd be better off using either (a) a gobal variable or (b) a stored value to do this. To use a global
variable, add this to a Standard Module:

[General Declarations section]

Public gintFormCount as Integer

Now when your form Opens:

Sub Form_Open(Cancel As Integer)
mingFormCount = mintFormCount +1
End Sub

This is the easiest way to do it, but in general I like to avoid global variables, since they can lose their value if,
for example, an unhandled error occurs.

To do a table-based value, add a table (tSettings) with two fields (tSettingName and tSettingValue, both Text fields).
Add a record to this table:

tSettingName="FormCount"
tSettingValue = "0"

Now add a function to a Standard Module:

Function IncrementFormCount() As Boolean
Dim lngCount As Long
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("SELECT * FROM tSettings WHERE sSettingName='FormCount'")

If Not (rst.EOF And rst.BOF) Then

rst.Edit
rst("sSettingvalue") = CStr(Val(rst("sSettingValue")) + 1)
rst.Update

End If

Set rst = Nothing

End Function

Now to increment the counter, call the function when the form opens:

Sub Form_Open(Cancel As Integer)
IncrementFormCount
End Sub



Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G'day Scott

thx for your reply.

I should clarify things a little as I feel I was a little vague. The
"Counter" field is based on a Forms recordset with a single field only,
which in-turn has a subform attached.. the subform may have several records
which the user can select, I draw the value from the "Counter" field and
insert it into the record the user has selected. Once selected and the value
is inserted, the user then generates a report based on his/her selection(s).
They can record the "counter" value and retrieve the report at any point,
instant or future. I toyed with the idea of making the "Counter" Unique, but
that would amass un-necessary clutter. I would prefer to keep it simple.

TIA
Mark.

Scott McDaniel said:
G'day Ppl

A long time ago, someone was kind enough to provide me with a code to
update
a counter control when the form opened.

e.g each time the form opened the counter control would increase by 1.

Something like
Me.Counter = Me.Counter + 1

not sure where it goes, tried BeforeUpdate() and OnOpen(), neither worked.

Is Counter a field in your form's recordsource? If not, then the value of
Counter would (probably) be 0 everytime the
form opens.

I'd suspect you'd be better off using either (a) a gobal variable or (b) a
stored value to do this. To use a global
variable, add this to a Standard Module:

[General Declarations section]

Public gintFormCount as Integer

Now when your form Opens:

Sub Form_Open(Cancel As Integer)
mingFormCount = mintFormCount +1
End Sub

This is the easiest way to do it, but in general I like to avoid global
variables, since they can lose their value if,
for example, an unhandled error occurs.

To do a table-based value, add a table (tSettings) with two fields
(tSettingName and tSettingValue, both Text fields).
Add a record to this table:

tSettingName="FormCount"
tSettingValue = "0"

Now add a function to a Standard Module:

Function IncrementFormCount() As Boolean
Dim lngCount As Long
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("SELECT * FROM tSettings WHERE
sSettingName='FormCount'")

If Not (rst.EOF And rst.BOF) Then

rst.Edit
rst("sSettingvalue") = CStr(Val(rst("sSettingValue")) + 1)
rst.Update

End If

Set rst = Nothing

End Function

Now to increment the counter, call the function when the form opens:

Sub Form_Open(Cancel As Integer)
IncrementFormCount
End Sub



Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
Back
Top