Results 1 to 7 of 7

Thread: ??? on proper use of Modules...

  1. #1
    Guest

    Question

    Hey all!

    A lot of times answers in the posts here speak of using modules. Especially if you are using certain tasks in different parts of your program.

    Now I understand how to Call modules from within your program, to perform repeated tasks throughout your project such as Error Handling and such.

    But my question is this... When I first began to learn VB I read about using 'specific' names for textboxes, etc. Like txtNetSales, txtGrossSales, txtGrossprofit and txtNetProfit.

    Now when you utilize a Module to perform a Error Check for a certain function like an IsNumeric. Do you need to have all your names declared the same to use the module in different parts of your app???

    And what if you want to use the ErrorCheck IsNumeric function for another part of your app which has txtNumberUnitsSold and txtNumberUnitsReturned???

    I'm wanting to begin to use modules and other techniques as my experience grows. Any advice or insights would be greatly appreciated.

    Thanks!

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    To reference an object such as txtNetSales on Form1 from a module, you need to include the name of the Form, so you would say for example

    If Val(Form1.txtNetSales) > 1000000 Then
    MsgBox "I had a good day"
    End If

  3. #3
    Guest
    It sounds as though you are confusing modules with functions/subroutines.

    A module holds one or more functions or subroutines. In at least one project I have going, I have 6 modules:

    String_Functions
    List_Box_Functions
    Date_Stuff
    (0) Run_all_this_stuff
    Testing_code
    Doesn't_really_work_right

    After I finish the projects, these modules will have documentation, proper names, and be ready for use in any future project.

    Within MODULES:

    A FUNCTION returns a result, but does not change the state of the program. That is, it will you can run it, and the only thing changed when it comes back is the variable that you save the result: xxx=Whatever(passedargs)

    A SUBROUTINE will change the state of the program by changing captions, resetting defaults, .....

    call Original_Defaults(passedargs)

    in response to clicking a button marked "Reset for Start"

    There have been/will be several threads on the "proper" (read: MY WAY) useage of the two constructs. You can believe whichever person makes the most sense, but be consistent. Consistently wrong is better than sometimes right.

    Good Luck
    DerFarm

  4. #4
    Guest

    So MartinLiss...

    To reference an object such as txtNetSales on Form1 from a module, you need to include the name of the Form, so you would say for example

    If Val(Form1.txtNetSales) > 1000000 Then
    MsgBox "I had a good day"
    End If
    Then if I were to use the same Module to check another textbox on another form. I would need to change Val(Form1.txtNetSales) >1000000 to Val(<Form2.txtNetProfits), wouldn't I???

    Guess I'm confused... When you call your Module to come in and perform an Error Check... The Module is already in place with all of the code ready to perform when called upon. How do you use the same Module for performing an IsNumeric check for example, with differing names???

    Think I'll search for more tutorials!!!

    Thanks for the replies though!




  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    vbMarketer: Since you are just beginning, I think I need to comment on DerFarm's response. He apparently has some strict self-imposed rules about what can and can not be done with Subroutines and Functions. The point I want you to understand is that those rules are his/her self-imposed rules and there is no reason why you can't have a Function modify the state of the program (in other words change a value). In my code it is very common for a given function to modify several program values. I use a function in that case rather than a subroutine because I want to be able to return (usually) a Boolean indicating if the updates were successful.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here's an example using a subroutine

    Public Sub CheckForGoodDay(MyTextBox as TextBox)

    If Val(MyTextBox) > 1000000 Then
    MsgBox "I had a good day"
    End If

    End Sub

    Usage:

    CheckForGoodDay Form1.txtNetSales
    CheckForGoodDay Form2.txtOtherTextBox

    Here's one using a Function

    Public Function CheckForGoodDay(MyTextBox as TextBox) As Boolean

    If Val(MyTextBox) > 1000000 Then
    CheckForGoodDday = True
    Else
    CheckForGoodday = False
    End If

    End Function


    Usage:

    If CheckForGoodDay(Form1.txtNetSales) then
    MsgBox "I had a good day"
    End If



  7. #7
    Guest
    Just got back from Ontario.....Martin is right, the restrictions are self-imposed. They are imposed because they suit my style of programming and debugging.

    I think they work because they make it easier to spot logical errors....my wife says I'm just being anal-retentive.

    The bottom line is what works for you. With a few exceptions, all the "rules" are somewhat cosmetic.


    DerFarm

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