Results 1 to 4 of 4

Thread: Generate auto OR # in vb6 & MS Access

  1. #1
    Member
    Join Date
    Aug 12
    Posts
    42

    Generate auto OR # in vb6 & MS Access

    Hey guys,

    Anyone help to generate ID reference, for ex.
    R-00000001
    R-00000002
    R-00000003
    ...... and so on.

  2. #2
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,157

    Re: Generate auto OR # in vb6 & MS Access

    I think you're going to have to use an AutoNumber column in Access and then 'build' the 'front end' after the SELECT.

    For example if the Autonumber Column name was 'ID' and it was in a RecordSet named RS
    Code:
    strID = "R-" & Left$("00000000",8 - Len(CStr(RS![ID]))) & CStr(RS![ID])
    and to unblock to the original numeric: (assuming it's a Long Type):
    Code:
    lngID = CLng(Mid$(strID, 3))
    Last edited by Doogle; Aug 17th, 2012 at 03:17 AM. Reason: Missed out a CStr in the Len Function

  3. #3
    Member
    Join Date
    Aug 12
    Posts
    42

    Re: Generate auto OR # in vb6 & MS Access

    Found this code in the internet, I like the return ID format in this code but does'nt work in vb6, I get error compile error sub or function not defined.. highlighted on the IsDbNull
    Code:
    
    'This function is For id Field like 'S00001','S00002','S00003','E00001','E00002','E00003'..
    Public Function Generate_No1(Table_Name As String, Auto_Field_Name As String, CategoryKey As String, Optional IdLength As Integer = 5) As String
    Dim con As New Connection
    Dim rs As New Recordset
    Dim MAx_No As Integer
    
    ' write the codding for connection
    'you can use any connection
    
     rs.Open ("Select Max(" & Auto_Field_Name & ") from " & Table_Name & " Where " & Auto_Field_Name & " like '" & CategoryKey & "*'"), con
    ' in the above Segment change the wildcard depends on the Database ; For Access *, For Sqlserver %, and so on
    ' Where condition Filter the Records
    ' Since it is formated it will gives the Biggest value
    ' Note : S0002 is smaller than s0019 but S002 is bigger than S0019 so it will return s003 
    ' so when you prepare Id Field give at least 6 characters for the keyfield
    If Not rs.EOF Then
         If Not IsDbNull(rs(0)) then
              MAx_No = Val(Right(rs(0), Len(rs(0)) - Len(CategoryKey))) + 1
         Else
              MAx_No = 1
         End if
         Generate_No1 = CategoryKey + Format(MAx_No, String(IdLength - Len(CategoryKey), "0"))
    Else
         MAx_No = 1
         Generate_No1 = CategoryKey + Format(MAx_No, String(IdLength - Len(CategoryKey), "0"))
    End If
    End Function

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,659

    Re: Generate auto OR # in vb6 & MS Access

    I'd use an autonumber, then create an expression field that formats it the way you want it to look.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •