Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Instances of Same Form, Moving Data to Orig. Form fr. Modal

Status
Not open for further replies.

DariceLR

MIS
Jun 20, 2001
27
US
I have previously posted my question and didn't receive a helpful answer, spending a week looking at other resources...

Situation:
I have a form that inputs records to a database. When I want to put in information found in a supporting table, I use a Modal form to search the table (on SQL Server 7) and move the information found in the search results back to the original form. After querying the supporting table, I move the information from the grid of search results found to the fields the user inputs their search requirements to and when the Modal form is closed, it sends the foreign information to the fields it needs to go in.

Problem:
When I close the form, it should take information from the current form and move it back into the original form. As seen in the below code, I get a new instance of the form with the information I wanted in it instead of the instance of the form that originally called the saved within the record I am creating.

Private Sub cmdOk_Click()
'What do I do to pull the informatiion out the data grid?
'The following line of code makes the new instance of the form "frmNewRequest"
'one instance of form
frmNewRequest.txtDeveloperSSN = "102938838"
'two instances of form
'Closes Form... and opens a new request... don't want that...
Me.Hide
'Unload Me
End Sub


Question(s):
How do I reference different instances of the form?
How can I tell which instance is which?
How do I tell the form which instance I want filled in?

(I thought ActiveForm may be of use in the MDI but I must not know how to use it or it didn't like how I used it)

(Thanks in advance!)



(The only response I got:)
jjames (Programmer) Jul 18, 2001
You may want treat your forms like any other object...

In your calling (or parent form) add something like:
Private Sub Command1_Click()
Dim strSSN As String
Dim objNewRequest As MyForm

Set objNewRequest = New MyForm

objNewRequest.Text1 = "12345678" 'Initialize any data
for this instance of
the form

objNewRequest.Show vbModal

'If we've made it here then the OK button has
'been clicked or window was closed in instance of MyForm

'Get data from instance of MyForm

strSSN = objNewRequest.Text1

Set objNewRequest = Nothing 'destroy the instance

End Sub


In your query form, in this case MyForm, add the following:

Private Sub cmdOk_Click()
Me.Hide
End Sub

Of course you need to add a command button named 'cmdOk'


THE PROBLEM IS, that I don't WANT a new version of the form... I want to use the form that called the Modal Form. I want it to find what form opened it to do work and push back data into it. Any solutions? ****************
DariceLR
:-{} :-V
****************
 
I would try creating a public function to handle showing the search form. Pass the "calling" form as a form argument to the function. Then make a public variable in the search form to keep track of which form called the search form:

'in frmSearch (general declarations):
Public CallingForm as Form

cmdOk_Click
CallingForm.Text1.Text = Me.Text1.Text
'do other grid handling here
End Sub


'in bas module:
Public Sub ShowSearchForm(CallingForm as Form)
frmSearch.CallingForm = CallingForm
frmSearch.Show vbModal, CallingForm
End Sub


'in frmMain:

ShowSearchForm Me



I hope this answers your question, or at least gives you another idea to try.

-Mike
 
I have the same problem, and I tried using the above example but I get error 91 (object not set) in the line:

frmSearch.CallingForm = CallingForm

Any Idea's?
 
I had the same problem, so I took DariceLR suggestion "I thought ActiveForm may be of use..."

When you call a modal form from a non-mdi form, you reference that forms objects by using the syntax:
FormName.object.property

I simply replaced FormName with mdiFormName.ActiveForm.
Since vb doesn't know what form will be active, the intellisense does not work.

To reference a Public Property on the MdiChild, I used the following syntax:
mdiFormName.ActiveForm.PropertyName = somevalue

To reference a control, I used this syntax:
mdiFormName.ActiveForm.ControlName.PropertyName=somevalue

 

Here is an example you might want to give a try...

If I understand your problem correctly. As Follows

You have a form that you create multiple times using code similar to...


'in a module add
Public Sub MakeNewForm1()
Dim F As Form
Set F = New Form1
F.Show
End Sub

Then you use a single instance of a form (say form2) multiple times by showing it vbModal,Me'(owner form)


'In form2 add the following code and text1 and command1
Option Explicit
Dim F As Form

Public Sub TellMeWhoCalled(Who As Form)
Set F = Who
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

F.Text1.Text = Me.Text1.Text

End Sub


'in form1 add the following code, text1, command1,command2
Private Sub Command1_Click()
Call MakeNewForm1
End Sub

Private Sub Command2_Click()
Load Form2
Form2.WhoCalled Me
Form2.Show vbModal, Me
End Sub


create a couple of instances of form1 then select one of them and click on command2 then when form2 shows itself change its text property and click on command1. This should change the text of text1 in the calling form and leave the rest of the text1's text in the other forms the same.

I Hope This Helps, Good Luck
 
Use public variables or public properties in the modal form

Say you have a public variable called m_sReturnValue

Sub GetUserInput()
Dim f As new frmSearch

f.Show vbModal

f.m_sReturnValue

unload f

set f = nothing

End Sub

When you click the close button in frmSearch, set its' public variable m_sReturnValue to the user selected value.


Better, maybe to let the search form open itself and return the value:
Caller Form1:

Sub GetUserInput()
Dim f As new frmSearch
dim sValue as string

sValue = f.DisplayAndReturnSel()

set f = nothing

End Sub

In frmSearch

Public Function DisplayAndReturnSel() As String

Me.Show vbModal

DisplayAndReturnSel= text1.Text

Unload me

End Sub [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top