|
-
Aug 17th, 2012, 01:42 AM
#1
Thread Starter
Member
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.
-
Aug 17th, 2012, 02:30 AM
#2
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
-
Aug 17th, 2012, 08:01 AM
#3
Thread Starter
Member
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
-
Aug 17th, 2012, 11:43 AM
#4
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|