Link to home
Start Free TrialLog in
Avatar of paulinebelford
paulinebelford

asked on

Working with multiple forms

I am trying to create a program with 3 separate forms: a menu, a form displaying contact details,which are stored in arrays,  and a 3r form for entering new contacts details into the array. But I need to access code from the separate forms and am unable to do this.
Avatar of Vbmaster
Vbmaster

Procedures and functions in forms are by default Private but you can change it to Public (just add the keyword Public in front of the Sub/Function/Property keyword in declaration - or of course replace if there is already a Private keyword thee). Then you can access a sub from anywhere in your project using code like..

  Call nameoftheform.subname
Either that or add a module and place all your PUBLIC routines in there. Structurally this is more correct.

You can pass a form name to the routines so the same routine can be used to on controls on different forms.

PUBLIC SUB MySub(Frm as Form)
frm.caption = "Hello!"
....
END SUB

Within the calling form:

CALL MySub( Me )


You can also use Objects to pass a pointer to a control:

PUBLIC SUB MySub(Obj as Object)
obj.text = "Hello!"
....
END SUB

Within the calling form:

CALL MySub( Me.Text1 )



M
Avatar of paulinebelford

ASKER

My main problem is not calling the functions. The problem is that I need to use data, which is input into the forms at runtime, in my functions and the functions require data from both of the forms. Is this possible?
How are you calling the data? I'm not quite sure what you mean, but if I'm right, you should be using:

public sub example()

' the contacts form is called frm1
'     one of the fields here is fld11
'     another is fld12
'
' the menu is frm2
'     one of the fields here is 21
'     another is fld22'

' the 3r form is frm3
'     one of the fields here is 31
'     another is fld32'

' depending on what you are doing it
' should be something like:

frm1.frm11 = frm2.fld22

end sub

What I'm trying to demonstrate is the

formname.object

method. This is the first thing I learned, so I'm sure you know - but it's just in case - after all, it could be your problem.

T
Pauline:

Here's one way to make your data available, there are a dozen more.  You could use a public UDT but let's use an array instead since you're already using arrays.  Put an array in a module, say Public sNewContactDetails(13) As String.  In the new contact details form, your textboxes should be an array, in which case, the textbox change event should have this  code: sNewContactDetails(Index)=txtDetails(Index).  Now that value is available everywhere in your app.  When you close the details form, clear it, or whatever, Erase the sNewContactDetails array.

Jim
ASKER CERTIFIED SOLUTION
Avatar of Gunsen
Gunsen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
o well, I suppose I'd better go straight for the answer when I think I've got it!

Well done Gunsen, I wasn't sure anyway and it's answered some of my problems too