J
jeh
The primary key of my Tbl_Main is a code rather than an auto-number.
The key for the next new record is the next item in the logical key
sequence. When adding new records to Tbl_Main I use a VBA subroutine
to write the new key to the screen via a Message Box. I then cut and
paste this to the key field in Frm_Main (which has DataEntry = yes). I
want to replace this cumbersome and error-prone process by something
that will cause the new key to automatically appear in
Frm_Main.KeyField.
I had thought that I could do this by copying the newly calculated key
to the next record after the final one in Tbl_Main. The code I'm using
is:
function NextKey()
Dim db As DAO.Database ' Database name
Dim rst As DAO.Recordset ' Recordset name
Dim strNxtId As String
Dim tdf As DAO.TableDef ' Table
' Set table and recordset
Set db = CurrentDb()
Set tdf = db.TableDefs("Tbl_Main")
Set rst = tdf.OpenRecordset
' 30 or so lines of code to evaluate strNxtId.
' The code includes reading from tdf via rst.
MsgBox strNxtId ' To be deleted when this routine works
' stop ' Everything works correctly to this
point.
rst.MoveLast
rst.MoveNext
rst.Fields(0) = strNxtId
end function
The VBA routine works and is fully tested to the MsgBox stage with the
"stop" instruction implemented. The next three lines are designed to
transfer the new primary key to the next blank record in Tbl_Main.
However when I comment "stop" out the run crashes at the
"rst.Fields(0)" line with the message:
"Run time Error 3020, Update or CancelUpdate without AddNew or Edit".
Could someone suggest how I can get round this or, as an alternative, a
better way to do what I'm trying to do? I'm using Access 2000, the
function is called by a RunCode instruction within a Macro and the
crash occurs before Frm_Main is opened.
Hope I've made myself clear
John
The key for the next new record is the next item in the logical key
sequence. When adding new records to Tbl_Main I use a VBA subroutine
to write the new key to the screen via a Message Box. I then cut and
paste this to the key field in Frm_Main (which has DataEntry = yes). I
want to replace this cumbersome and error-prone process by something
that will cause the new key to automatically appear in
Frm_Main.KeyField.
I had thought that I could do this by copying the newly calculated key
to the next record after the final one in Tbl_Main. The code I'm using
is:
function NextKey()
Dim db As DAO.Database ' Database name
Dim rst As DAO.Recordset ' Recordset name
Dim strNxtId As String
Dim tdf As DAO.TableDef ' Table
' Set table and recordset
Set db = CurrentDb()
Set tdf = db.TableDefs("Tbl_Main")
Set rst = tdf.OpenRecordset
' 30 or so lines of code to evaluate strNxtId.
' The code includes reading from tdf via rst.
MsgBox strNxtId ' To be deleted when this routine works
' stop ' Everything works correctly to this
point.
rst.MoveLast
rst.MoveNext
rst.Fields(0) = strNxtId
end function
The VBA routine works and is fully tested to the MsgBox stage with the
"stop" instruction implemented. The next three lines are designed to
transfer the new primary key to the next blank record in Tbl_Main.
However when I comment "stop" out the run crashes at the
"rst.Fields(0)" line with the message:
"Run time Error 3020, Update or CancelUpdate without AddNew or Edit".
Could someone suggest how I can get round this or, as an alternative, a
better way to do what I'm trying to do? I'm using Access 2000, the
function is called by a RunCode instruction within a Macro and the
crash occurs before Frm_Main is opened.
Hope I've made myself clear
John