Passing values from search form to another form

  • Thread starter Lau via AccessMonster.com
  • Start date
L

Lau via AccessMonster.com

On my search form, user can search for client by either entering last name,
first name or both. What I would like to happen is if no client is found,
user would click AddClient button; after clicking, it would open the
registration form with last name and first name filled in with the value(s)
from the search form. Anybody has anymore ideas about how this can be done?
Thanks!
 
S

Steve Sanford

You could use the "openargs" clause of the "DoCmd.OpenForm" command or you
can 'push' the data into the form.

In the search for youhave a button named "AddClient". The code for the click
event would look like this:

'---------------- begin -----------------------------------------
Private Sub AddClient_Click()
On Error GoTo Err_AddClient_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "registration"
DoCmd.OpenForm stDocName, , , stLinkCriteria


'change the control names "Me.FN_Source" & "Me.LN_Source" to the names of
the CONTROLs on the search form

'change the control names "Forms(stDocName).FN_Dest" &
"Forms(stDocName).LN_Dest" to the names of the CONTROLs on the
***registration*** form
' for the following lines:

Forms(stDocName).FN_Dest = Me.FN_Source
Forms(stDocName).LN_Dest = Me.LN_Source

Exit_AddClient_Click:
Exit Sub

Err_AddClient_Click:
MsgBox Err.Description
Resume Exit_AddClient_Click

End Sub
'--------------------- end ------------------------------------

HTH
 
L

Lau via AccessMonster.com

Steve, thanks. Below are my codes for both method 1 and 2. Maybe you can
provide some more help.

********************** Method 1 ************************
The destination form opens; it defaults to the first record in the database
and displays this message "Object Required."
-------------------------------------------------------------------------
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frm_referral_outcome"

DoCmd.OpenForm stDocName, , , stLinkCriteria

frm_referral_outcome.last_name = Me.txtLname
frm_referral_outcome.first_name = Me.txtFname

Exit_cmdAddClient_Click:
Exit Sub

Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click

End Sub
-------------------------------------------------------------------------

********************** Method 2 ************************
Using OpenArgs. The destination form defaults to the first record in the
database and overwrites the values with values from the search form.
-------------------------------------------------------------------------
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim strLname, strFname As String

strLname = Me.txtLname.Value
strFname = Me.txtFname.Value

stDocName = "frm_referral_outcome"

DoCmd.OpenForm stDocName, OpenArgs:=strLname & "|" & strFname

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()
Dim intPos As Integer 'Position of the Pipe

'If Me.Len(OpenArgs) > 0 Then
If Len(OpenArgs) > 0 Then
intPos = InStr(Me.OpenArgs, "|")

If intPos > 0 Then

'testing
MsgBox "firstname= " & Mid$(Me.OpenArgs, intPos + 1) & " and lastname=
" & Left$(Me.OpenArgs, intPos - 1), vbOKOnly

'Retrieve Last Name
Me.last_name.Value = Left$(Me.OpenArgs, intPos - 1)

'Retrieve First Name
Me.first_name.Value = Mid$(Me.OpenArgs, intPos + 1)

End If
End If

End Sub
 
S

Steve Sanford

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
 
L

Lau via AccessMonster.com

Excellent suggestions about variable declaration. Thanks!

Method 1:
-------------------------------
After implementing your suggestion, the destination form
(frm_referral_outcome) loads with blanks and it displays this message:
"The expression you entered refers to an object that is closed or doesn't
exist."


Method 2
-------------------------------
It just displays the message "Invalid use of Null" on the search form right
after I click "AddClient".
However, after adding Null ("") to the assignment statements as below, it
seems to be working!
strLname = Me.txtLname.Value & ""
strFname = Me.txtFname.Value & ""

I prefer method 1 because the VB codes are under search form and they can
easily be debugged and fixed. If you could provide more insights, I would be
very much appreciated!

Thank you for your help.



Steve said:
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
-------------------------------------------------------------------------
 
S

Steve Sanford

About Method 2:
However, after adding Null ("") to the assignment statements as below, it
seems to be working!
strLname = Me.txtLname.Value & ""
strFname = Me.txtFname.Value & ""

This is *not* adding a NULL, it is adding an empty string; acutally it is
converting a NULL to an empty string. It is one way to eliminate NULLs. You
could also use

strFname = Nz(Me.txtFname, "")



I recreated a search form and a data entry form ("frm_referral_outcome"). I
tried both methods and didn't get any errors.
Obviously, I can't see your forms/code, but if you are searching for a name,
how are you passing NULLs?

In the search form, do you have two unbound text boxes in the form header to
enter the first/last names? If the names is not found, do the two text boxes
get cleared? I don't understand why NULLs are being passed.

What code do you have for Method 1?
Is form "frm_referral_outcome" used only for data entry?
Is the record source for form "frm_referral_outcome" a query, a query with
parameters or a table?


Try this
'----------------------------------------------------------
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click

'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.
' does this cause an error?
Forms!frm_referral_outcome.first_name = "Paris"
Forms!frm_referral_outcome.last_name = "Hilton"

'MUST have "Forms!" on these two lines.
' Forms!frm_referral_outcome.first_name = Me.txtFname & ""
' Forms!frm_referral_outcome.last_name = Me.txtLname & ""

Exit_cmdAddClient_Click:
Exit Sub

Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click

End Sub
'----------------------------------------------------------

--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Lau via AccessMonster.com said:
Excellent suggestions about variable declaration. Thanks!

Method 1:
-------------------------------
After implementing your suggestion, the destination form
(frm_referral_outcome) loads with blanks and it displays this message:
"The expression you entered refers to an object that is closed or doesn't
exist."


Method 2
-------------------------------
It just displays the message "Invalid use of Null" on the search form right
after I click "AddClient".
However, after adding Null ("") to the assignment statements as below, it
seems to be working!
strLname = Me.txtLname.Value & ""
strFname = Me.txtFname.Value & ""

I prefer method 1 because the VB codes are under search form and they can
easily be debugged and fixed. If you could provide more insights, I would be
very much appreciated!

Thank you for your help.



Steve said:
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
-------------------------------------------------------------------------
 
L

Lau via AccessMonster.com

You are awesome! Thank you very very much. Below is the code, just in case
somebody wants to use it.

Reffering to Method 1:
I put
DoCmd.Close acForm, "frm_client_search"
after the destination form and it worked!!!


Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click

Dim stLinkCriteria As String

DoCmd.OpenForm "frm_referral_outcome", , , , acFormAdd

Forms!frm_referral_outcome.last_name = forms!frm_client_search.txtLname
Forms!frm_referral_outcome.first_name = forms!frm_client_search.txtFname

DoCmd.Close acForm, "frm_client_search"


Exit_cmdAddClient_Click:
Exit Sub

Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click

End Sub




Steve said:
About Method 2:
However, after adding Null ("") to the assignment statements as below, it
seems to be working!
strLname = Me.txtLname.Value & ""
strFname = Me.txtFname.Value & ""

This is *not* adding a NULL, it is adding an empty string; acutally it is
converting a NULL to an empty string. It is one way to eliminate NULLs. You
could also use

strFname = Nz(Me.txtFname, "")

I recreated a search form and a data entry form ("frm_referral_outcome"). I
tried both methods and didn't get any errors.
Obviously, I can't see your forms/code, but if you are searching for a name,
how are you passing NULLs?

In the search form, do you have two unbound text boxes in the form header to
enter the first/last names? If the names is not found, do the two text boxes
get cleared? I don't understand why NULLs are being passed.

What code do you have for Method 1?
Is form "frm_referral_outcome" used only for data entry?
Is the record source for form "frm_referral_outcome" a query, a query with
parameters or a table?

Try this
'----------------------------------------------------------
Private Sub cmdAddClient_Click()
On Error GoTo Err_cmdAddClient_Click

'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.
' does this cause an error?
Forms!frm_referral_outcome.first_name = "Paris"
Forms!frm_referral_outcome.last_name = "Hilton"

'MUST have "Forms!" on these two lines.
' Forms!frm_referral_outcome.first_name = Me.txtFname & ""
' Forms!frm_referral_outcome.last_name = Me.txtLname & ""

Exit_cmdAddClient_Click:
Exit Sub

Err_cmdAddClient_Click:
MsgBox Err.Description
Resume Exit_cmdAddClient_Click

End Sub
'----------------------------------------------------------
Excellent suggestions about variable declaration. Thanks!
[quoted text clipped - 160 lines]
 

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