Russ, your application version within access needs to be configured to use
ADO (the alternative is to use DAO) recordsets. I almost exclusively use
ADO over DAO since Access 2000 introduced it ( I assumed you are using
Access 2000 or greater). To configure your code for using ADO, you need to
select "References" from the "Tools" drop-down menu where you have the
source code module open and active. Within the list of References, you need
to find and select "Microsoft ActiveX Data Objects x.xx Library", where
x.xx is a version number such as 2.1. The importance of the version number
is dependent on the oldest version of Access installed on your users' PC's
(i.e., you don't want to select a higher version if one of your user's
configuration might not support it).
I will include the DAO equivalent code below, however you need to ensure a
reference for DAO has been selected (i.e., "Microsoft DAO 3.51 Object
Library"):
Private Sub Name_AfterUpdate()
Dim rst As DAO.Recordset
On Error GoTo HandleErr
set rst = currentDB.OpenRecordset("tblMySubTableName", dbOpenDynaset)
With rst
.AddNew
!SubName = Me.txtName
!SubSSN = Me.txtSSN
.Update
End With
rst.Close
ExitHere:
Set rst = Nothing
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical,
"Form_MyFormName.Name_AfterUpdate" 'ErrorHandler:
End Select
End Sub
I am getting the following error in visual basics:
User defined type not defined.
it is highliting the line:
rst As ADODB.Recordset
could you help me fix this problem. thank you
russ
Ron Kunce said:
The last name field on the parent form has an event AfterUpdate method. In
this method you should write code to open a recordset on the sub-table and
use the .addnew function to create a new record in this table then write the
name and ssn (if already entered) from the form to the recordset. see
example below (using ADO recordsets):
Private Sub Name_AfterUpdate()
Dim rst As ADODB.Recordset
On Error GoTo HandleErr
Set rst = New ADODB.Recordset
rst.Open "tblMySubTableName", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
With rst
.AddNew
!SubName = Me.txtName
!SubSSN = Me.txtSSN
.Update
End With
rst.Close
ExitHere:
Set rst = Nothing
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical,
"Form_MyFormName.Name_AfterUpdate" 'ErrorHandler:
End Select
End Sub
just subtitute your sub-table name, sub-table field names and parent form
field names. The .update statement will write a record into your sub-table
with the Name and SSN primary keys from your parent form, with any default
values you have identified for the sub-table. Be sure to close the recordset
and use the set rst = nothing at the end of your routine to clear the memory
space used, whether or not you use the error-handling statements or not.
I have the child form because it has other fields in it. all of the fields
have default values set. if the user doesnt need to change these values,