Results 1 to 11 of 11
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    Reference Variable in Sub Form?

    A2k (9.0.3821) SR-1

    How do I reference a main form module variable mstrTableName in a sub form?

    =====================
    Option Compare Database
    Option Explicit

    Private mstrTableName As String
    ========================

    Thanks, John Graves

  2. #2
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    I think the only way you can reference it is to make it Public.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  3. #3
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    No you cannot even make it a public, you must make it a normal control (text box) on the main form (invisible if you like) and then you can access it.
    Cheers,
    Pat

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

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    If you declare a public variable in a form module, it becomes a public property of the form and can be referenced from other modules as long as the form is open.
    Charlotte

  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    Hi Charlotte,
    I tried to reference a main form public variable from a subform and got the following message:
    MSAccess can't find the field "mstrTableName" referred to in your expression.
    I cannot understand this based upon what you have said.
    Cheers,
    Pat

  6. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Sint Niklaas, Belgium
    Posts
    2,778
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    You can only declare a variable as public in a module. Not in a Form Module.
    According the help file :
    <hr>Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.

    Caution The Public statement can't be used in a class module to declare a fixed-length string variable.
    <hr>
    Francois

  7. #7
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    You are right, of course. I just misread the post, just seeing "module" and not "form module".
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  8. #8
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    I had no problem referencing a module-level Public variable declared in a form module from a subform. As example, in main form declared Public mstrLNAME As String. From subform, referenced this variable as follows:
    <pre>Private Sub Form_Current()
    Dim strLNAME As String
    strLNAME = Forms![MAIN_frm].mstrLNAME
    Me.txtMainLNAME = strLNAME
    (or)
    Me.txtMainLNAME = Forms![MAIN_frm].mstrModLNAME
    End Sub </pre>

    When running main form the subform control displays the value of the main form variable. Had no problem in assigning main form variable either to a variable in declared in subform, or directly to a subform control. This worked with string and integer, did not try with fixed-length string. As Charlotte noted a public variable acts as a public property of the module which you can reference using the dot operator.
    HTH

  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    Right on

    Thanks All, Public is the key.

    This also works

    Thanks to Charlotte & John Viescas the following works:

    Main Form has this:
    =====================
    Option Compare Database
    Option Explicit

    Public mstrFormName As String
    ========================

    Sub Form needs this:

    Dim strFormName As String
    strFormName = Form_frm_Search_BP_MainForm.mstrFormName

    Thanks, John

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

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    Actually, you would be better off referencing it as a property of the parent form, assuming that the subform in question is on frm_Search_BP_mainform. Using Parent references the same form, but it specifically references that particular *instance* of the parent form, plus the syntax is easier. You don't have to refer to the class/form module directly, only to the property of the class. Try this:

    strFormName = Parent.mstrFormName
    Charlotte

  11. #11
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference Variable in Sub Form? (A2k (9.0.3821) SR-1)

    Thanks Charlotte

    I like that better.

    John

Posting Permissions

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