[QUOTE]Function GetMaxId() todayDateStr = Year(date()) & Right("00"& Month(date()),2) & Right("00"& Day(date()),2) strQuery="select top 1 id from test where id like '"& todayDateStr &"%' Order by Id Desc" set rs=server.createobject("adodb.recordset") rs.open strQuery,conn,1,1 If Not Rs.Eof Then NewId = FormatNumber(Rs("Id"),0) + 1 Else NewId = todayDateStr &"0001" End If rs.close() Set rs = Nothing GetMaxId = NewId End Function[/QUOTE]
Function GetMaxId() Dim todayDateStr,NewId 'todayDateStr 取得当天日期形成的8位字符串 todayDateStr = Year(date()) & Right("00"& Month(date()),2) & Right("00"& Day(date()),2) '利用模糊查询查找出ID左边8位等于todayDateStr(即今天添加的记录)的,并按倒序排列的第一条记录 '即是今天添加的ID最大的记录 strQuery="select top 1 id from test where id like '"& todayDateStr &"%' Order by Id Desc" set rs=server.createobject("adodb.recordset") rs.open strQuery,conn,1,1 If Not Rs.Eof Then '如果当天已经添加记录,则在最大的记录编号上加1 'FormatNumber(Num,n)用于格式化数字,n指保留的小数位数,这里表示不保留小数位 NewId = FormatNumber(Rs("Id"),0) + 1 Else '如果当天没有添加记录,则直接把后四位赋值为“0001” NewId = todayDateStr &"0001" End If rs.close() Set rs = Nothing '返回生成的ID编号 GetMaxId = NewId End Function