transfer control name between forms

antonyx

Arsenal Supporter
Local time
Today, 22:42
Joined
Jan 7, 2005
Messages
556
hello..

imagine that FormA and FormB both use FormX while they are open to perform a similar task..

at the moment im using something like this..

FormA & FormB

Code:
Private Sub txtControl_AfterUpdate()
DoCmd.OpenForm "FormX"
End Sub

txtControl will be placed on both FormA and FormB

the purpose of FormX is to search records.. and transfer a value back to one of the two forms.. so at the moment.. when FormX is ready to transfer the value.. i am using this..

Code:
Private Sub btnTransfer_Click()
If CurrentProject.AllForms("FormA").IsLoaded Then
Forms!FormA.txtControl = newID
Forms!FormA.SetFocus
DoCmd.close acForm, "FormX"
ElseIf CurrentProject.AllForms("FormB").IsLoaded Then
Forms!FormB.txtControl = newID
Forms!FormB.SetFocus
DoCmd.close acForm, "FormX"
End If
End Sub

this is fine and works like a treat..

HOWEVER..

i also require txtControl2 on FormA and FormB to be populated in the same way..

so what i am doing at the moment is this.. i create a duplicate form called FormX2 and use this..

Code:
Private Sub btnTransfer_Click()
If CurrentProject.AllForms("FormA").IsLoaded Then
Forms!FormA.txtControl2 = newID
Forms!FormA.SetFocus
DoCmd.close acForm, "FormX2"
ElseIf CurrentProject.AllForms("FormB").IsLoaded Then
Forms!FormB.txtControl2 = newID
Forms!FormB.SetFocus
DoCmd.close acForm, "FormX2"
End If
End Sub

i dont want a duplicate form like this..i want 1 FormX and i want that FormX to know which control was used to open it..

so if you are focused on txtControl1-FormA and the after update of txtControl1 triggers FormX to open.. then the name of that control can be transfered to FormX and placed in an unbound textbox.. lets call it txtFromControl

then i could use something like this..

Code:
Private Sub btnTransfer_Click()
txtFromControl = ThisControl
If CurrentProject.AllForms("FormA").IsLoaded Then
Forms!FormA.ThisControl = newID
Forms!FormA.SetFocus
DoCmd.close acForm, "FormX"
ElseIf CurrentProject.AllForms("FormB").IsLoaded Then
Forms!FormB.ThisControl = newID
Forms!FormB.SetFocus
DoCmd.close acForm, "FormX"
End If
End Sub

this way FormX will know which control has opened it.. and will know which control to transfer its value to when closed..

i hope that is clear.. i wait patiently for your wisdom..
 
I think the better way to do this would be for formX to not care who called it, and leave that responsibilty up to the form that called it. This is how I would approach it:

FormA:
Code:
Private Sub txtControl_AfterUpdate()
DoCmd.OpenForm "FormX", windowmode:=acDialog
If CurrentProject.AllForms("FormX").IsLoaded Then
    me.txtControl = Forms!FormX.newID
    docmd.close acForm, "FormX"
end if
End Sub

FormX:
Code:
Private Sub btnTransfer_Click()
me.visible = false

What we're doing is opening formX as a dialog, code execution is passed to formX until it is closed or hidden. So when you click transfer on formX it hides the form, but does not close it, returning control back to formA. FormA then takes the value from formX and closes formX. This way it is your calling procedure that determines what happens to the value, and formX merely provides the value to you. This way you can use formX as much as you want from wherever you want, with the calling procedure assigning the values.

Hope this helps out! This is how I reuse my Calendar form throughout my database, the form simply holds the date for the calling procedure to retrieve it.
 
yes.. it works perfectly.. thank you
 

Users who are viewing this thread

Back
Top Bottom