Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    What am I doing wrong in this reference to the control?



    Code:
    Public Function ResizeControl(FormName As Form)
    Dim frm As String
    frm = FormName.Name
    
    Dim prop As Control
    prop = Forms(frm).Controls(8) 'This tells me the object variable isn't set
    
    
    'Dim prop As Property
    'prop = Forms(frm).Controls(8).Width 'This tells me the object variable isn't set
    
    'Dim prop As Property
    'prop = Forms!FormName.Controls(8) 'This says it cannot find the form "FormName"
    Prop is a placeholder variable name while I just try and get it to work.

    I am trying to shorten the reference to the control.
    I am trying to code my own centered controls using the Width property so I will be setting the width of a lot of controls and want to reference the controls array on the main form by typing something like "Prop(8) = x" or "Prop("cboTest") = x"

    I must be overthinking this or something right? Am I close?

    Thanks in advance

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What is "8" supposed to be?

    What type of control do you want resize width of?

    Here is example of looping through all controls on form:
    Code:
    Dim ctlName As Control
    For Each ctlName In Me.Controls
        If ctlName.ControlType = acTextBox Or ctlName.ControlType = acComboBox Then ctlName.BackStyle = 0
    Next
    This example loops through a specific group of controls on form:
    Code:
    Private Sub optSet_AfterUpdate()
    Dim i As Integer, j As Integer
    For i = 1 To 8
        For j = 0 To 10
            Me.Controls(Choose(i, "A", "B", "C", "D", "J", "M", "P", "R") & j).Visible = Me.optSet
        Next
    Next
    End Sub
    Last edited by June7; 06-21-2023 at 06:52 PM.
    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
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You need to use Set for a control object:
    Code:
    Dim prop As Control
    Set prop = Forms(frm).Controls(8) 'This tells me the object variable isn't set
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Gicu View Post
    You need to use Set for a control object:
    Code:
    Dim prop As Control
    Set prop = Forms(frm).Controls(8) 'This tells me the object variable isn't set
    Cheers,
    Quote Originally Posted by Gicu View Post
    You need to use Set for a control object:
    Code:
    Dim prop As Control
    Set prop = Forms(frm).Controls(8) 'This tells me the object variable isn't set
    Cheers,
    This is exactly what it was! Its interesting that if I try to set a value to a Form object without using "Set" it tells me "Invalid use of property" but in this case with a Control, Compiling tells me nothing and when the code is run it tells me the object variable isn't set.

    Compiling instantly warns me about the Form object but waits until running the line to tell me that the Control isn't set. I wonder why that is.
    I even tested this by making sure I am indeed attempting to assign a Form/Control respectively. (What I mean is I made sure to attempt assigning a Form to the Form Object, and a Control to the Control Object)

    After putting "Set" there I can use prop.width with no problem. Thanks!

    Quote Originally Posted by June7 View Post
    What is "8" supposed to be?

    What type of control do you want resize width of?

    Here is example of looping through all controls on form:
    Code:
    Dim ctlName As Control
    For Each ctlName In Me.Controls
        If ctlName.ControlType = acTextBox Or ctlName.ControlType = acComboBox Then ctlName.BackStyle = 0
    Next
    This example loops through a specific group of controls on form:
    Code:
    Private Sub optSet_AfterUpdate()
    Dim i As Integer, j As Integer
    For i = 1 To 8
        For j = 0 To 10
            Me.Controls(Choose(i, "A", "B", "C", "D", "J", "M", "P", "R") & j).Visible = Me.optSet
        Next
    Next
    End Sub
    The 8 is supposed to be the number of the control. I was trying to modify the 8th control. It was a placeholder as this is going to go into a loop that will loop through all the controls.
    I want my fields to auto-resize and auto-center themselves upon resizing the form.
    So on Form_Resize I am having it call a function to retrieve the width and height of the form and then I am going to manually use percentages to set them to be a certain percent from the edge of the screen.
    On Form_Open I have 2 separate functions to create arrays that store all the default Left and Top values so that I can use them to help calculate the difference between Controls.

    I am still working it out but currently this is the code I have written incase you are curious. (Forgive the mess)

    Code:
    Option Compare Database
    Option Explicit
    
    
    Public DefaultLeftValues() As Long, DefaultTopValues() As Long, LeftValues() As Long, TopValues() As Long
    
    
    
    
    'CAS = Cycle and Save
    'Prop = Property
    Public Function CasLeftProp(FormName As String)
    Dim ControlCount As Long, ctrl As Long, frm As Form
    Set frm = Forms(FormName)
    ControlCount = frm.Controls.Count
    ReDim DefaultLeftValues(0 To ControlCount)
    ReDim LeftValues(0 To ControlCount)
    
    
    For ctrl = 0 To ControlCount - 1
        DefaultLeftValues(ctrl) = frm.Controls(ctrl).Left
        If ctrl = 1 Or ctrl = 3 Then
        Debug.Print frm.Controls(ctrl).Name
        Debug.Print frm.Controls(ctrl).Left
        Debug.Print ctrl
        Debug.Print DefaultLeftValues(ctrl)
        End If
    Next ctrl
    End Function
    
    
    Public Function CasWidthProp(FormName As Form)
    Dim ControlCount As Long, ctrl As Long, frm As Form
    Set frm = Forms(FormName)
    ControlCount = frm.Controls.Count
    ReDim TopValues(0 To ControlCount)
    
    
    For ctrl = 0 To ControlCount - 1
        DefaultLeftValues(ctrl) = frm.Controls(ctrl).Left
    '    Debug.Print frm.Controls(Ctrl).Name
    '    Debug.Print frm.Controls(Ctrl).Left
    '    Debug.Print Ctrl
    '    Debug.Print DefaultLeftValues(Ctrl)
    Next ctrl
    End Function
    
    
    Public Function ResizeControl(FormName As Form)
    Dim frm As String, i As Long, ctrl
    i = 4
    'frm = FormName.Name
    'Debug.Print frm.Controls.Count
    'frm = cstr(Forms
    'Debug.Print Forms!frmSearchLarge.Controls.Item(4).Name
    'ctrl = Forms(FormName).Controls.Item(i).Name
    
    
    'DoCmd.Maximize
    Dim Width As Long, Height As Long
    Width = Forms(FormName.Name).InsideWidth
    Height = Forms(FormName.Name).InsideHeight
    Debug.Print "Width: " & Width
    Debug.Print "Height: " & Height
    
    
    'for loop to loop through controls and set the left postion to left + width/4 and width to width/2
    
    
    'Debug.Print "Left: " & leftvalue
    'Debug.Print "Width/4: " & Width / 4
    'Debug.Print "^^ Added: " & (Width / 4) + leftvalue
    Debug.Print "Width/2: " & Width / 2
    Debug.Print "(Width / 2) / 2: " & (Width / 2) / 2
    Debug.Print
    
    
    LeftValues(1) = Width / 40 + DefaultLeftValues(1)
    frm = FormName.Name
    Dim prop As Control
    prop = Forms(frm).Controls(8)
    FormName = Forms!frmSearchLarge
    'Dim prop As Property
    'prop = Forms(FormName).Contols(8).Width
    
    
    
    
    'Forms!frmSearchLarge.Controls ("cboPartnum")
    prop.Width = Width / 8
    Forms!frmSearchLarge.Controls("cboPartnum").Left = LeftValues(1) '(Width / 40) + DefaultLeftValues(1)
    
    
    LeftValues(3) = LeftValues(1) + (Forms!frmSearchLarge.Controls("cboPartnum").Width) + 400 '+ DefaultLeftValues(3)
    Forms!frmSearchLarge.Controls("cboAlloy").Width = Width / 8
    Forms!frmSearchLarge.Controls("cboAlloy").Left = LeftValues(3) '(Width / 40) + DefaultLeftValues(3)
    End Function

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Compiling instantly warns me about the Form object but waits until running the line to tell me that the Control isn't set. I wonder why that is.
    Some references are not evaluated until code runtime - especially those that use bang (!) as a separator.
    I say you don't "Set" variable values like x=1, you assign values to the variables. You Set object variables.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Micron View Post
    Some references are not evaluated until code runtime - especially those that use bang (!) as a separator.
    I say you don't "Set" variable values like x=1, you assign values to the variables. You Set object variables.
    That kind of makes sense to me. I understand what you are saying but my brain keeps asking "why?".

    "Dim x" initializes a variable space in memory.
    "x = 1" assigns 1 to the intialized memory for x

    "Dim FormVariable" would then initialize a variable space in memory.
    "Set FormVariable = Forms!InsertFormName" would then set the variable to redirect/reference the allocated space for the pre-existing object?
    Set is needed because we *setting* the variable to equal an already existing object versus assigning is us more manually putting a value in.

    Does this understanding sound correct?

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I have to leave for an appointment. See if this helps for now.
    https://learn.microsoft.com/en-us/of...ject-variables
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
    Public Function CasLeftProp(FormName As String)
    Dim ControlCount As Long, ctrl As Long, frm As Form
    Set frm = Forms(FormName)
    ControlCount = frm.Controls.Count
    why use "set frm" when you can pass the form object?

    Code:
    Public Function CasLeftProp(frm as form)
    
    Dim ControlCount As Long 
    
    ControlCount = frm.Controls.Count
    Same with controls

    Code:
    public SomeSub(ctl as control)
    
    ctl.width = 1440
    
    end sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Vita View Post
    "Set FormVariable = Forms!InsertFormName" would then set the variable to redirect/reference the allocated space for the pre-existing object?
    Set is needed because we *setting* the variable to equal an already existing object versus assigning is us more manually putting a value in.

    Does this understanding sound correct?
    Not always, if you are sloppy.

    See this thread and the answer in page 2.
    https://eileenslounge.com/viewtopic....39787&start=20

    All because someone did not use Option Explicit.
    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

  10. #10
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by moke123 View Post
    Code:
    Public Function CasLeftProp(FormName As String)
    Dim ControlCount As Long, ctrl As Long, frm As Form
    Set frm = Forms(FormName)
    ControlCount = frm.Controls.Count
    why use "set frm" when you can pass the form object?

    Code:
    Public Function CasLeftProp(frm as form)
    
    Dim ControlCount As Long 
    
    ControlCount = frm.Controls.Count
    I can't remember why I did this. I may have done it while trying to figure out a problem but I changed it back thanks!

    Quote Originally Posted by moke123 View Post
    Same with controls

    Code:
    public SomeSub(ctl as control)
    
    ctl.width = 1440
    
    end sub
    With controls its different. I use the controls.count to get the number of them and then cycle through each one and get the values so I want to reference the forms entire control group rather than pass just one control through.
    The goal of that function is to save every controls Left property to an array.

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    With controls its different. I use the controls.count to get the number of them and then cycle through each one and get the values so I want to reference the forms entire control group rather than pass just one control through.
    The goal of that function is to save every controls Left property to an array.
    Same concept passing the object. When you pass the form object all of its objects are exposed.

    I tend to use dictionaries for things like this, just a personal preference, as I like the methods and properties available with them.

    Code:
    Dim dictLeft As Object
    Dim dictTop As Object
    
    
    Sub GetLeftTop(frm As Form)
        Dim ctl As Control
        Dim k As Variant
    
    
        Set dictLeft = CreateObject("Scripting.Dictionary")
        Set dictTop = CreateObject("Scripting.Dictionary")
        
        For Each ctl In frm.Controls
    
    
            dictTop.Add ctl.Name, ctl.Top
            dictLeft.Add ctl.Name, ctl.Left
            
        Next
    
    
        For Each k In dictLeft.Keys
    
            Debug.Print "Control Name = " & k , "Left = " & dictLeft(k), "top = " & dictTop(k)
    
        Next
    
    
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by moke123 View Post
    Same concept passing the object. When you pass the form object all of its objects are exposed.

    I tend to use dictionaries for things like this, just a personal preference, as I like the methods and properties available with them.

    Code:
    Dim dictLeft As Object
    Dim dictTop As Object
    
    
    Sub GetLeftTop(frm As Form)
        Dim ctl As Control
        Dim k As Variant
    
    
        Set dictLeft = CreateObject("Scripting.Dictionary")
        Set dictTop = CreateObject("Scripting.Dictionary")
        
        For Each ctl In frm.Controls
    
    
            dictTop.Add ctl.Name, ctl.Top
            dictLeft.Add ctl.Name, ctl.Left
            
        Next
    
    
        For Each k In dictLeft.Keys
    
            Debug.Print "Control Name = " & k , "Left = " & dictLeft(k), "top = " & dictTop(k)
    
        Next
    
    
    End Sub
    Huh, Interesting. I haven't heard of or read about dictionaries. Is this a native object?

  13. #13
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    BTW, you don't need the control count to loop over them.
    Code:
    Dim ctl As Control
    For Each ctl in Me.Controls
       If ctl.ControlType = acTextBox Then do something
    Next
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Micron View Post
    BTW, you don't need the control count to loop over them.
    Code:
    Dim ctl As Control
    For Each ctl in Me.Controls
       If ctl.ControlType = acTextBox Then do something
    Next
    I also needed to count the controls so that I could set the array limit. I suppose I could increase the array limit with everytime the for loop is run.

    The part that is getting messy is the actual positioning since different controls are different sizes and in different places. Is there perhaps a better way to do what I am doing?
    Code:
    Public Function ResizeControl(FormName As Form)
    Dim Frm As String, i As Long, Ctrl As Control, ControlCount As Long
    Frm = FormName.Name
    ControlCount = FormName.Controls.Count
    
    
    'Gets the forms width and height
    Dim Width As Long, Height As Long
    Width = Forms(FormName.Name).InsideWidth
    Height = Forms(FormName.Name).InsideHeight
    'Debug.Print "Width: " & Width
    'Debug.Print "Height: " & Height
    
    
    
    
    
    
    
    
    'for loop to loop through controls and set the left postion
    For i = 0 To ControlCount - 1 '(Going to replace with a for each ctl in FormName.Controls as you suggested)
        Dim cboPartnumWidth: cboPartnumWidth = Width / 3.5
        LeftValues(7) = Width / 50 + DefaultLeftValues(7) 'calculates the place to put the bar. Technically the defaultleftvalues doesnt need to be there
        LeftValues(5) = LeftValues(7) + cboPartnumWidth + 500
        
        Dim MinMaxWidth As Long: MinMaxWidth = Width / 9 'Min and Max are shorthand for field names
        Dim TempNProcWidth As Long: TempNProcWidth = Width / 6 'Another shorthand for some field names
        LeftValues(16) = Width / 50 + DefaultLeftValues(16)
        LeftValues(18) = LeftValues(16) + MinMaxWidth + 200
        LeftValues(31) = LeftValues(18) + MinMaxWidth + 1000
        
        
        
        LeftValues(26) = LeftValues(5) + cboPartnumWidth + 200
        Select Case Forms(Frm).Controls(i).Name
            Case "cboPartnum", "lblPartnum"
                Set Ctrl = Forms(Frm).Controls(i) 'Should be 7
                Ctrl.Width = cboPartnumWidth
                Ctrl.Left = LeftValues(7)
                'Forms!frmSearchLarge.Controls ("cboPartnum")
                'Forms!frmSearchLarge.Controls("cboPartnum").Left = LeftValues(1) '(Width / 40) + DefaultLeftValues(1)
    
    
            Case "cboType", "lblType"
                Set Ctrl = Forms(Frm).Controls(i) 'Should be 5
                Ctrl.Width = cboPartnumWidth
                Ctrl.Left = LeftValues(5)
                
            Case "cboGrade", "lblGrade"
                Set Ctrl = Forms(Frm).Controls(i) 'should be 26
                'Ctrl.Width = Width / 20
                Ctrl.Left = LeftValues(26)
            
            Case "txtMax", "lblMax"
                Set Ctrl = Forms(Frm).Controls(i) 'Should be 16
                Ctrl.Width = MinMaxWidth
                Ctrl.Left = LeftValues(16)
            
            Case "txtMin", "lblMin"
                Set Ctrl = Forms(Frm).Controls(i) 'Should be 18
                Ctrl.Width = MinMaxWidth
                Ctrl.Left = LeftValues(18)
                
            Case "cboTemp", "lblTemp"
                Set Ctrl = Forms(Frm).Controls(i) 'Should be 31
                Ctrl.Width = TempNProcWidth
                Ctrl.Left = LeftValues(31)
                
            Case "chkA", "lblA", "chkB", "lblB", "chkC", "lblC"
                Set Ctrl = Forms(Frm).Controls(i) 'chkA = 37, chkB = 39, chkC = 41
                'ctrl.width
                Select Case i
                    Case 37, 39, 41 'checkboxes
                        Ctrl.Left = LeftValues(26) + 1500
                    Case 38, 40, 42 'labels
                        Ctrl.Left = LeftValues(26) + 1800
                End Select
                'select statement for 'top' property
        End Select
    Next
    'Debug.Print Ctrl.Name
    End Function

  15. #15
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I also needed to count the controls so that I could set the array limit.
    Yes, I forgot that you were using an array and that I had mentioned that I probably would not for this.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 03-09-2018, 11:11 PM
  2. Replies: 15
    Last Post: 05-12-2016, 02:27 PM
  3. Replies: 7
    Last Post: 04-22-2016, 08:19 AM
  4. Replies: 1
    Last Post: 10-06-2015, 06:50 AM
  5. Wrong reference with .fields in DAO recordset
    By ddd in forum Programming
    Replies: 1
    Last Post: 12-08-2009, 05:34 PM

Tags for this Thread

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