Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing variables from a form ( VB6 SP5)

    Are public variables the only way to pass events to and from a form?

    I have some variables to pass, some to return, and need to detect if a cancel button was pressed.

    If it was a function i'd pass the variables over so the ones passed over are updated. Also, I was thinking along the lines of an events cancel variable i.e. Form_Unload(Cancel As Integer).
    Currently i'm doing it all with public variables.

    I was just wondering if there was a neater way of doing it. My method seems to be a bit messy and uses alot of public variables for such a simple thing.

    Regards,

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Passing variables from a form ( VB6 SP5)

    You can use WithEvents to capture specific events from another class object by creating an instance of the other class in your current form. I don't have any VB examples lying around but I do it all the time from both Access and VB.Net. <img src=/S/hmmn.gif border=0 alt=hmmn width=15 height=15> In fact, doing a Lounge search on WithEvents turned up <post:=376,743>post 376,743</post:> ... <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    Charlotte

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables from a form ( VB6 SP5)

    <img src=/S/grin.gif border=0 alt=grin width=15 height=15> I didn't think of WithEvents when I was searching for an answer!

    I think I used the wrong terminology in my post - I was rushing it as I was trying to finish something else before I left work.
    Quote me "Are public variables the only way to pass events to and from a form?"
    By "to pass events" I didnt strictly mean VB events. I meant events as in everything that happened on that form, inlcuding variables that were updated.

    However, I think your suggestion will fix my problem - and I still haven't had a chance to learn to use Classes properly. It seems they fix quite alot so I'll ask my boss for some time to learn them, maybe he'll fit it in between projects! Thanks Charlotte.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Passing variables from a form ( VB6 SP5)

    You can also use RaiseEvent from the called form to send up a notice that something has occurred. Then any object that sinks that event by having a handler for it will respond immediately when the event is raised. This is a key part of loosely coupled programming. The object raising the event need know nothing about the object or objects sinking it. It just announces to anyone listening that a particular event has happened. It's up to the objects that sink that event to react.
    Charlotte

  5. #5
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables from a form ( VB6 SP5)

    Ah, that might be handy.

    I have refined my code a little since my first post. Instead of declaring public variables that were always available, I declared private variables, but used Objects and passed the actual control to the pop up form, so when the popup form made a change to the object, the original object was also changed, meaning I didnt have to pass the data back.

    My original method involved writing data to the public variables, opening a form, copying the data from public vars to controls, user edits the form, data is copied back to public vars, close form, data is copied from public vars back to original controls! I did say it was messy!

    Thanks for the tip about RaiseEvent, that might be very handy as currently our programming has a tendancy to run processes regardless of if a user changed anything. I might be able to use RaiseEvent to only run processes if something was changed.

    Regards,

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables from a form ( VB6 SP5)

    Take a look at the article Passing values b/w modules & userforms. In addition to the methods discussed there, I have used the form's Tag property to pass a concatenated string of variables to/ from a form. With VB5, I don't think you have the convenience of split & join functions, but it's pretty easy to "roll your own". e.g. VB Helper: HowTo: Write Split and Join functions for VB 5. Handy to have these as general utility functions anyway - and they might be perfect candidates as methods for your own class! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

  7. #7
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing variables from a form ( VB6 SP5)

    Thanks Alan.

    I am going to have a practice with the information from the link you posted as that looks like it could be very useful.
    Due to the urgency of the task in question I ended up using the Tag method where I could (Tags were already used in places and I didnt want to combine and split strings in this case).

    However, where I didnt use Tags I altered my method of opening a form. I usually go straight for the form.show so I didnt think of doing this when I first posted....
    This is an example of what I used:
    SubForm.Load
    SubForm.Variable1 = form1.textbox1
    'I had declared public variables (variable1) on the form (SubForm) rather than global variables in a module.
    SubForm.Show

    Then on the unload event I passed back the data to the original control before SubForm was unloaded.
    That avoids declaring a load of global public variables in a module, which was my main concern.

Posting Permissions

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