Return value from a form


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Return value from a form

  1. #1
    John Kang Guest

    Return value from a form


    Is there a proper technique for returning value from a form? For example,
    I want the user to select something from a list of choices on a form and
    return the value of what the user chose to the calling procedure/form? Do
    I have to resort to using public variables?

  2. #2
    Jim Edgar Guest

    Re: Return value from a form

    John --

    Add the following function to a form:

    ' You can add any arguments to the
    ' Display function that you need.
    Public Function Display() As String
    Me.Show vbModal
    Display = m_sReturnValue
    End Function

    Declare a form level variable called:

    m_sReturnValue

    and set the value of the form level value in your code
    prior to closing the form. Do not reference any control
    on the form after the Me.Show vbModal line of code
    or the form will reload.


    To use the form do the following:

    Dim frm as New MyForm, sReturn As String
    sReturn = frm.Display


    Hope it helps,

    Jim Edgar
    "John Kang" <johnchulkang@hotmail.com> wrote in message
    news:3991daea$1@news.devx.com...
    >
    > Is there a proper technique for returning value from a form? For example,
    > I want the user to select something from a list of choices on a form and
    > return the value of what the user chose to the calling procedure/form? Do
    > I have to resort to using public variables?




  3. #3
    John Kang Guest

    Re: Return value from a form


    Thanks Jim, your solution should work. However, what if I wanted multiple
    return values from a form? How about passing an object and having it returned
    back from a form? Does same technique apply for objects?

    Thanks!

    "Jim Edgar" <djedgar@home.com> wrote:
    >John --
    >
    >Add the following function to a form:
    >
    >' You can add any arguments to the
    >' Display function that you need.
    >Public Function Display() As String
    > Me.Show vbModal
    > Display = m_sReturnValue
    >End Function
    >
    >Declare a form level variable called:
    >
    >m_sReturnValue
    >
    >and set the value of the form level value in your code
    >prior to closing the form. Do not reference any control
    >on the form after the Me.Show vbModal line of code
    >or the form will reload.
    >
    >
    >To use the form do the following:
    >
    >Dim frm as New MyForm, sReturn As String
    >sReturn = frm.Display
    >
    >
    >Hope it helps,
    >
    >Jim Edgar
    >"John Kang" <johnchulkang@hotmail.com> wrote in message
    >news:3991daea$1@news.devx.com...
    >>
    >> Is there a proper technique for returning value from a form? For example,
    >> I want the user to select something from a list of choices on a form and
    >> return the value of what the user chose to the calling procedure/form?

    Do
    >> I have to resort to using public variables?

    >
    >



  4. #4
    Jim Edgar Guest

    Re: Return value from a form

    John --

    Pass some arguments byref, ie..

    Private m_sStringArg As String
    Private m_iIntegerArg As Integer
    Private m_dDateArg As Date
    Private m_lReturnValue As Long

    Public Function Display(arg1 As String, arg2 As Integer, arg3 As Date) As
    Long
    ' Get default values for the form level variables
    m_sStringArg = arg1
    m_iIntegerArg = arg2
    m_dDateArg = arg3
    ' Use your own return values to indicate the success/failure of the
    ' form, ie: 0 = Cancel, -1 = Success, etc...
    m_lReturnValue = 0 ' Set default return value

    ' Show the form. The form level variables will be
    ' set by the user. If the user selects Cancel then
    ' the return value will be 0.
    Me.Show vbModal

    arg1 = m_sStringArg
    arg2 = m_iIntegerArg
    arg3 = m_dDateArg
    Display = m_lReturnValue
    End Function


    Sample use:

    Dim sStringArg As String, iIntegerArg As Integer
    Dim dDateArg As Date, lRet As Long, frm As New MyForm

    lRet = frm.Display(sStringArg, iIntegerArg, dDateArg)
    If lRet = -1 Then ' Success
    ' Use the returned values here
    txtMyString = sStringArg
    txtMyNumber = iIntegerArg
    txtMyDate = dDateArg
    Else ' User cancelled the form
    MsgBox "User cancelled the form."
    End If


    This is a pretty quick example but I think you'll get the idea. You
    can also try passing references to objects if that's what you need.
    Hope it helps.

    Jim Edgar


    "John Kang" <johnchulkang@hotmail.com> wrote in message
    news:3991ea5f$1@news.devx.com...
    >
    > Thanks Jim, your solution should work. However, what if I wanted multiple
    > return values from a form? How about passing an object and having it

    returned
    > back from a form? Does same technique apply for objects?
    >
    > Thanks!
    >
    > "Jim Edgar" <djedgar@home.com> wrote:
    > >John --
    > >
    > >Add the following function to a form:
    > >
    > >' You can add any arguments to the
    > >' Display function that you need.
    > >Public Function Display() As String
    > > Me.Show vbModal
    > > Display = m_sReturnValue
    > >End Function
    > >
    > >Declare a form level variable called:
    > >
    > >m_sReturnValue
    > >
    > >and set the value of the form level value in your code
    > >prior to closing the form. Do not reference any control
    > >on the form after the Me.Show vbModal line of code
    > >or the form will reload.
    > >
    > >
    > >To use the form do the following:
    > >
    > >Dim frm as New MyForm, sReturn As String
    > >sReturn = frm.Display
    > >
    > >
    > >Hope it helps,
    > >
    > >Jim Edgar
    > >"John Kang" <johnchulkang@hotmail.com> wrote in message
    > >news:3991daea$1@news.devx.com...
    > >>
    > >> Is there a proper technique for returning value from a form? For

    example,
    > >> I want the user to select something from a list of choices on a form

    and
    > >> return the value of what the user chose to the calling procedure/form?

    > Do
    > >> I have to resort to using public variables?

    > >
    > >

    >




  5. #5
    Eric Litwin Guest

    Re: Return value from a form


    "John Kang" <johnchulkang@hotmail.com> wrote:
    >
    >Is there a proper technique for returning value from a form? For example,
    >I want the user to select something from a list of choices on a form and
    >return the value of what the user chose to the calling procedure/form?

    Do
    >I have to resort to using public variables?


    You can have multiple Property Gets on the form that return the values of
    private variables.

    For example, Form A has:
    Private mlngSelectedId As Long

    Public Property Get SelectedId() As Long
    ' mlngSelectedId is set in another procedure
    SelectedId = mlngSelectedId
    End Property

    A method in Form B might do the following:
    Dim frm As FormA
    Set frm = New FormA

    frm.Show vbModal
    MsgBox "SelectedId = " & frm.SelectedId
    Unload frm

    You can add as many properties to Form A as you need. Just don't unload Form
    A until you've read all the properties you are interested in.

  6. #6
    Andrew Guest

    Re: Return value from a form


    "Eric Litwin" <eric_litwin@countrywide.com> wrote:
    >
    >You can have multiple Property Gets on the form that return the values of
    >private variables.
    >
    >For example, Form A has:
    >Private mlngSelectedId As Long
    >
    >Public Property Get SelectedId() As Long
    > ' mlngSelectedId is set in another procedure
    > SelectedId = mlngSelectedId
    >End Property
    >
    >A method in Form B might do the following:
    >Dim frm As FormA
    >Set frm = New FormA
    >
    >frm.Show vbModal
    >MsgBox "SelectedId = " & frm.SelectedId
    >Unload frm
    >
    >You can add as many properties to Form A as you need. Just don't unload

    Form
    >A until you've read all the properties you are interested in.



    Eric,
    The idea of using Property Get to pass the variable seems a nicer, more object
    oriented approach. But I was just wondering what the difference is between
    using Property Get to return a private variable or making a public variable
    within the Form to do the same thing. The syntax to get at the variable
    from another Form is the same:

    --Your example:
    dim mlngSelectedId as Long

    Public Property Get SelectedId() As Long
    ' mlngSelectedId is set in another procedure
    SelectedId = mlngSelectedId
    End Property

    --Or Public variable:
    Public SelectedId as Long


    --Usage for either:

    Dim frm As FormA
    Set frm = New FormA

    frm.Show vbModal
    MsgBox "SelectedId = " & frm.SelectedId
    Unload frm



    Just curious.
    Thanks,

    Andrew.




  7. #7
    Jim Pragit Guest

    Re: Return value from a form


    John,

    I asked myself the same question a while back. I tried a couple different
    methods and this is the one I finally settled upon. Here's step by step
    instructions:

    First, you create a property for this form called DialogResult. Here's the
    code to do that:

    Private mvarDialogResult As VbMsgBoxResult 'general declarations

    Public Property Get DialogResult() As VbMsgBoxResult
    DialogResult = mvarDialogResult
    End Property

    The idea behind using a DialogResult property is that you can easily distinguish
    between the user pressing the OK button and the Cancel button (or the Yes
    and No buttons, which ever your form is using).

    Note that I intentionally declare DialogResult as VbMsgBoxResult. This gives
    you a nice IntelliSense popup list when you're coding. Very nice

    Then I default to mvarDialogResult to vbCancel in the Form_Load event (this
    is in case the user closes the form by clicking the X button in the upper
    right hand corner of the form):

    Private Sub Form_Load()
    'default to cancel
    mvarDialogResult = vbCancel
    End Sub

    And then simply set it in the OK and Cancel button's click events:

    Private Sub cmdOK_Click()
    mvarDialogResult = vbOK
    Unload Me
    End Sub

    Private Sub cmdCancel_Click()
    mvarDialogResult = vbCancel
    Unload Me
    End Sub

    To simplify things further, you can create a template form with all this
    code already written for you.

    Now, create a property for every value you want the user to enter. So if
    this were a form where the user you enter their age, you would code this:

    Private mvarAge As Long 'general declarations

    Public Property Get Age As Long
    Age = mvarAge
    End Property

    In the Form_Load event, default mvarAge to zero:

    mvarAge = 0

    In the cmdOK event, set mvarAge to whatever the user typed in:

    mvarAge = CLng(txtAge.Text)

    There, now your dialog form is done. You call it like this:

    Dim lngAge As Long

    frmGetAge.Show vbModal
    If frmGetAge.DialogResult = vbOK Then
    lngAge = frmGetAge.Age
    MsgBox "The user is " & CStr(lngAge) & " years old"
    Else
    MsgBox "The user pressed Cancel"
    End If

    Like I said, I tried a couple different methods and I found this one to be
    the best.

    - Jim

    "John Kang" <johnchulkang@hotmail.com> wrote:
    >
    >Is there a proper technique for returning value from a form? For example,
    >I want the user to select something from a list of choices on a form and
    >return the value of what the user chose to the calling procedure/form?

    Do
    >I have to resort to using public variables?



  8. #8
    mrfelis Guest

    Re: Return value from a form

    Andrew <andybecker@earthlink.net> wrote in message
    news:39920093$1@news.devx.com...
    > Eric,
    > The idea of using Property Get to pass the variable seems a nicer, more

    object
    > oriented approach. But I was just wondering what the difference is

    between
    > using Property Get to return a private variable or making a public

    variable
    > within the Form to do the same thing. The syntax to get at the variable
    > from another Form is the same:
    >


    Andrew,

    A public variable is Read/Write. This allows other programmers in the
    project (or yourself at a later date) to subvert the variable to do
    different things. While this subversion isn't as bad as a Global variable
    in a Standard Module, it is still a bad implementation for a return value. A
    Property Get (with out the related Property Let/Set) is Read Only, and can
    only be modified by code inside the form itself. This encourages using the
    variable for only what you intended it for.

    In a large project, subverted variables tend to cause bugs. The design may
    be a difficult one to locate and impossible to fix.

    Case in point: a project I, and about 4 or 5 other programmers, currently
    work on has a Global variable gbCancel as Boolean. In some places this value
    is set to True and not reset back to False at the end of the operation. This
    causes other features to react as if the user canceled a dialog even when he
    clicks OK.

    The bug is small and has proved itself to be extremely difficult to find.
    The manager doesn't want to waste more resources on finding it. So the bug
    has been there for more than 3 years (as long as I've worked at this
    company.)
    --
    ~~~
    C'Ya,
    mrfelis
    mrfelis@yahoo.NOSPAM.com
    just remove the spam




  9. #9
    Andrew Guest

    Re: Return value from a form


    >A Property Get (with out the related Property Let/Set) is Read Only, and

    can
    >only be modified by code inside the form itself.


    Ah, I see the difference now. I am going to start using Property Gets!

    Thanks mrfelis!
    Andrew.


  10. #10
    John Kang Guest

    Re: Return value from a form


    Thanks Jim & Eric. You guys both supplied same techinique for what I wanted
    so I guess it wins by default. Seriously, I really like this approach a
    lot 'cause it's so logically easy to understand and implement.

    Thanks guys!

    "Jim Pragit" <James.Pragit@BakerNet.com> wrote:
    >
    >John,
    >
    >I asked myself the same question a while back. I tried a couple different
    >methods and this is the one I finally settled upon. Here's step by step
    >instructions:
    >
    >First, you create a property for this form called DialogResult. Here's the
    >code to do that:
    >
    > Private mvarDialogResult As VbMsgBoxResult 'general declarations
    >
    > Public Property Get DialogResult() As VbMsgBoxResult
    > DialogResult = mvarDialogResult
    > End Property
    >
    >The idea behind using a DialogResult property is that you can easily distinguish
    >between the user pressing the OK button and the Cancel button (or the Yes
    >and No buttons, which ever your form is using).
    >
    >Note that I intentionally declare DialogResult as VbMsgBoxResult. This

    gives
    >you a nice IntelliSense popup list when you're coding. Very nice
    >
    >Then I default to mvarDialogResult to vbCancel in the Form_Load event (this
    >is in case the user closes the form by clicking the X button in the upper
    >right hand corner of the form):
    >
    > Private Sub Form_Load()
    > 'default to cancel
    > mvarDialogResult = vbCancel
    > End Sub
    >
    >And then simply set it in the OK and Cancel button's click events:
    >
    > Private Sub cmdOK_Click()
    > mvarDialogResult = vbOK
    > Unload Me
    > End Sub
    >
    > Private Sub cmdCancel_Click()
    > mvarDialogResult = vbCancel
    > Unload Me
    > End Sub
    >
    >To simplify things further, you can create a template form with all this
    >code already written for you.
    >
    >Now, create a property for every value you want the user to enter. So if
    >this were a form where the user you enter their age, you would code this:
    >
    > Private mvarAge As Long 'general declarations
    >
    > Public Property Get Age As Long
    > Age = mvarAge
    > End Property
    >
    >In the Form_Load event, default mvarAge to zero:
    >
    > mvarAge = 0
    >
    >In the cmdOK event, set mvarAge to whatever the user typed in:
    >
    > mvarAge = CLng(txtAge.Text)
    >
    >There, now your dialog form is done. You call it like this:
    >
    > Dim lngAge As Long
    >
    > frmGetAge.Show vbModal
    > If frmGetAge.DialogResult = vbOK Then
    > lngAge = frmGetAge.Age
    > MsgBox "The user is " & CStr(lngAge) & " years old"
    > Else
    > MsgBox "The user pressed Cancel"
    > End If
    >
    >Like I said, I tried a couple different methods and I found this one to

    be
    >the best.
    >
    >- Jim
    >
    >"John Kang" <johnchulkang@hotmail.com> wrote:
    >>
    >>Is there a proper technique for returning value from a form? For example,
    >>I want the user to select something from a list of choices on a form and
    >>return the value of what the user chose to the calling procedure/form?


    >Do
    >>I have to resort to using public variables?

    >



  11. #11
    Jim Edgar Guest

    Re: Return value from a form

    John --

    Be careful with this technique because it may produce some unexpected
    results.

    > >There, now your dialog form is done. You call it like this:
    > >
    > > Dim lngAge As Long
    > >
    > > frmGetAge.Show vbModal
    > > If frmGetAge.DialogResult = vbOK Then
    > > lngAge = frmGetAge.Age
    > > MsgBox "The user is " & CStr(lngAge) & " years old"
    > > Else
    > > MsgBox "The user pressed Cancel"
    > > End If
    > >


    Calling the property gets after unloading the form causes the form to
    remain in memory (although invisible to the user) and the form level
    variables retain whatever value you have given them. Try changing
    the following line in Jim's example and you'll see what I mean:

    Private Sub Form_Load()
    'default to cancel
    mvarDialogResult = vbCancel
    ' Change the following line
    ' mvarAge = 0
    ' to
    txtAge = mvarAge
    End Sub

    You'll see that each time you show the form the mvarAge variable
    retains the value from the previous time the form was displayed
    because the form never fully unloads.

    This technique is fine as long as you remember to initialize all of
    the variables during form_load and you don't mind the form residing
    in memory after you have used it. I used to use this technique myself
    until I got burned by not re-initializing one of the variables and my
    program kept updating the database with a wrong value. That's why
    I went to the style of passing arguments by reference in a public
    'Display' function. I can set default values for the arguments and when
    the form unloads I set it to nothing so it is removed from memory. The
    'Display' function can return an error code or you can use Jim's idea
    of VbMsgBoxResult if you don't need to return any codes. Anyway,
    just be aware of the variable issue when using this style.

    Hope it helps,

    Jim Edgar



  12. #12
    Jim Pragit Guest

    Re: Return value from a form


    Jim,

    The problem you mention isn't really a problem. Simply set the form to nothing
    when you're done with it:

    Set frmAge = Nothing

    Sorry, I just forgot to mention this. BTW, this should be standard practice
    for all forms, not just dialogs.

    - Jim

    "Jim Edgar" <djedgar@home.com> wrote:
    >John --
    >
    >Be careful with this technique because it may produce some unexpected
    >results.
    >
    >> >There, now your dialog form is done. You call it like this:
    >> >
    >> > Dim lngAge As Long
    >> >
    >> > frmGetAge.Show vbModal
    >> > If frmGetAge.DialogResult = vbOK Then
    >> > lngAge = frmGetAge.Age
    >> > MsgBox "The user is " & CStr(lngAge) & " years old"
    >> > Else
    >> > MsgBox "The user pressed Cancel"
    >> > End If
    >> >

    >
    >Calling the property gets after unloading the form causes the form to
    >remain in memory (although invisible to the user) and the form level
    >variables retain whatever value you have given them. Try changing
    >the following line in Jim's example and you'll see what I mean:
    >
    > Private Sub Form_Load()
    > 'default to cancel
    > mvarDialogResult = vbCancel
    > ' Change the following line
    > ' mvarAge = 0
    > ' to
    > txtAge = mvarAge
    > End Sub
    >
    >You'll see that each time you show the form the mvarAge variable
    >retains the value from the previous time the form was displayed
    >because the form never fully unloads.
    >
    >This technique is fine as long as you remember to initialize all of
    >the variables during form_load and you don't mind the form residing
    >in memory after you have used it. I used to use this technique myself
    >until I got burned by not re-initializing one of the variables and my
    >program kept updating the database with a wrong value. That's why
    >I went to the style of passing arguments by reference in a public
    >'Display' function. I can set default values for the arguments and when
    >the form unloads I set it to nothing so it is removed from memory. The
    >'Display' function can return an error code or you can use Jim's idea
    >of VbMsgBoxResult if you don't need to return any codes. Anyway,
    >just be aware of the variable issue when using this style.
    >
    >Hope it helps,
    >
    >Jim Edgar
    >
    >



  13. #13
    Jim Pragit Guest

    Re: Return value from a form


    For clarity's sake, here's the corrected code for calling the dialog form:

    Dim lngAge As Long

    frmGetAge.Show vbModal
    If frmGetAge.DialogResult = vbOK Then
    lngAge = frmGetAge.Age
    MsgBox "The user is " & CStr(lngAge) & " years old"
    Else
    MsgBox "The user pressed Cancel"
    End If
    Set frmGetAge = Nothing

    Everything's the same as my original post except for the last line which
    is new.

    - Jim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center