I have a separate form for searching and number of other transaction forms. So, when ever I need I simply called search form by passing relevant sql statement and filter data in to search form. This is working absolutely fine. But I have a problem from passing selected searched value back to original form. Because original form name is not fixed. I have different transaction forms calling in to one search form. I need to return selected values in to calling form and calling text box without activing any event in originating form after search. Any help would be highly appreciate.

Thanks a lot for everyone. I applied your solutions and it is fine now.

Try creating public variable and storing value whenever required. You can
use these values whenever required by referring each of them as
formname.variable. Hope this helps you.

//In Form1 code:-

string username =which value you want to pass;
form2 = new Form2(textBox1 .Text );
form2.Show();

//In Form2 code:-

//Take one textbox in form2,under write this code.

Form2 form2;
public Form2(string username)
{
InitializeComponent();
textBox1.Text = username;
}

Before closing the search form to keep the name of a new form in any
variable or array element.

In your Search Form store the answer in a public variable.

Public MyAnswer As String 'or whatever.

Also declare the search criteria as public variable(s) in the Search Form.

Public SearchCriteria as String 'or whatever

Give the Ok button an event handler that performs the search.

In the original form, create the dialog as needed.

Dim MySearchForm as SearchForm

MySearchForm = new SearchForm()

MySearchForm.SearchCriteria = “Whatever”

If MySearchForm.ShowDialog() <> DialogResult.OK Then

Return

End If

TextBox1.Text = SearchForm.MyAnswer

SearchForm = Nothing

The dialog approach is simple and straightforward and (IMO) the preferred
way to get information back to the caller from the callee.

Jon

This message was composed of recycled electrons.

Hi

The below way can be a proposed and easy solution if you don’t want to go in
a dialog approach for the search form:-

  1. Make a base form which will have one global property to accept the search
    return value
  2. Inherit the base form to all your transaction forms and but them upon
    that, by this way the base form property is visible also.
  3. In search form create one base form property and while calling from the
    transaction form assign the Base Form property of the search form with the
    concerned form.
  4. When the search form closes, assign the Base Form’s the # 1 property in
    your search form.

Hope this works
Regards
Pradeep

Gardhi,

Let’s start by looking at how you can write code in the search form that can set a value in some other form.

In the Search form, you want to store a result into something within the calling form. That “something” should be named the same in each form that can call the Search form; it can be a control (in which case you will set the value of the control), or it can be a variable (in which case it must be public), or it can be a procedure.

Regardless of which type of target you use, your code must identify the form first. To do that, you use the application’s Forms collection, and you specify the form (as a member of the collection) by name. For example, if the Search form is called from a form named fPurchase, you would use a statement like:

Forms(“fPurchase”).txtSearchResult = vSearchResult

to transfer a result from the variable vSearchResult to the text box txtSearchResult

In a language like Visual Basic for Applications (VBA), you can use a string variable anywhere that you can use a string literal. In other words, if you have set a variable formName to the name of the calling form, you can use the statement:

Forms(formName).txtSearchResult = vSearchResult

to set the text box to the result on the form whose name is in the formName variable.

The next question is, “How can I pass the name of the calling form to the search form?” I strongly recommend that you use the OpenArgs argument.

When you use the DoCmd.OpenForm method to open a form (in this case, the Search form), the last argument is the OpenArgs parameter. You can pass anything you want to the called form as a string. In the called form, you can use the form’s OpenArgs property to retrieve the passed value.

For example, you could have a statement in the code on the Search form like:

formName = Me.OpenArgs

and then use formName to refer to the calling form. Or, if your only use of the OpenArgs property is to pass the calling form’s name, you could even use the statement:

Forms(Me.OpenArgs).txtSearchResult = vSearchResult

Michael S. Meyers-Jouan

Thanks for the reply. But I need more information for this problem.

I declared public variable in main module and assigned searched value in to variable and try to pass in to calling form. But the problem is to get the value in to caller, it should have to run procedure in caller form.

If I explain it deeply, when user want to select employee name for employee leave record entry, simply open the search window through the leave entry form.
My search form contains grid control and text box user can quickly type few letters and filter the employee names starting from those letters. Then user can go through the grid control and hit the enter key on required record.

What I need is, user selected record should appear in the text box in calling form same time and close the search form.

Thanks for the reply. Could you please explain it little more detail. In other way I need to refer the calling form and text box name from particular form.

Hey,
Why don’t you use a global class and global variables…you can store the selected searched values into that global variables.
Sent on my BlackBerry? from Vodafone

Why do not you keep the names of open forms in a special array? When you
close the form the corresponding array element to replace an empty string?

If the calling form calls the search form as a dialog, it can read the
results from the search form before disposing of it. Otherwise, make the
calling form (not the form’s name) one of parameters passed to the callee.
The callee then has a direct reference to the caller and can give the
results back to it. However, I strongly recommend the Dialog approach.

Jon

This message was composed of recycled electrons.