Pass Input Text Field From 1 Form to Secondary Form

R

Robert Nusz @ DPS

I have an vehicle accident application using two different tables. Table 1 is
primary driver record set. Table 2 would be or could be child record set of
1st, (secondary drivers). I want to be able to create a record type 1 on
entry form, then have command button to "Add Driver" and open 2nd form adding
other driver involved records. I want to save the accident CASE_NUM input on
form 1, and carry that value over to Add Driver form 2 CASE_NUM. I've tried
SetValue and have had no luck, command button works, opens 2nd form, places
focus in proper position, but fails to populate the CASE_NUM field. Any
suggestions would be greatly appreciated.

Bits of Code follows:

Form 1 name is: FR_CR_E
Field name is: Saved_Case_Num
Control Source: = [CASE_NUM]

Form: FR_CR_E has "Add Driver" command button with the following code:
name = Add_Drivers
Caption = &Add Drivers
Tag = Add_Drivers

ON CLICK code is:

Private Sub Add_Drivers_Click()
On Error GoTo Err_Add_Drivers_Click

Dim stDocName As String

strDocName = "Fr_Add_Driver_E"
' Open Fr_Add_Driver_E form in data-entry mode and store CASE_NUM
' the form's OpenArgs property.
DoCmd.OpenForm strDocName, , , , acAdd, , Me!Saved_Case_Num

' give DRIVER_NUM control focus
Forms![Fr_Add_Driver_E]!DRIVER_NUM.SetFocus

Exit_Add_Drivers_Click:
Exit Sub

Err_Add_Drivers_Click:
MsgBox Err.Description
Resume Exit_Add_Drivers_Click

End Sub

2nd form opens and places cursor in proper field, allows entry of new
secondary drivers, but fails to carry over and place the CASE_NUM field from
previous form.
Any suggestions or assistance would be greatly appreciated. Thanks in
advance.
 
P

Peter Hammond

Hi Robert,

what I usually do in this case is to use the "BeforeInsert" event in the
secondary form.
Something like this:

Private Sub Form_BeforeInsert(Cancel As Integer) 'This is the BeforeInsert
event in the _secondary_ form

[Table2.CaseNum] = Forms![MainForm]![CaseNum]

End Sub

this works provided that you use a Query as the source to the Form.
this is another example using text controls instead of inserting the value
directly on the table field

Private Sub Form_BeforeInsert(Cancel As Integer)
Me.Form2.CaseNum = Forms![MainForm]![CaseNum]
End Sub

of course you would first have to validate that the CaseNum in the primary
form is not null (if your caseNum field in your table2 is a key). I'm sure
there are other ways to do it, but this one works fine for me...

Hope this helps

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top