Results 1 to 7 of 7
  1. #1
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287

    Referencing a variable in a parent form from the subform

    Hey guys,



    I have a parent form [F_Selections] with a combobox. The after update on that combobox generates a stored variable "MaxSelected." I am trying to reference this variable in the subform [F_Sel_Sub]. Everything I google pulls up openargs, but that doesn't seem right.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Where are you trying to reference the variable? If in VBA post code.

    Cannot reference Access variables in textboxes. They can reference TempVars - I've never used them.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Just in code. The variable "MaxSelected" gets compared to "CurrentSelected" on the after update of a button on the subform. Once CurrentSelected > MaxSelected, I have an error message pop up.

    I'm new to using Public subs. I tried making the parent form's after event sub public, but I still couldn't call the variable.

    This is the code that creates "MaxSelected":
    Code:
    Public Sub SlspSelect_AfterUpdate()
    Dim MaxSelected As Integer
    
    
    Me.NumSel.Requery
    Me.F_DR_Select_Sub.Requery
    Me.F_DR_Select_Sub.SetFocus
    
    
    If IsNull(DLookup("AdjNumber", "T_SelectionNumAdj", "[Name] = '" & _
            Forms!F_GalleySelections![SlspSelect] & "'")) Then
        MaxSelected = 10
        Else
        MaxSelected = DLookup("AdjNumber", "T_SelectionNumAdj", "[Name] = '" & _
            Forms!F_GalleySelections![SlspSelect] & "'")
    End If
    
    
    
    
    End Sub
    Here is where I am trying to use it:

    Code:
    Private Sub Frame2_AfterUpdate()
    
    
    Dim CurSelected As Integer
    Dim msgTooMany As String
    
    
    'Check to see how many names have been submitted.
    msgTooMany = "You have selected the max amount. Add less people to the list " & _
        "or send a mailer to less people."
    
    
    CurSelected = DCount("Name", "T_DreamRooms", "[AddToList] = '1' AND [NoMail] = '1' " & _
        "AND [Salesperson] = '" & Forms!F_GalleySelections![SlspSelect] & "'")
    
    
    If CurSelected >= MaxSelected Then
        MsgBox msgTooMany, vbOKOnly
        Me.Frame2 = 2
    Else
        Select Case Me.Frame2
            Case 1
                [NextMailed] = Forms!F_GalleySelections!NextAdDate
            Case 2
                [NextMailed] = Null
        End Select
        DoCmd.RunCommand acCmdSaveRecord
        Forms!F_GalleySelections.NumSel.Requery
    End If
    End Sub

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    If you want to refer to a variable between procedures of the same object code module, the variable must be declared in header section above all the procedures of that module:

    Option Compare Database
    Option Explicit
    Dim MaxSelected As Integer

    Public Sub SlspSelect_AfterUpdate()
    ...

    If you want a variable available to any modules then the variable must be declared in a general module header.

    Or pass a value from one form to another by:

    1. using OpenArgs

    2. populate a textbox on one form and another form can reference that textbox.

    3. TempVars may be an option - as I understand, they can be declared anywhere and then they are available anywhere
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Further to June7's comment -

    To reference a variable in a parent form from a subform, it must be declared as Public in the main form : Public MaxSelected As Integer

    and the reference in the subform code is Me.parent.MaxSelected .

    John

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Wow, never thought about using Parent.

    Again, the variable is declared in the module header.

    Be aware that if code execution is interrupted, all variables are cleared (lose their value). This is not the case with TempVars.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Didn't even know that you could declare variables in the header....

    Learn new things everyday.

    Worked great with the .parent.

    Thanks guys!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Referencing form fields with a string variable
    By ts_hunter in forum Programming
    Replies: 3
    Last Post: 02-13-2014, 01:06 PM
  2. Replies: 2
    Last Post: 01-18-2014, 11:51 AM
  3. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  4. Replies: 4
    Last Post: 05-16-2011, 04:58 PM
  5. Referencing a form in a subform
    By 161 in forum Forms
    Replies: 3
    Last Post: 01-24-2011, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums