Full Version: Using vba to add record to subform
UtterAccess Forums > Microsoft® Access > Access Forms
pduffer
I have a project where I populate a set of tables with information using a form and subforms. The main form (and underlying table) has data that will not be duplicated, but the subforms and their tables may have duplicate information. For example, a class may have different meeting times or locations on different days so the main form has the basic class data and the subform has a record for each day/time/location combination.
sers enter information for new classes, or retrieve information for existing classes and indicate changes to be made. To retrieve information for existing classes, the forms and related tables need to be populated with data from similar tables related to the class to be changed. The problem I have is that I don't know how to get my suform that has the day/time/location information to add a new record to be populated with data for classes with multiple meeting details. The code I have thus far is listed below, with a statement indicating where I need to add a new record to the table for the subform. I imagine this is a really simple thing to accomplish. Thanks for all help.
Ray
Private Sub CRN_AfterUpdate()
Dim db As Database
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim counter2 As Integer

Set db = CurrentDb
Set rst = db.OpenRecordset("tblClassData", dbOpenDynaset)
Set rst2 = db.OpenRecordset("tblClassMeeting", dbOpenDynaset)
counter2 = 0

With rst
.MoveFirst
While Not .EOF
If rst!Term = Me!Term And rst!CRN = Me!CRN Then
Me.SUBJ = rst!SUBJ
Me.CRSE = rst!CRSE
Me!Title = rst!Title
Me!ClassID = rst!ClassID

With rst2
.MoveFirst
While Not .EOF
If rst2!ClassID = rst!ClassID Then
If counter2 <> 0 Then
'This is where I need to add a new record to the subform to be populated
End If
Forms![frmRequestEntry]![Meeting Day/Time/Room Information].Form![Days] = rst2!Days
Forms![frmRequestEntry]![Meeting Day/Time/Room Information].Form![Begin] = rst2!Begin
Forms![frmRequestEntry]![Meeting Day/Time/Room Information].Form![End] = rst2!End
Forms![frmRequestEntry]![Meeting Day/Time/Room Information].Form![Building] = rst2!Building
Forms![frmRequestEntry]![Meeting Day/Time/Room Information].Form![Room] = rst2!Room
counter2 = counter2 + 1
End If
.MoveNext
Wend
End With
Exit Sub
Else
.MoveNext
End If
Wend
If .EOF Then
Exit Sub
End If
End With
End Sub
NorthNone
This is my gold standard for addressing subforms from parent forms and parent forms from subforms. Perhaps it can help you.
Refer to Form and Subform Properties and Controls
pduffer
Thanks, but I had already found that ... and it didn't help.
just tried something I found elsewhere on the web, and it worked!
Here's part of the post with the helpful information ...
<

My problem is now solved!
Ray
NorthNone
Thanks for posting the solution for others who may need it in the future!
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.