Results 1 to 8 of 8
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Forms! syntax

    I'm trying to find why my referenced to the forms collection is failing.
    With the following code: (The two lines of code reside in a general module)
    Code:
     
    MsgBox Forms!frmTMS.OptionLabel8.Caption
    MsgBox Forms!Forms(GSwBrdName).Controls("OptionLabel" & GSwBrdOpt).Caption
    The first line has the "hard coded" form name and one of the form's control names also "hard coded" so as to confirm the caption. The second line is an attempt to referenced the same caption, but using the global variables to stipulate which form and which control. The controls reference is correct, but the forms reference produces the error:


    Click image for larger version. 

Name:	000.jpg 
Views:	21 
Size:	19.8 KB 
ID:	49833
    What should the "Forms(frmName)" syntax be so as to referenced the form?

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Sorry orange, the reference makes no mention as to how to substitute the name of the form with the use of a string variable.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    This - Forms!Forms( ) is 2 attempts to get a member from the same collection.
    To use a variable methinks you'd have to use the latter syntax but as Forms(GSwBrdName)
    None of the examples at the link use that syntax, so not sure if any of them really point to the reason why it fails.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,913
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Yes, I has "bang" on the brain being in a general module outside that of the form itself, so it didn't even occur to me that
    Code:
    Forms(GSwBrdName).Controls("OptionLabel" & GSwBrdOpt).Caption
    using a simple reference to the forms collection was all that was needed.
    Thanks,
    Bill

  7. #7
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    All you need to do to reference your controls is watch your form variable. There are 4 places to look for your stuff:
    1. at the root of your form object
    2. in the .Controls property of your form object
    3. in the .Form property of your form object
    4. in the .Parent property of any item in your .Properties property

    The bang just simply replaces the need to write the name of the control with quotes in a parentheses by looking at the object's items' name properties, which is why you should not use a bang if you're programmatically trying to access an object using its name. Here's an example of all this nonsense:
    Code:
    Sub GettingSubformNameInManyWays()
        Dim theForm As Form
        Set theForm = Forms("MainForm")
        
        '----------------------------------
        ' Items in the root of the form object
        Dim rootItem As Variant
        For Each rootItem In theForm
            Debug.Print rootItem.Name
        Next
        Debug.Print "Root appended: ", theForm!SubForm1.Name
        
        '----------------------------------
        ' Items in the "Controls" property of the form object
        Dim controlsItem As Variant
        For Each controlsItem In theForm.Controls
            Debug.Print controlsItem.Name
        Next
        Debug.Print "Controls property bang: ", theForm.Controls!SubForm1.Name
        Debug.Print "Controls property no bang: ", theForm.Controls("SubForm1").Name
        
        '----------------------------------
        ' Items in the "Form" property of the form object
        Dim formItem As Variant
        For Each formItem In theForm.Form
            Debug.Print formItem.Name
        Next
        Debug.Print "Form property bang: ", theForm.Form!SubForm1.Name
        Debug.Print "Form property no bang: ", theForm.Form("SubForm1").Name
        
        '----------------------------------
        ' Items in the "Properties" property of the form object have
        ' a "Parent" property where you can also get references
        ' here's an example of how from the "Caption" property
        ' but could be any other property there, like "PopUp" or whatever
        Debug.Print "From the parent bangs", theForm.Properties!Caption.Parent!SubForm1.Name
        Debug.Print "From the parent no bangs", theForm.Properties("Caption").Parent("SubForm1").Name
    End Sub
    Last edited by Edgar; 03-05-2023 at 07:49 PM. Reason: Meant name of control with quotes

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Yes, just saw the updates. I should have read more thoroughly.

    Generic:
    ......
    frmName = "MyForm"
    cntrlName = "MyControl"


    cntrlCaption = Forms(frmName).Controls(cntrlName).Caption or
    cntrlCaption = Forms(frmName).Controls("MyControl").Caption
    Last edited by orange; 03-05-2023 at 04:05 PM. Reason: late to updated posts in thread

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

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2022, 10:34 AM
  2. Replies: 18
    Last Post: 02-22-2021, 05:25 PM
  3. Guide for interpreting MSDN / MS documentation syntax-syntax !!
    By pisorsisaac@gmail.com in forum Access
    Replies: 4
    Last Post: 11-18-2017, 07:07 AM
  4. Convert SQL Server Syntax To Access Query Syntax
    By chalupabatman in forum Queries
    Replies: 1
    Last Post: 10-18-2017, 08:53 PM
  5. Replies: 1
    Last Post: 06-01-2012, 02:40 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