One search form - many uses?

C

chris

I have a form [frmProject] which uses a search form (allowing wildcard
searches on several fields) frmApplicPerFind to look up an applicant's
name and fill in details in fields in the main form. The code I am
using on the search form to copy the data is as follows:

Private Sub Command22_Click()
'Sets the Applicant name in the project form to be the same as
the record clicked
Forms!frmProject.txtApplicant = Forms!frmApplicPerFind.FirstName &
" " & Forms!frmApplicPerFind.Surname
'copies the matching person ID into the project record
Forms.frmProject.ApplicPerID = Me!PersonID
'Close the search form
DoCmd.Close acForm, "frmApplicPerFind"

I want to use the same search form from a sub form on frmProject which
will display a list of other people associated with a project. (In
this case I only want the table behind the subform to hold the IDs for
the people and not their names as well). What would be the best way to
use the same search form for both searches as the code will be
different depending on where the found data is to be copied to?

On this subform I want to store only the ID but to display the name.
Can this only be done through a combo or is there some other way? In
this case I will not actually be needing the functions of a combo as
the search form will be providing the look up facility and using one
could be confusing for a user.

Chris
 
T

Tom Wussernark [MSFT]

if you will want to reuse your code then you will need to move to SQL Server
and Access Data Projects

sorry
 
C

chris

if you will want to reuse your code then you will need to move to SQL Server
and Access Data Projects
sorry

I don't want to reuse the code, but I want to reuse the form and have
different actions performed by it according to where the form was
opened from. The problem is really - how can pass information to the
form when it opens I tell it where it has been opened from so that I
can use this to determine the action when a record is selected (the
form displays a list of records matching the search criteria in
continuous forms format).
Chris
 
K

Ken Snell \(MVP\)

I'm not completely clear about the circumstances for the reuse of the code.
However, I see that you've "hard-coded" the name of the main form and the
search form into the code. Is your desire that you can specify either the
main form or the subform as the target of the code's seetting of values?

How will the search form know which (main form or subform) to use? Will it
be determined by which one is used to call the search form?

I'm sure we can help you get the result you want, just give us a bit more
information.
 
C

chris

Yes, it is exactly that. I need the code to be determined according
which one is used to call the search form but I don't know how to pass
this information to the search form.
Chris
 
K

Ken Snell \(MVP\)

There are a number of ways this can be done, depending upon how "flexible"
you want the search form to be with respect to being used by other forms
than just these two.

However, let's assume that you just want to use the main form and the
subform. We'll pass a value to the search form when you open it that
identifies if the main form or the subform called the search form.

In the code for the button on the main form that calls the search form, pass
a 1 as the OpenArgs argument of the DoCmd.OpenForm step:
DoCmd.OpenForm "SearchFormName", , , , , , "1"

In the code for the button on the subform that calls the search form, pass a
2 as the OpenArgs argument of the DoCmd.OpenForm step:
DoCmd.OpenForm "SearchFormName", , , , , , "2"

Then, in the search form's code, use an If..Then block of code to set the
value from the search form:

If Me.OpenArgs = "1" Then
' write the value to the textbox on the main form
ElseIf Me.OpenArgs = "2" Then
' write the value to the textbox on the subform
End If
 
T

Tony Toews [MVP]

Tom Wussernark said:
if you will want to reuse your code then you will need to move to SQL Server
and Access Data Projects

Note that this person is really A a r o n K e m p f and that he is not an employee
of Microsoft.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
T

Tony Toews [MVP]

chris said:
I don't want to reuse the code, but I want to reuse the form and have
different actions performed by it according to where the form was
opened from. The problem is really - how can pass information to the
form when it opens I tell it where it has been opened from so that I
can use this to determine the action when a record is selected (the
form displays a list of records matching the search criteria in
continuous forms format).

Ken has already posted the OpenArgs solution.

And Aaron is wrong.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

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