Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005
    Posts
    102

    Unanswered: Creating a Code Group!

    Hi guys,

    I have 50 forms in a database, most of them runs through VB Codes... Some of the Subs (procedure) have the same codes. For example,

    Dim Msg, Style, Title, Help, Response, MyString
    Msg = "Are you sure you want to add this record?"
    Style = vbYesNo + vbQuestion + vbDefaultButton2
    Title = "Add Record?"

    The code stated above is associated with many command buttons from different forms. I want to declare this as a function rather than a Sub and call it from anywhere I want. How do I do this? Do I declare it as a private function or public? And how do I call it?

    Thanks in advance...

  2. #2
    Join Date
    Dec 2004
    Location
    Tennessee
    Posts
    31

    Code Grouping-User Modules

    Hey you want to create this a Public Function so that your whole app can access it.

    Code:
    Public Function myModule()
    Dim Msg, Style, Title, Help, Response, MyString
    Msg = "Are you sure you want to add this record?"
    Style = vbYesNo + vbQuestion + vbDefaultButton2
    Title = "Add Record?"
    End Function
    Then on your command button's click event, you should just be able to call it like this:

    Code:
    Private Sub cmdButton1_Click()
      myModule()
    End Sub
    This should work, let me know if there are any problems with it.


    Gene~
    The older I get, the more new stuff I tend to forget, the stuff I don't use any more...now that I remember!

    Mail Me

  3. #3
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    All you did there was declare a bunch of strings and let them sit...

    I'm not sure how much time you're going to save by placing a straight-up msgbox call in a public function. To answer the question:

    Code:
    Public Function CustomMsgBox () As Boolean
    If MsgBox "Are you sure you want to add this record?", "Add Record?", vbYesNo = vbYes Then
    CustomMsgBox = True
    Else
    CustomMsgBox = False
    End If
    You'll have to supply more detail about what this messagebox is controlling and why you want to put it in a public function for me to give any better code.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

Posting Permissions

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