I've changed a little a Function I found here at Mr.Excel..
What is does: Assignes codes for each box I have...
But when I add a new record and don`t want a code assigned for it i'd like to close the form without saving it, you undersant?
the code assignes for a field called COD the value of
{idempresa} - 000.0001 for the first box of this "idempresa"
for the second... {idempresa} - 000.0002
and so on...
What I'd like to have is a button that would CANCEL this reg add...
Thanks!!!
Function GetNextPKValue(IDEmpresa) As String
Dim adoRS As ADODB.Recordset, strSQL As String
Dim strPotentialValue As String, lngSuffix As Long
strPotentialValue = Format(Val(IDEmpresa), "00")
'Search to see if any record exists for the current day
'CHANGE THIS TO MATCH YOUR TABLE/FIELD NAME
strSQL = "SELECT TOP 1 Caixa.cod FROM Caixa WHERE Caixa.cod Like '" & _
strPotentialValue & "%' ORDER BY Caixa.cod DESC;"
Set adoRS = CreateObject("ADODB.RecordSet")
adoRS.Open strSQL, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
If adoRS.BOF And adoRS.EOF Then 'There are no values for this date
GetNextPKValue = strPotentialValue & "-000.0001" 'so it must be record number 1 for the current date
adoRS.Close
Exit Function
End If
ww = Val(Right$(adoRS.Fields(0), 8))
mm = Format(Val(Left$(ww, 3)), "000") & Format(Val(Right$(ww, 4)), "0000") + 1
xx = "-" & Left(mm, 3) & "." & Right(mm, 4)
GetNextPKValue = Left$(strPotentialValue, 2) & xx
adoRS.Close
End Function