I’m having a problem with running a custom function within an Access database. Currently, there’s a process in place to import new data, and assign a foreign key (ENTRY_ID) to -1 for all new rows. A separate process runs to change the -1 to the next number in an incremental sequence.
This is the function:
Public Function GetNextEntry(table As String) As Double
Dim r As DAO.Recordset
Dim i As Double
Set r = CurrentDb.OpenRecordset("SELECT LAST_ENTRY_ID FROM LAST_ENTRY WHERE TABLE_NM='" & table & "';")
If r.EOF Then
GetNextEntry = -1
Exit Function
End If
i = r![LAST_ENTRY_ID] + 1
r.Edit
r![LAST_ENTRY_ID] = i
r.Update
GetNextEntry = i
Exit Function
GetNextEntry_ERROR:
GetNextEntry = -1
End Function
And this is an extremely simplified version of the query:
INSERT INTO DATA
SELECT GetNextEntry('DATA') AS ENTRY_ID, IMPORT.*
FROM IMPORT
What I expect to get back would look like
ENTRY_ID, NAME, NUMBER
1, 'account 1', '1234567'
2, 'account 1', '1234567'
3, 'account 1', '1234567'
4, 'account 1', '1234567'
5, 'account 1', '1234567'
But I’m actually getting
ENTRY_ID, NAME, NUMBER
1, 'account 1', '1234567'
1, 'account 1', '1234567'
1, 'account 1', '1234567'
1, 'account 1', '1234567'
1, 'account 1', '1234567'
It seems that the function if running only once for the entire group. I can force it to run multiple times by feeding it data from the resulting dataset, but then if I scroll through the data, it will rerun the function each time the row is redrawn to the screen.
This is my long winded way of asking if there’s a better way to accomplish what I’m after. I assume there must be, because this seems overly complicated.
Access is being smart; it does not realize that your function returns a different
value each time.
Try redefining your function as:
And then
This way Access will think they GetNextEntry depends on a value from import and will get a new value each time.