function to enter data and return a value

  • 15 years ago

    Just trying to keep my code clean and compartmentalised

    What is the proper way to create a function that opens up a form or dialog box, user enters in various data which gets calculated and a  value is returned.

    ie

    will be called like this:

    myvalue =  Builder()

    this will open up my formula builder form, where users enter data,  text1.text on the form gets returned.

    Hope that makes sense, (suffering from lack of sleep) Sleep [|-)]

    Thanks

  • 15 years ago
    The general systax is

    Public/Private Function fn_name(Byval/Byref as datatype,....) as return_type
    ...
    ...
    'Do the process here,
    ...

    fn_name = return_value
    'return the value by assigning the return value to the function name as above

    End Function



  • 15 years ago

    It also depends on the scope of the function, grarun already told you the general syntax. If you need function in just your form then declare it in your form, if you need it in your project then make it in module. If you need it in several projects then you can also go for classes… it depends on how you’re going to use it…

  • 15 years ago

    Sorry, I explained it wrong.

    I am creating a  form (kind of like the builder in MS Access), where there is a textbox, a treeview control and buttons for plus, minus, mutiply etc. The textbox1 text is created from these inputs

     

    When the user clicks ok, the form returns the text in textbox1, if the user clicks cancel or close button, a string of zero length is returned ie:""

    I wanted a clean way to call this like

    strFormula = Builder()

    so it opens up the builder form, data is entered and textbox1.text is returned.

    how would i code my function?

    Function Builder()

    frmBuilder.show

    'add code here so that after frmbuilder form is closed, the function gets the text in textbox1, if OK was clicked

    'add code for:   if cancelbutton was clicked or form was closed with X then Builder = ""

    Thanks and sorry about the miscommunication

     

  • 15 years ago

    Use Model Level Dialog Form

    ---------- Code for Builder Form ------------

    Public Enum FormulaBuilderResults

        FormulaBuilder_Cancel

        FormulaBuilder_OK

    End Enum

    Private m_Response as FormulaBuilderResults

    Private m_strFormula as String

    Public Property Get Response() as FormulaBuilderResults

        Response = m_Response

    End Property

    Public Property Get Formula() as String

        Formula = m_strFormula

    End Property

    Sub Form_Load()

        Response = FormulaBuilder_Cancel

    End Sub

    Sub cmdOK_Click()

        m_strFormula = txtFormula.Text

        Response = FormulaBuilder_OK

        Unload Me

    End Sub

    Sub cmdCancel_Click()

        m_strFormula = ""

        Response = FormulaBuilder_Cancel

        Unload me

    End Sub

    ----------- End of Formula Builder Code ---------------

    Now in calling form use as,

    Sub cmdBuildFormula()

        frmFormulaBuilder.Show vbModal

        if frmFormulaBuilder.Response = FormulaBuilder_Cancel Then Exit sub

        debug.Print frmFormulaBuilder.Formula

    End Sub

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne