A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

count number in field



 
 
Thread Tools Display Modes
  #1  
Old February 1st, 2009, 07:28 AM posted to microsoft.public.access.gettingstarted
steve goodrich
external usenet poster
 
Posts: 132
Default count number in field

I use an unbound text box on my form to count the number of records there
are, then add 1 to the total with. =Count(*)+1

The user then enters this number in a field called Log No which is bound to
the table.



Is there a way to add this number automatically to my field (Log No) when
the form loads. I tried the auto number but it keeps going out of sink and
missing numbers out



Something on the lines of



Log no = number of last record+1



So if the last log number used was 200, when a new record was opened the log
no field would auto fill with 201



Using Access 97



Thanks


  #2  
Old February 1st, 2009, 01:04 PM posted to microsoft.public.access.gettingstarted
Philip Herlihy
external usenet poster
 
Posts: 292
Default count number in field

Steve Goodrich wrote:
I use an unbound text box on my form to count the number of records there
are, then add 1 to the total with. =Count(*)+1

The user then enters this number in a field called Log No which is bound to
the table.



Is there a way to add this number automatically to my field (Log No) when
the form loads. I tried the auto number but it keeps going out of sink and
missing numbers out



Something on the lines of



Log no = number of last record+1



So if the last log number used was 200, when a new record was opened the log
no field would auto fill with 201



Using Access 97



Thanks




Here's a fragment of code which I use to determine what "Invoice Number"
has just been generated as an Autonumber:

Note, to avoid clutter, I haven't included all the code. This is part
of a function that builds an INSERT statement which creates a new
Invoice record. That table has an Autonumber as a primary key, and I
need to use elsewhere the number just created. So, after the stSQL is
executed, I use the line:
Set rs = CurrentDb.OpenRecordset("SELECT @@IDENTITY")
... to access that Autonumber generated a split second before. Note that
not everyone thinks it's a good idea to use the _value_ of an Autonumber
this way (I print them on the Invoices as an identifier) but it works
for me. Here's the code:
================================================== =================

stSQL = "INSERT INTO tbl_Invoice (... blah blah...)"
Set db = CurrentDb()
'Insert new Invoice record:
CurrentDb.Execute stSQL
'return last Autonumber generated:
Set rs = CurrentDb.OpenRecordset("SELECT @@IDENTITY")
If rs.EOF Then
MsgBox "No Autonumber Generated:" & vbCrLf & "Cannot
continue", vbCritical, "in cmd_report_Click()"
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Sub
Else
'store ID of inserted Invoice record
lngInvoiceID = rs.Fields(0)
rs.Close
Set rs = Nothing
Set db = Nothing
End If
================================================== =================

Hope that's useful.

Phil, London
  #3  
Old February 1st, 2009, 01:40 PM posted to microsoft.public.access.gettingstarted
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default count number in field

To increment the last log no value by 1 when a new record is inserted you can
either put the following in the form's Current event procedu

Me.[Log no].DefaultValue = _
"""" & Nz(DMax("[Log no]", "[YourTable]"), 0) + 1 & """"

or the following in the form's BeforeInsert event procedu

Me.[Log no] = _
Nz(DMax("[Log no]", "[YourTable]"), 0) + 1

The former has the advantage of showing the new number as soon as a user
navigates to the blank new record, whereas the latter inserts the value only
when the user starts to add other data into a new record. You might be
wondering why in the first example the value is wrapped in literal quotes
characters even though a number. This is because the DefaultValue property
is always a string expression, regardless of the data type of the field to
which the control is bound. In the above case it wouldn't actually matter if
the quotes were omitted, but they can be essential in some situations, dates
being a case in point, so its best to include them in any case.

While the above will work fine in a single user environment, in a multi-user
environment on a network it could give rise to a conflict if two or more
users are entering a new record simultaneously, in which case the first one
would be able to save their record , but the other(s) would experience an
error due to the violation of the unique key on the log no field. There are
various ways of avoiding this. Roger Carlson has a simple solution at:


http://www.rogersaccesslibrary.com/f...19 89c53d7ffb


while mine at the following link is more complex, but includes provision for
re-setting the value at which the sequence will start when the next record is
inserted:


http://community.netscape.com/n/pfx/...g=ws-msdevapps



Ken Sheridan
Stafford, England

"Steve Goodrich" wrote:

I use an unbound text box on my form to count the number of records there
are, then add 1 to the total with. =Count(*)+1

The user then enters this number in a field called Log No which is bound to
the table.



Is there a way to add this number automatically to my field (Log No) when
the form loads. I tried the auto number but it keeps going out of sink and
missing numbers out



Something on the lines of



Log no = number of last record+1



So if the last log number used was 200, when a new record was opened the log
no field would auto fill with 201



Using Access 97



Thanks




  #4  
Old February 1st, 2009, 04:29 PM posted to microsoft.public.access.gettingstarted
Al Campagna[_2_]
external usenet poster
 
Posts: 1,462
Default count number in field

Steve,
Try this as a Default Value for your field LogNo...
=NZ(DCount("*","tblYourTable"),0) +1
--
hth
Al Campagna
Microsoft Access MVP
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."

"Steve Goodrich" wrote in message
. ..
I use an unbound text box on my form to count the number of records there
are, then add 1 to the total with. =Count(*)+1

The user then enters this number in a field called Log No which is bound
to the table.



Is there a way to add this number automatically to my field (Log No) when
the form loads. I tried the auto number but it keeps going out of sink
and missing numbers out



Something on the lines of



Log no = number of last record+1



So if the last log number used was 200, when a new record was opened the
log no field would auto fill with 201



Using Access 97



Thanks




  #5  
Old February 1st, 2009, 05:47 PM posted to microsoft.public.access.gettingstarted
Linq Adams via AccessMonster.com
external usenet poster
 
Posts: 1,474
Default count number in field

Note that setting the Default Value of the field like this carries the same
risk in a multi-user environment has been noted by Ken.

While placing the code in the Form_Before event carries a small risk is of
this in the multi-user environment, by assigning it at the last minute cuts
the odds down considerably. I've used it for years without ever having had
two users save a record at the precise same instant.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via http://www.accessmonster.com

  #6  
Old February 2nd, 2009, 06:37 AM posted to microsoft.public.access.gettingstarted
steve goodrich
external usenet poster
 
Posts: 132
Default count number in field

Thanks guys, works great
"Steve Goodrich" wrote in message
. ..
I use an unbound text box on my form to count the number of records there
are, then add 1 to the total with. =Count(*)+1

The user then enters this number in a field called Log No which is bound
to the table.



Is there a way to add this number automatically to my field (Log No) when
the form loads. I tried the auto number but it keeps going out of sink
and missing numbers out



Something on the lines of



Log no = number of last record+1



So if the last log number used was 200, when a new record was opened the
log no field would auto fill with 201



Using Access 97



Thanks




  #7  
Old February 2nd, 2009, 03:07 PM posted to microsoft.public.access.gettingstarted
Al Campagna[_2_]
external usenet poster
 
Posts: 1,462
Default count number in field

Linq,
Thanks for that info.
I've used my method many times in the past with no problems, but... that
doesn't mean it can't be improved.
(My environments, to date, have not involved multiple users on the same
form)

I'll take a look at Roger's and Ken's solutions, as soon as I have a
chance.

Thanks,
Al Campagna
Microsoft Access MVP
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."

"Linq Adams via AccessMonster.com" u28780@uwe wrote in message
news:9110f62845e28@uwe...
Note that setting the Default Value of the field like this carries the
same
risk in a multi-user environment has been noted by Ken.

While placing the code in the Form_Before event carries a small risk is of
this in the multi-user environment, by assigning it at the last minute
cuts
the odds down considerably. I've used it for years without ever having had
two users save a record at the precise same instant.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via http://www.accessmonster.com



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:45 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.