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!

passing Values between forms 3

Status
Not open for further replies.

PeterWallace

Programmer
Apr 28, 2003
210
AU

I have a class Called Human

Consits of Firstname
Lastname
FullName
Title
Age
TypeofHuman

A Human could be a Landlord , A Tenant , A Creditor etc


I have MainForm where data is Entered depending on the Type

This passes Data to another form ( FrmNames )
where The Human Data is Is Entered

This Passes it back to Main Form

FrmMAIN

dim Landlord as Human

Form_load

set Landlord = New Human

' loads form name to get data
FrmNAMES.HandTo Landlord
FrmNAMES.show vbmodal

'----------------------------------

FrmName is to Enter New Data Or Modify existing Names and
Hand them Back to FrmMAIN

Via

FrmMain.ReturnFromName ObjectHuman

My Problem is FrmNames

dim ObjectHuman as human

sub HandTo ( HandoverHuman as Human)

'If I

Set OblectHuman = New Human

'I then have copy HandoverHuman to ObjectHuman
ObjectHuman.Firstname = HandoverHuman.Firstname
...
ObjectHuman.Title = HandoverHuman.Title


AND SAME THING when I Pass Back to FrmMain

How Can I Pass The Class Data between the Forms WITHOUT having to Copy it Each Time ???


Thanks In Advance

Peter





 
You could add a module and declare your class as a global variable or set it as Public in frmMain referencing it from frmNames as frmMain.Human.

Both ways should mean you wouldn't need to use the HandTo procedure.
 
I don't like the method suggested in the faq that chip pointed to. The problem I have is that you must set properties on the form before opening it, then you open it, and finally, check the properties. I have another method that I prefer, and would like to share with you.

On form 2, you would have several text boxes to store FirstName, LastNAme, title, age, and Type. Assume that the text boxes are named, txtFirstName, txtLastName, txtTitle, txtAge and txtType.

On form 2, create a public Subroutine.

Code:
Option Explicit

Private mLastName As String
Private mFirstName As String
Private mTitle As String
Private mAge As String
Private mHumanType As String
Private bCancel As Boolean

Public Sub GetUserSelection(ByRef LastName As String, ByRef FirstName As String, ByRef Title As String, ByRef Age As String, ByRef HumanType As String, ByRef Cancel As Boolean)
  bCancel = True
  txtLastName.Text = LastName
  txtFirstName.Text = FirstName
  txtTitle.Text = Title
  txtAge.Text = Age
  txtType.Text = HumanType

  Call Show(vbModal)

  LastName = mLastName
  FirstName = mFirstName
  Title = mTitle
  Age = mAge
  HumanType = mHumanType

  Cancel = bCancel
End Sub


Private Sub btnCancel_Click()
    
    Unload Me
    
End Sub

Private Sub btnOk_Click()
    
    mLastName = txtLastName.Text
    mFirstName = txtFirstName.Text
    mTitle = txtTitle.Text
    mAge = txtAge.Text
    mHumanType = txtType.Text
    
    bCancel = False
    
    Unload Me
    
End Sub

On the first form, you would have something like this...

Code:
Private Sub Command1_Click()
    
    Dim strLastName As String
    Dim strFirstName As String
    Dim strTitle As String
    Dim strAge As String
    Dim strHumanType As String
    Dim bCancel As Boolean
    
    Call Form2.GetUserSelection(strLastName, strFirstName, strTitle, strAge, strHumanType, bCancel)
    If bCancel Then
        Exit Sub
    End If
    
    [green]' user did not cancel so do something with the data[/green]
    
End Sub
The trick here is to call the Show method of the form from within the form itself. Also, you need to use byref parameters to pass the data back to the calling form. This method only works if you are opening the form modally.

I prefer this method because you cannot forget to set a property on the form, because there are no properties. Since all of the parameters are required, you must supply them.

I find this code easier to maintain. Suppose, for example, you call this form from several places in your code. Then, 6 months from now, you want to add shoe size or eye color. With my method, you need to add parameters to the function. After adding the parameters to the function, you will get compile errors everywhere else. So you will know all the places in the code that need to be updated. With Chip's property method, your code would compile and you would have to find all the places (manually) that you are calling this form.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
thanks to all Three Of you

I will Experiment with the suggestions above

At Moment, prior Testing, I am leaning to simitdevs option
because I have set up the Class to do most of the checking of the input date .. e.g There MUST be a Last name ...

and There are another few Classes That I want to do the same sort of thing with ... Phone , Address and more importantly that the data is validated e.g Rates

and I do not want to have to do that for the variables, Boxes etc on the forms as well...

I realise That Data would be Checked when Handed back to main form and accolcated to the various class ....

The others Do have many attractions as well ... so Will Test before deciding
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top