Pasting result of search

P

Peter Kinsman

I will start by warning that I come from a world of procedural programming,
where problems like this would not occur.

In several applications, it is necessary to search for a Product Code, which
is as difficult as you want to make it - I have recently found that the Text
property of a TextBox in conjunction with its KeyUp event can be most useful
in restricting the records presented in a search. The problem I have is in
pasting the result of the search into the original form, when, within the
same database, several forms can call the same search form. I have tried to
do it using the OpenArgs property of the search form, but this means that
the coding behind the search form has to be altered each time a new form
makes use of it

Does anyone have any suggestions please?

Many thanks

Peter Kinsman
 
G

Graham Mandeno

Hi Peter

I'm sorry, I might be a bit dense, but I have no idea from your message what
it is you are trying to achieve.

Can you please give some more details about how you want to invoke the
search, what you want to search for, how you want the outcome of the search
presented, and what you mean by "pasting the result of the search into the
original form".

I suspect this may simply be a case of constructing and applying a filter.
 
A

Allen Browne

There's a couple of ways to do this, Peter. Here's one.

I take it that your search form searches for something like a ClientID value
that ultimately gets returned to a particular text box or combo on the
calling form. One way to do this would be to declare a public object, set it
before calling the search form, and assign the value to it from the search
form.

In the General Declarations section of a standard module (at the top, with
the Option statements):
Public gctlSearchID As Control

In the code that calls the search form where you want the value returned to
cboClientID:
Set gctlSearchID = Me.cboClientID
DoCmd.OpenForm "frmSearch"

In the command button on the search form that returns the current row's
ClientID value:
If Not gctlSearchID Is Nothing Then
gctlSearchID = Me.ClientID
DoCmd.Close acForm, Me.Name
End If
and in the Close event of the search form:
Set gctlSearchID = Nothing

This works where:
- The search form was called without returning a value (since Form_Close
clears the variable.)

- The search form is called without anywhere to return a value to (since we
test if it is Nothing.)

- The search form was called from a subform: since the control is a valid
reference, we don't need a Forms!xxx approach (which would fail for
subforms.)

- The search form was called again from another form before the first call
was closed (since this just reassigns the public variable), or you could
open the search form in dialog mode if you want to prevent this.
 
P

Peter Kinsman

Graham

A typical application handles fish in a cold store. Whether the main form
handles Receipts, Sales or Transfers, the user needs to be able to select
the Product Code, but the information to be pasted into the main form will
differ from one activity to the next.
I hope that clarifies what I am trying to achieve.

Peter

Graham Mandeno said:
Hi Peter

I'm sorry, I might be a bit dense, but I have no idea from your message
what it is you are trying to achieve.

Can you please give some more details about how you want to invoke the
search, what you want to search for, how you want the outcome of the
search presented, and what you mean by "pasting the result of the search
into the original form".

I suspect this may simply be a case of constructing and applying a filter.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Peter Kinsman said:
I will start by warning that I come from a world of procedural
programming, where problems like this would not occur.

In several applications, it is necessary to search for a Product Code,
which is as difficult as you want to make it - I have recently found that
the Text property of a TextBox in conjunction with its KeyUp event can be
most useful in restricting the records presented in a search. The
problem I have is in pasting the result of the search into the original
form, when, within the same database, several forms can call the same
search form. I have tried to do it using the OpenArgs property of the
search form, but this means that the coding behind the search form has to
be altered each time a new form makes use of it

Does anyone have any suggestions please?

Many thanks

Peter Kinsman
 
G

Graham Mandeno

Hi Peter

So, do you want a general-purpose form to look up a product code, and then
use the product code selected to either find a matching record in the
current form (as in your product maintenance form), or to insert into a
field in a new record (as in an order entry form)?

I suggest you write a jacket function which opens your lookup form in dialog
mode. After the user has found and selected a product, the lookup form
makes itself invisible and the function returns the selected product code.

Something like this:

Public Function LookupProduct() as Variant
Const FormName = "frmLookupProduct"
If SysCmd(acSysCmdGetObjectState, acForm, FormName) Then
' form is already open - set no selection and show the form
With Forms(FormName)
.SelectedProduct = Null
.Visible = True
End With
Else
DoCmd.OpenForm FormName, WindowMode:=acDialog
End If
' wait till the form is made invisible (or perchance closed)
If SysCmd(acSysCmdGetObjectState, acForm, FormName) Then
LookupProduct = Forms(FormName).SelectedProduct
End If
End Function

In your lookup form, declare a module-level public variable:

Public SelectedProduct as Variant

and when the user selects a product:

SelectedProduct = <the selected product code>
Me.Visible = False

You can then call your function whenever you want to look up a product code,
no matter what the context.

Note that, because you are just hiding the form between lookups and not
closing it, each successive lookup will return to the form as it was for the
last search, with the same selection criteria etc. I imagine this would be
more desirable than closing the form between lookups and retorting each one
form scratch. If not, you can just close the form after retrieving the
value.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand



Peter Kinsman said:
Graham

A typical application handles fish in a cold store. Whether the main form
handles Receipts, Sales or Transfers, the user needs to be able to select
the Product Code, but the information to be pasted into the main form will
differ from one activity to the next.
I hope that clarifies what I am trying to achieve.

Peter

Graham Mandeno said:
Hi Peter

I'm sorry, I might be a bit dense, but I have no idea from your message
what it is you are trying to achieve.

Can you please give some more details about how you want to invoke the
search, what you want to search for, how you want the outcome of the
search presented, and what you mean by "pasting the result of the search
into the original form".

I suspect this may simply be a case of constructing and applying a
filter.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Peter Kinsman said:
I will start by warning that I come from a world of procedural
programming, where problems like this would not occur.

In several applications, it is necessary to search for a Product Code,
which is as difficult as you want to make it - I have recently found
that the Text property of a TextBox in conjunction with its KeyUp event
can be most useful in restricting the records presented in a search.
The problem I have is in pasting the result of the search into the
original form, when, within the same database, several forms can call
the same search form. I have tried to do it using the OpenArgs property
of the search form, but this means that the coding behind the search
form has to be altered each time a new form makes use of it

Does anyone have any suggestions please?

Many thanks

Peter Kinsman
 
P

Peter Kinsman

Graham
Thank you VERY MUCH for that.
I have used pop up forms in the past, but had forgotten that a WindowMode of
acDialog would create similar to the familiar world of procedural
programming.
I have also looked at a similar suggestion from Albert D Kallal.
Peter

Graham Mandeno said:
Hi Peter

So, do you want a general-purpose form to look up a product code, and then
use the product code selected to either find a matching record in the
current form (as in your product maintenance form), or to insert into a
field in a new record (as in an order entry form)?

I suggest you write a jacket function which opens your lookup form in
dialog mode. After the user has found and selected a product, the lookup
form makes itself invisible and the function returns the selected product
code.

Something like this:

Public Function LookupProduct() as Variant
Const FormName = "frmLookupProduct"
If SysCmd(acSysCmdGetObjectState, acForm, FormName) Then
' form is already open - set no selection and show the form
With Forms(FormName)
.SelectedProduct = Null
.Visible = True
End With
Else
DoCmd.OpenForm FormName, WindowMode:=acDialog
End If
' wait till the form is made invisible (or perchance closed)
If SysCmd(acSysCmdGetObjectState, acForm, FormName) Then
LookupProduct = Forms(FormName).SelectedProduct
End If
End Function

In your lookup form, declare a module-level public variable:

Public SelectedProduct as Variant

and when the user selects a product:

SelectedProduct = <the selected product code>
Me.Visible = False

You can then call your function whenever you want to look up a product
code, no matter what the context.

Note that, because you are just hiding the form between lookups and not
closing it, each successive lookup will return to the form as it was for
the last search, with the same selection criteria etc. I imagine this
would be more desirable than closing the form between lookups and
retorting each one form scratch. If not, you can just close the form
after retrieving the value.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand



Peter Kinsman said:
Graham

A typical application handles fish in a cold store. Whether the main
form handles Receipts, Sales or Transfers, the user needs to be able to
select the Product Code, but the information to be pasted into the main
form will differ from one activity to the next.
I hope that clarifies what I am trying to achieve.

Peter

Graham Mandeno said:
Hi Peter

I'm sorry, I might be a bit dense, but I have no idea from your message
what it is you are trying to achieve.

Can you please give some more details about how you want to invoke the
search, what you want to search for, how you want the outcome of the
search presented, and what you mean by "pasting the result of the search
into the original form".

I suspect this may simply be a case of constructing and applying a
filter.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

I will start by warning that I come from a world of procedural
programming, where problems like this would not occur.

In several applications, it is necessary to search for a Product Code,
which is as difficult as you want to make it - I have recently found
that the Text property of a TextBox in conjunction with its KeyUp event
can be most useful in restricting the records presented in a search.
The problem I have is in pasting the result of the search into the
original form, when, within the same database, several forms can call
the same search form. I have tried to do it using the OpenArgs
property of the search form, but this means that the coding behind the
search form has to be altered each time a new form makes use of it

Does anyone have any suggestions please?

Many thanks

Peter Kinsman
 

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