Creating New Record by addnew

mmdonloaf

New member
Local time
Today, 18:14
Joined
Jun 21, 2007
Messages
4
Hi

Apologies this is likely to be a stupid question, total novice with VBA.

What I'm trying to do is create a new record in a link table and populate it with TaskNo and TaskOwnerRef. TaskNo is selected on the ST3TaskLogfrm form and then opens subform FrmMultiStaff where the TaskOwnerRef is selected and I am using a command button on the subform to add this to the link table.

This is the code Im using for the command button to add a new record and populate.

Private Sub cmdAddStaffToTask_Click()

Dim db As Database
Dim rst As Recordset

Set db = CurrentDb()
Set rst = db.OpenRecordset("TblTaskOwnerLink", dbOpenDynaset)
rst.AddNew 'this adds the new record to the table
rst!TaskNo = [Forms!ST3TaskLogfrm]
rst!TaskOwnerRef = [Me!TaskOwnerRef]

rst.Update
rst.Close

End Sub

The code was inherited with the project and I havn't had chance to build any error handlying into it yet.

I am getting Compile Error: User defined type not defined.

Any help would be grand.
 
What is the name of the form this code is running in and what are the name of the controls holding the values you want?
 
Although it may not be the case here you should be explicit in what kind of recordset you want (DAO or ADODB)
Code:
Dim db As [COLOR="Red"]DAO.[/COLOR]Database
Dim rst As [COLOR="red"]DAO.[/COLOR]Recordset
 
What is the name of the form this code is running in and what are the name of the controls holding the values you want?

The Form the code is running in is "FrmMultiStaff" I need "TaskOwnerRef" from this form, and "TaskNo" from ST3TaskLogfrm.

Cheers
 
Assuming the ST3TaskLogfrm is open then the following code should work:
Code:
Private Sub cmdAddStaffToTask_Click()

   Dim rst As DAO.Recordset
   Set rst = CurrentDb().OpenRecordset("TblTaskOwnerLink", dbOpenDynaset)
   rst.AddNew   'this adds the new record to the table
   rst!TaskNo = Forms!ST3TaskLogfrm.TaskNo
   rst!TaskOwnerRef = Me!TaskOwnerRef
   rst.Update
   rst.Close
   Set rst = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom