Hi Scott,
I played around with append queries but could not get anything but
blank records to add.
Try this:
*********************Begin Code*******************
Option Compare Database
Option Explicit
Function GetTask()
On Error GoTo ProcError
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim varLastRecord As Integer
Dim strSQL As String
Set db = CurrentDb()
Set rec = db.OpenRecordset("tblTask")
If rec.RecordCount > 0 Then
rec.MoveLast
varLastRecord = rec(0)
strSQL = "INSERT INTO tblNewTasks ( TaskID ) " _
& "VALUES (" & varLastRecord & ");"
db.Execute strSQL, dbFailOnError
End If
ExitProc:
On Error Resume Next 'Cleanup
rec.Close: Set rec = Nothing
db.Close: Set db = Nothing
Exit Function
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure GetTask..."
Resume ExitProc
End Function
*********************End Code******************
Notes:
varLastRecord = rec("FieldName") might be a bit safer than
varLastRecord = rec(0)
It would certainly improve the readability of your code.
The line of code above that includes:
"INSERT INTO tblNewTasks ( TaskID ) "
should be modified to insert into the proper table name and field name for
your new table.
Your code, as it was written, is dependent on the priority of references if
you have both the DAO and ADO libraries included as checked references. I
always recommend explicit declarations. For example, use:
Dim rec As DAO.Recordset instead of Dim rec As Recordset
You'll notice that I used this form in the code that I presented above.
The reason is that both libraries (DAO & ADO) include Recordset. The code
will compile fine, as long as one of these libraries is included. However,
you can get a run-time error if the ADO library has a higher priority (listed
before the DAO library). Here is an article where you can read more about
this issue:
ADO and DAO Library References in Access Databases
http://www.access.qbuilt.com/html/gem_tips1.html
Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
:
Tom:
Here is the function I wrote:
Function GetTask()
Dim db As Database
Dim rec As Recordset
Dim varLastRecord As Integer
Dim tbl As TableDef
Set db = CurrentDb()
Set rec = db.OpenRecordset("tblTask")
rec.MoveLast
varLastRecord = rec(0)
Debug.Print varLastRecord
rec.Close
End Function
I used the Debug.Print to confirm that varLastRecord has the value I want
but I am not sure how to take the next step to get that value into a table.
I played around with append queries but could not get anything but blank
records to add.
Thank you in advance for any help you may be able to provide.
Scott
__________________________________________
:
By "pass that number into another table", do you mean that you want to add
that number to another table? If so, an append query should work.
Tom
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
:
I have a function that returns the value of the last autonumber from a table
and want to pass that number into another table? Any thoughts on how to do
it?