Hi
If you really want to do it the hard way, something like:
Option Compare Database
Option Explicit
Public Function NextString(strPrimeKey As String, strTableName As String, strPrefix As String, intKeyLength As Integer) As String
Dim Db As DAO.Database
Dim Rs As DAO.Recordset
Dim strSQL As String
'
Set Db = CurrentDb()
If Len(strPrefix) = 0 Then
strSQL = "SELECT " & strPrimeKey & " FROM " & strTableName & " ORDER BY " & _
strPrimeKey & " DESC;"
Else
strSQL = "SELECT " & strPrimeKey & " FROM " & strTableName & " " & _
"WHERE Left(" & strPrimeKey & "," & Len(strPrefix) & ") = " & Chr(34) & _
strPrefix & Chr(34) & " ORDER BY " & _
strPrimeKey & " DESC;"
End If
Set Rs = Db.OpenRecordset(strSQL, dbOpenForwardOnly)
If Rs.RecordCount < 1 Then
NextString = strPrefix & Format(1, String(intKeyLength, "0"))
Else
NextString = strPrefix & Format(Val(Mid(Rs(strPrimeKey), Len(strPrefix) + 1)) + 1, String(intKeyLength, "0"))
End If
Rs.Close
Set Rs = Nothing
Set Db = Nothing
End Function
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click
DoCmd.Close
Exit_cmdExit_Click:
Exit Sub
Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click
End Sub
Private Sub cmdSave_Click()
On Error GoTo Error_cmdSave
DoCmd.RunCommand acCmdSaveRecord
Exit_cmdSave:
Exit Sub
Error_cmdSave:
Select Case Err.Number
Case 3022
strPrimeKey = NextString("strPrimeKey", "tblTestString", "", 7)
Resume
Case Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdSave
End Select
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(strPrimeKey) Then
strPrimeKey = NextString("strPrimeKey", "tblTeststring", Format(Date, "YY-MM"), 2)
End If
End Sub
Private Sub cmdNew_Click()
On Error GoTo Err_cmdNew_Click
DoCmd.GoToRecord , , acNewRec
Exit_cmdNew_Click:
Exit Sub
Err_cmdNew_Click:
MsgBox Err.Description
Resume Exit_cmdNew_Click
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Select Case DataErr
Case 3022
lngPrimeKey = NextNumber("lngPrimeKey", "tblTest", Format(Date, "YY/MM"), 2)
Response = acDataErrContinue
Case Else
MsgBox "Error " & DataErr & " " & Error(DataErr)
Response = acDataErrContinue
End Select
End Sub
will do it and should work ok in Multi user environment
if you need explanation post back
Regards
Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now -
UK