I am using MYSQL DB.
I am having a problem on Generating unique number, checking the database table if it is already taken.
my problem. 2 users save record at the same time. and they get same unique number.
here are my codes :
DATABASE CONNECTION
Any help? I need your help guys.
I am having a problem on Generating unique number, checking the database table if it is already taken.
my problem. 2 users save record at the same time. and they get same unique number.
here are my codes :
Code:
Public Sub NewEmployeeCode() ' NEW EMPLOYEE CODE
On Error Resume Next
Dim sqlCriteria As String
Dim sCODE As String
Dim eCode As String
sCODE = "E"
sqlCriteria = "SELECT * FROM sql_Employee ORDER BY sNumber ASC" ' CRITERIA
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sqlCriteria, cn ' OPEN MYSQL CONNECTION
If rs.RecordCount = 0 Then
eCode = sCODE & "-00001"
ElseIf rs.RecordCount > 0 Then
If Not rs.EOF Then
rs.MoveLast
eCode = sCODE & "-" & Format(Val(Right(rs!sNUMBER, 5) + 1), "00000")
End If
End If
frm_Employee.txtNumber.Text = eCode ' SET EMPLOYEE CODE
Set rs = Nothing
End Sub
Public Sub CheckEmployeeNumber() ' CHECK NUMBER BEFORE SAVING
On Error Resume Next
Dim sqlCriteria As String
Dim sCODE As String
sCODE = frm_Employee.txtNumber.Text
sqlCriteria = "SELECT * FROM sql_Employee WHERE sNumber='" & sCODE & "'" ' CRITERIA
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sqlCriteria, cn
If rs.EOF Then
Call NewEmployeeCode ' New Employee code
Else ' re-generate if exist
Call NewEmployeeCode ' New Employee code
End If
Set rs = Nothing
End Sub
Code:
Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
& "SERVER=localhost;" _
& "DATABASE=sqlpic;" _
& "UID=root;" _
& "PWD=ssmpc;" _
& "OPTION=3"
cn.Open