previously I was given this code for getting max id on multi user environment, but I think this is for mssql. I am using mysql and I am getting error.
Code:
Public loggeduserid As Integer
Public Function FirstAvailablePositiveInteger(po_DatabaseConnection As ADODB.Connection, ByVal p_TableName As String, ByVal p_TemporaryTableName As String, ByVal p_PositiveIntegersFieldName As String) As Long
' Only works if scanned field is guaranteed to use only positive integers!
' po_DatabaseConnection : The ADODB database connection that contains the table we want to
' find the first available positive integer in
' p_TableName : The name of the table that we are searching in
' p_TemporaryTableName : The name of a table that will be used for temporary storage
' IMPORTANT NOTE: The data in this table will be delete
' and the table will be dropped!
' DO NOT USE THIS TABLENAME FOR ANYTHING ELSE
' p_PrimaryIntegersFieldName : The field name we will be searching for the first unused value
Dim lo_Rs As ADODB.Recordset
Dim l_AvailablePositiveInteger As Long
Dim l_CreatedTemporaryTable As Boolean
Dim l_TransactionOpen As Boolean
Dim i As Long
Dim l_Max As Long
Dim l_ErrNum As Long
Dim l_ErrDesc As String
On Error GoTo ErrorHandler
If po_DatabaseConnection Is Nothing Then
Err.Raise 5, , "Database connection not created or open!"
End If
If po_DatabaseConnection.State <> adStateOpen Then
Err.Raise 5, , "Database connection not open!"
End If
Set lo_Rs = New ADODB.Recordset
lo_Rs.Open "SELECT COUNT(*) AS record_count, MAX(" & p_PositiveIntegersFieldName & ")-COUNT(*) AS missing_recordcount, MAX(" & p_PositiveIntegersFieldName & ") AS max_integer, MAX(" & p_PositiveIntegersFieldName & ")*(MAX(" & p_PositiveIntegersFieldName & ")+1)/2-SUM(" & p_PositiveIntegersFieldName & ") AS missing_sum FROM " & p_TableName, po_DatabaseConnection, adOpenStatic, adLockReadOnly, adCmdText
If lo_Rs.Fields("record_count").Value = 0 Then
' No records in table, return 1
l_AvailablePositiveInteger = 1
Else
Select Case lo_Rs.Fields("missing_recordcount").Value
Case 0
' There are no missing numbers, so return the maximum used number +1
l_AvailablePositiveInteger = lo_Rs.Fields("max_integer").Value + 1
Case 1
' There is only 1 deleted record, sothe sum of our missing positive integers must be the first available number
l_AvailablePositiveInteger = lo_Rs.Fields("missing_sum").Value
Case Else
' More than 1 record has been deleted
' Create a temporary table that with an autonumber column
' And select in the primary integer column for all of the rows from the main table
' The first row where the primary integer column value doesn't match the autonumber column value
' is our first missing number
lo_Rs.Close
po_DatabaseConnection.BeginTrans
l_TransactionOpen = True
On Error Resume Next
po_DatabaseConnection.Execute "DROP TABLE " & p_TemporaryTableName
po_DatabaseConnection.Execute "DROP TABLE " & p_TemporaryTableName
po_DatabaseConnection.Execute "DROP TABLE " & p_TemporaryTableName
po_DatabaseConnection.Execute "DROP TABLE " & p_TemporaryTableName
On Error GoTo ErrorHandler
po_DatabaseConnection.Execute "CREATE TABLE " & p_TemporaryTableName & " (id int(11), positive_integer int(15),PRIMARY KEY(id));"
po_DatabaseConnection.Execute "CREATE INDEX idx_integers ON " & p_TemporaryTableName & " (positive_integer ASC);"
l_CreatedTemporaryTable = True
po_DatabaseConnection.Execute "INSERT INTO " & p_TemporaryTableName & " (positive_integer) SELECT " & p_PositiveIntegersFieldName & " FROM " & p_TableName & " ORDER BY " & p_PositiveIntegersFieldName & " ASC"
po_DatabaseConnection.CommitTrans
l_TransactionOpen = False
lo_Rs.Open "SELECT id FROM " & p_TemporaryTableName & " WHERE id<>positive_integer ORDER BY id ASC", po_DatabaseConnection, adOpenStatic, adLockReadOnly, adCmdText
If lo_Rs.State = adStateOpen Then
If lo_Rs.RecordCount = 0 Then
' Sanity check - shouldn't get here
Debug.Assert False
l_AvailablePositiveInteger = 1
Else
On Error Resume Next
lo_Rs.MoveFirst
On Error GoTo ErrorHandler
If lo_Rs.EOF Then
'Debug.Assert False
MsgBox Err.Description
' Sanity check - don't think we should ever get here!
End If
End If
Else
' Sanity check - shouldn't get here
Debug.Assert False
Err.Raise vbObjectError, , "Could not open temporary table."
End If
l_AvailablePositiveInteger = lo_Rs.Fields("id").Value
End Select
End If
If l_AvailablePositiveInteger = 0 Then
' Sanity check! Don't think we should ever get here
Err.Raise vbObjectError, , "Unexpected Error! Could not determine first available positive integer for table '" & p_TableName & "', column '" & p_PositiveIntegersFieldName & "'"
End If
FirstAvailablePositiveInteger = l_AvailablePositiveInteger
Cleanup:
On Error Resume Next
If Not lo_Rs Is Nothing Then
If lo_Rs.State = adStateOpen Then
lo_Rs.Close
End If
Set lo_Rs = Nothing
End If
If l_TransactionOpen Then
l_TransactionOpen = False
If Not po_DatabaseConnection Is Nothing Then
po_DatabaseConnection.RollbackTrans
End If
End If
If l_CreatedTemporaryTable Then
l_CreatedTemporaryTable = False
If Not po_DatabaseConnection Is Nothing Then
po_DatabaseConnection.Execute "DROP TABLE " & p_TemporaryTableName
End If
End If
If l_ErrNum <> 0 Then
On Error GoTo 0
Err.Raise l_ErrNum, , l_ErrDesc
End If
Exit Function
ErrorHandler:
l_ErrNum = Err.number
l_ErrDesc = Err.Description
Resume Cleanup
End Function