User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 427,983 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,432 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Sep 20th, 2004
Views: 12,127
Access 200,XP and 2003
visualbasic Syntax | 5 stars
  1. Public Function GetNextAutoNumber(strTableName As String) As Long
  2.  
  3. 'Declare some object variables
  4.  
  5. Dim wsCurrent As DAO.Workspace
  6. Dim DBCurrent As DAO.Database
  7. Dim qdfAutoNum As DAO.QueryDef
  8. Dim rstAutoNumbers As DAO.Recordset
  9.  
  10. 'set up references for those objects
  11.  
  12. Set wsCurrent = DBEngine.Workspaces(0)
  13. Set DBCurrent = wsCurrent.Databases(0)
  14. Set qdfAutoNum = DBCurrent.QueryDefs("qrybasAutoNumber")
  15. qdfAutoNum.Parameters(0) = strTableName
  16.  
  17. ' Create the recordset
  18.  
  19. Set rstAutoNumbers = qdfAutoNum.OpenRecordset(dbOpenDynaset)
  20.  
  21. 'Check for empty an empty recordset and proceed with it accordingly
  22.  
  23. If (rstAutoNumbers.BOF) And (rstAutoNumbers.RecordCount = 0) Then
  24. GetNextAutoNumber = -1 ' returned if autonumber can not be created.
  25. Else
  26. rstAutoNumbers.Edit
  27. rstAutoNumbers!LastNumberUsed = _
  28. rstAutoNumbers!LastNumberUsed + rstAutoNumbers!Increment
  29.  
  30. ' update the tblAutoNumbers and return the key value
  31.  
  32. GetNextAutoNumber = rstAutoNumbers!LastNumberUsed
  33. rstAutoNumbers.Update
  34. End If
  35.  
  36. ' Clean up objects
  37.  
  38. rstAutoNumbers.Close
  39. qdfAutoNum.Close
  40. Set rstAutoNumbers = Nothing
  41. Set qdfAutoNum = Nothing
  42.  
  43. DBCurrent.Close
  44. wsCurrent.Close
  45.  
  46.  
  47. End Function
Comments (Newest First)
vsm.9998090111 | Newbie Poster | Jul 5th, 2008
Is it possible to generate a seven digit ID from a name of a customer in such a way that the first three digits are alphabetic (say an acronym of the initials of the customer) and the last four digits are numeric using some logic that uniquely identifies that customer? Say, if the customer name is Vikram Shankar Mathur, I get the ID VSM8819? The problem is if there is a Vivek Shankar Mathur, it should reflect as another unique ID like VSM8820.

Please help.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 9:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC