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 » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Autonumber (Using Dmax) on Continuous Forms



 
 
Thread Tools Display Modes
  #1  
Old August 28th, 2009, 09:11 PM
Jeff Monroe Jeff Monroe is offline
Member
 
First recorded activity by OfficeFrustration: May 2006
Location: San Diego, CA
Posts: 27
Default Autonumber (Using Dmax) on Continuous Forms

I have created a continuous that I want to increment a number (by 1) each time a record is saved. I have found when using the code below, the field increments to the next number in the new record once the current record is saved. However, using a continuous form, it does not. When the form is first loaded it is incremented correctly, but once the current record is updated and saved, the new record lists the number that was lised on load

Can this work with continuous forms?

Thanks.


Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo Err_Form_Error

Response = IncrementField(DataErr)

Exit_Form_Error:
Exit Sub

Err_Form_Error:
MsgBox Err.Description
Resume Exit_Form_Error

End Sub

Function IncrementField(DataErr)
If DataErr = 3022 Then
Me!FA_no = DMax("FA_no", "tblFA_Log") + 1
IncrementField = acDataErrContinue
End If
End Function
__________________
jm
  #2  
Old August 29th, 2009, 06:48 AM posted to microsoft.public.access.forms
Allen Browne
external usenet poster
 
Posts: 11,706
Default Autonumber (Using Dmax) on Continuous Forms

Jeff, instead of waiting for an error, use the BeforeUpdate event of the
form to assign the next available:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.FA_no) Then
Me.FA_no = DMax(...
End If
End Sub

It strikes me as better to assign the number than to wait for an error to
occur.

Form_BeforeUpdate is the last possible moment before the record is saved, so
using this event reduces the chance that 2 people entering records at the
same time will be assigned the same number (as they could be if you used
Form_BeforeInsert.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Jeff Monroe" wrote in message
...

I have created a continuous that I want to increment a number (by 1)
each time a record is saved. I have found when using the code below,
the field increments to the next number in the new record once the
current record is saved. However, using a continuous form, it does not.
When the form is first loaded it is incremented correctly, but once the
current record is updated and saved, the new record lists the number
that was lised on load

Can this work with continuous forms?

Thanks.


Private Sub Form_Error(DataErr As Integer, Response As Integer)
On Error GoTo Err_Form_Error

Response = IncrementField(DataErr)

Exit_Form_Error:
Exit Sub

Err_Form_Error:
MsgBox Err.Description
Resume Exit_Form_Error

End Sub

Function IncrementField(DataErr)
If DataErr = 3022 Then
Me!FA_no = DMax("FA_no", "tblFA_Log") + 1
IncrementField = acDataErrContinue
End If
End Function




--
Jeff Monroe


 




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 12:56 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.