Welcome to the forum, Jozef.
I am confused about what you are trying to do. You have a form "frmInputNewRec", but in the code you open another form??
Is this a multi-used dB?
If yes, how many users a one time?
Is the db split?
I would probably use the form before uodate event if there are other fields that need to have data entered.
If it is only enter "building" (name?), then using the control after update event would be OK.
You could post your dB with just a couple of example records.......
I did revise your code.. this is what I came up with:
Code:
Private Sub Building_AfterUpdate()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim iNextNum As Integer
Dim sBuildingDef As String
Dim sSQL As String
Set dbs = CurrentDb
sBuildingDef = Forms!frmInputNewRec!Building
sSQL = "SELECT TOP 1 DocumentRegistry.DocNum"
sSQL = sSQL & " FROM DocumentRegistry RIGHT JOIN Activity_Registry ON DocumentRegistry.DocNum = Activity_Registry.DocNum"
sSQL = sSQL = " WHERE DocumentRegistry.DocNum Like " & sBuildingDef & "*;"
sSQL = sSQL = " ORDER BY DocumentRegistry.DocNum DESC , Activity_Registry.DocNum DESC"
' Debug.Print sSQL
Set rst = dbs.OpenRecordset(sSQL)
If Not rst.BOF And Not rst.EOF Then
MsgBox sBuildingDef
MsgBox rst!DocNum
iNextNum = Val(Right(rst!DocNum, 4)) + 1
MsgBox iNextNum, vbYesNoCancel
If vbYes Then
sSQL = "INSERT INTO DocumentRegistry ( DocNum )"
sSQL = sSQL & " VALUES ('" & (sBuildingDef & iNextNum) & "');"
' Debug.Print sSQL
dbs.Execute sSQL, dbFailOnError
End If
End If
rst.Close
Set rst = Nothing
Set dbs = Nothing
End Sub
Not saying it will work, but ............