OnClick in List Box to make a new record

  • Thread starter Thread starter Bradley C. Hammerstrom
  • Start date Start date
B

Bradley C. Hammerstrom

A2K

I want to click an item in a list box on a Main form and create a new record
in the Subform with the same field automatically filled in.

For example, first of all the main form has an unbound combo to select a
student name. That combo has an AfterUpdate event that uses the FindFirst
method to display records with only that student. Next the user clicks the
list box to select a class. That click event creates a new record in the
subform (for that student and for that class) to which other info may be
added manually.

What is the Event code to create a new record and fill-in a field based on
the selection in the list box?

Brad H.
 
Hi Brad:

Here's a typical line to make a new record - just place in the form's
Current() property:

If Me.NewRecord = -1 Then ' if new record
' make the recordnumber unique by adding 1 to the highest record number
[ProtocolNo] = DMax("[ProtocolNo]", "ProtocolTable") + 1
' tell user to name the new record
[ProtocolName] = "Please Name Record..."
MsgBox "A new record has been added!", vbInformation
[List46].SetFocus
Me.Refresh
Exit Sub
End If
' Find the record that matches the control.
Dim rst As Object
Set rst = Me.Recordset.Clone
rst.FindFirst "[ProtocolNo] = " & str(Me![List46].Value)
Me.Bookmark = rst.Bookmark
rst.Close

Regards,
Al
 
Actually, Access has a way to do all of this without much coding at all.

First, be sure there is a relationship defined between your Students table
and your Classes table, with referential integrity enforced and a cascading
update.

Now set the LinkMasterFields of the subform control to the combobox, and the
LinkChildFields to the Student field of the table underlying the subform.
Now whenever you start a new record in the subform, the Student field should
automatically fill.

BTW -
can you assure that you'll never have two students with the same name?

= Turtle
 
Back
Top