CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2007
    Posts
    33

    Passing Values between forms

    Hi Guys,

    Having trouble passing a value from one form to another. The first form has 2 check boxes and an open commnad for a order form. The user clicks if the order is paid by check or Credit Card, then they open the order form and fill it out. In my orders table i have two fields created for CC and check. Basically depending on what the user clicked on the first form, when they save the record in the second form it will store all the values and the value if the user checked check or credit card.

    Ive been playing around with the openargs command but i cannot seem to get this to work.

    Any help would be appreciated.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Passing Values between forms

    Extract the information into a class (forms should NOT hold state. Pass the instance of the class from one form to the other...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Passing Values between forms

    OR... access Form1 directly from Form2:

    if Form1.optCheck.Value then
    msgbox "Check"
    else
    msgbox "Card"
    end if

    OR... pass the value into the Tag property of Form2...

    OR... create a public property on either Form1 or Form2...

    ETC...
    Busy

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Passing Values between forms

    Quote Originally Posted by Thread1
    OR... access Form1 directly from Form2:

    if Form1.optCheck.Value then
    msgbox "Check"
    else
    msgbox "Card"
    end if

    OR... pass the value into the Tag property of Form2...

    OR... create a public property on either Form1 or Form2...

    ETC...
    While any of these approaches WILL work, I would not recommend ANY of them (although they are common). The reasons are many, but the following are the highlights:

    1) Having state (and/or logic) in Forms dramatically reduces the testablity of the code.

    2) Tight coupling between the forms reduces re-use and create additional dependancies.

    3) You can quickly end up with circular references with can kill scalability.

    Thee are a bunch more, but these sould be enough to disuade any developer who is doing anything other than "playing with throw away code"

    4) Throw-Away code has a very bad habit of lasting a long long long time.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    May 2006
    Location
    London (UK)
    Posts
    138

    Re: Passing Values between forms

    Impressive...



    Quote Originally Posted by TheCPUWizard
    While any of these approaches WILL work, I would not recommend ANY of them (although they are common). The reasons are many, but the following are the highlights:

    1) Having state (and/or logic) in Forms dramatically reduces the testablity of the code.

    2) Tight coupling between the forms reduces re-use and create additional dependancies.

    3) You can quickly end up with circular references with can kill scalability.

    Thee are a bunch more, but these sould be enough to disuade any developer who is doing anything other than "playing with throw away code"

    4) Throw-Away code has a very bad habit of lasting a long long long time.

    Hope that Helps a Bit, but Sorry if it doesn't.
    Dont forget to Rate the Post...


    Thanks & Regards
    Manu Raj

    Nothings is Impossible in this World even Impossible says "I M possible"...

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Passing Values between forms

    Quote Originally Posted by TheCPUWizard
    While any of these approaches WILL work, I would not recommend ANY of them (although they are common). The reasons are many, but the following are the highlights:

    1) Having state (and/or logic) in Forms dramatically reduces the testablity of the code.

    2) Tight coupling between the forms reduces re-use and create additional dependancies.

    3) You can quickly end up with circular references with can kill scalability.

    Thee are a bunch more, but these sould be enough to disuade any developer who is doing anything other than "playing with throw away code"

    4) Throw-Away code has a very bad habit of lasting a long long long time.

    that is actually a good and general approach in software design. hope it will convince the OP to change his approach when all he wanted is passing values between forms . quite overkill but nice suggestions though
    Busy

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Passing Values between forms

    4) Throw-Away code has a very bad habit of lasting a long long long time.
    The reason being, it works so well, is easy to understand and consists only of three lines of code.

    I fully agree with the CPUWizard, about bad programming habits, but I dont consider it too bad, to access elements or variables of a form directly, as long as it is within a "closed" project of smaller size, where reuse of code is not a subject.
    I would possibly even make these values properties of the Form and writing Property Get Procedures to access them.

  8. #8
    Join Date
    Oct 2007
    Posts
    33

    Re: Passing Values between forms

    Hi Guys,

    Thanks for all your help. I will try your suggestions tonight.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Passing Values between forms

    Then, there's always Global Variables
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured