Hi Lau,
In Method 1, you are getting the error because of improper syntax. When
refering to a different form, you MUST use
Forms!formName
You can do something like
Dim AddForm as Form
AddForm = "Forms!formName"
AddForm.MyControl = 16
but "Forms!" must be there somewhere.
Here is Method1 with corrections:
************* Method 1 w/Corrections ***************
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click
'Dim stDocName As String
'stDocName = "frm_referral_outcome"
'acFormAdd puts form in Add mode - cannot edit other records.
'added "acFormAdd' argument
DoCmd.OpenForm "frm_referral_outcome",,,, acFormAdd
'MUST have "Forms!" on these two lines.
Forms!frm_referral_outcome.last_name = Me.txtLname
Forms!frm_referral_outcome.first_name = Me.txtFname
Exit_cmdAddClient_Click:
Exit Sub
Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click
End Sub
-------------------------------------------------------------------------
One more note on Method1. I use :
Dim stDocName As String
stDocName = "frm_referral_outcome"
DoCmd.OpenForm stDocName, , , , acFormAdd
Forms(stDocName).tblast_name = Me.txtLname
Forms(stDocName).tbfirst_name = Me.txtFname
because I only have to change the name of the form in *one* place if (when)
I reuse this snippet in other places. Less chance of spelling errors. <g>
=============================
FYI - When you declare variables like this:
Dim strLname, strFname As String
you have delcared "strLname" as a variant and "strFname" as a String. To
delcare both as strings, use:
Dim strLname As String, strFname As String
I put each variable on a separate line (personal preferance)
Dim strLname As String
Dim strFname As String
OK, now Method 2............
Well, I changed a lot. Look it over...
************* Method 2 w/Corrections ***************
-------------------------------------------------------------------------
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click
Dim stDocName As String
Dim strLname As String
Dim strFname As String
strLname = Me.txtLname.Value
strFname = Me.txtFname.Value
stDocName = "frm_referral_outcome"
DoCmd.OpenForm stDocName, OpenArgs:=strFname & "," & strLname,
DataMode:=acFormAdd
Exit_cmdAddClient_Click:
Exit Sub
Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click
End Sub
-------------------------------------------------------------------------
***** VB codes on the destination form (frm_referral_outcome) ***
-------------------------------------------------------------------------
Private Sub Form_Load()
'variant
Dim arNames
'zero based array - two elements -> 0 & 1
arNames = Array(1)
If Len(Trim(Nz(OpenArgs, ""))) > 0 Then
arNames = Split(Me.OpenArgs, ",")
'testing
MsgBox "firstname = " & arNames(0) & " and lastname= " & arNames(1),
vbOKOnly
'Retrieve First Name
Me.first_name = arNames(0)
'Retrieve Last Name
Me.last_name = arNames(1)
End If
End Sub
-------------------------------------------------------------------------
Another note: If a field name is last_name and you create a control bound to
that field, Access names the control the same as the field. It is good
practice to rename controls - example: if field name = "last_name" , rename
textbox control with prefix of "tb" (tb = textbox); as in "tbLast_name ".
I made tables and forms to test the code, but I might not have put all the
variable names back to you names.
HTH
Steve, thanks. Below are my codes for both method 1 and 2. Maybe you can
provide some more help.
[quoted text clipped - 79 lines]
End Sub
-------------------------------------------------------------------------