Previous Thread

8/10/2006 11:29:18 PM    Auto number generator
Greetings, 
 
I am having problem with the following codes for some reason and I 
 
cannot figure it out where the problem is. However, what I'm trying to 
 
achive is to have a auto number generator for my quote form, so 
 
basically this generator would create a quote number that will change 
 
as new records are added. I am doing this in MS Access 2000. 
 
here is the code: 
 
----------------------------------------------- 
 
Private Sub Form_BeforeInsert(Cancel As Integer) 
 
Dim rst As DAO.Recordset 
 
Dim i As Integer 
 
Dim nMaxVal As Integer 
 
Dim n As Integer 
 
nMaxVal = 1630 
 
Set rst = Me.RecordsetClone 
 
If Not rst.BOF Then 
 
rst.MoveFirst 
 
End If 
 
Do While Not rst.EOF 
 
n = rst![Quote_Num] 
 
If n > nMaxVal Then 
 
nMaxVal = n 
 
End If 
 
rst.MoveNext 
 
Loop 
 
Me![Quote_Num] = nMaxVal + 1 
 
End Sub 
 
----------------------------------------------------- 
 
My guess is that problem lays in the next one 
 
------------------------------------------------------ 
 
Private Sub txtQuoteNo_Change() 
 
Dim rst As DAO.Recordset, intI As Integer 
 
Set rst = Me.RecordsetClone 
 
If IsNull(rst![Quote_Num]) Then 
 
intI = rst![Quote_Num] 
 
Me![Quote_Num] = intI + 2 
 
End If 
 
End Sub 
 
------------------------------------------------------ 
 
Could you please help me figure this out. 
 
Thanks in advance 
 
-Rino



8/15/2006 2:58:57 PM    Re: Auto number generator
You can't manipulate Autonumbers! It assigns a new number (either sequential 
 
or random depending on what you set). Furthermore, it uses an Autonumber on 
 
"New Record" whether you save the record or not. 
 
Use a numeric field (or a text field manipulated via numerics) if you want to 
 
add 1 to YOUR OWN field. 
 
Your code is poor too. You don't have to step through a recordset to get the 
 
existing max value. Lookup MAX in Query help!(you can do the same thing with a 
 
single query) 
 
So far as I know, getting/setting the next record number should be done in 
 
Before Update event. Because, a new number is not necessary unless the record 
 
is actually saved! (and may limit some multi-user conflicts for the same 
 
reason) 
 
(It is sometimes said that Autonumbers are only for internal database use. 
 
Whatever, certainly you can't guarantee what they are! And most certainly you 
 
can't set them) 
 
Chris 
 
PS It is considered good ACCESS practice, due to BUGS since Access97, to close 
 
a recordset, and possibly to Set rst = Nothing also. Otherwise you can get 
 
leftover "funnies", either memory leaks or Access not quitting (I've forgotten 
 
exactly). It sucks if MS never fixed these bugs, but there you nearly caught 
 
me ranting :-) 
 
"Rino" <davorins@gmail.com> wrote in message 
 
news:1155277758.361582.191470@p79g2000cwp.googlegroups.com...