Search code examples
vbams-access

How can you move controls around an Access form?


I am working on an Access database and need to move text boxes and labels around a form. I am trying to use the .Move function to move the controls around on the form but it doesn't seem to work properly as all it will do is move all of the specified elements to the top left corner of the form regardless of what is specified. This is my code,

Private Sub Form_Activate()
    Dim gObj As Control
    Dim lObj As Control
    Dim llObj As Control
    Dim lllObj As Control
    Dim llllObj As Control
    Dim aObj As Control
    Dim aaObj As Control
    Dim aaaObj As Control
    Dim condit As String
    Dim x1 As Variant
    Dim x2 As Variant
    Dim y1 As Variant
    Dim y2 As Variant
    
    x1 = 0.0417
    x2 = 0.0417
    t1 = 0.5417
    t2 = 0.7917
    For i = 0 To 22
        Set gObj = Me.Controls("G" & i)
        If gObj.Value Then
            condit = gObj.Value
        Else
            condit = ""
        End If
        If condit = "" Or condit = "0" Then
            Set lObj = Me.Controls("L" & i)
            Set llObj = Me.Controls("LL" & i)
            Set lllObj = Me.Controls("LLL" & i)
            Set llllObj = Me.Controls("LLLL" & i)
            
            Set aObj = Me.Controls("A" & i)
            Set aaObj = Me.Controls("AA" & i)
            Set aaaObj = Me.Controls("AAA" & i)
            
            gObj.Visible = False
            lObj.Visible = False
            llObj.Visible = False
            lllObj.Visible = False
            llllObj.Visible = False
            
            aObj.Visible = False
            aaObj.Visible = False
            aaaObj.Visible = False
            Else
                Set lObj = Me.Controls("L" & i)
                Set llObj = Me.Controls("LL" & i)
                Set lllObj = Me.Controls("LLL" & i)
                Set llllObj = Me.Controls("LLLL" & i)

                Set aObj = Me.Controls("A" & i)
                Set aaObj = Me.Controls("AA" & i)
                Set aaaObj = Me.Controls("AAA" & i)

                'lObj.Move Left:=x1, Top:=t1
                'lObj.Move Top:=t1

                'gObj.Move Left:=x2, Top:=t2
                'gObj.Move Top:=t2
                lObj.Move lObj.Left = x1, lObj.Top = t1
                gObj.Move gObj.Left = x2, gObj.Top = t2
                x1 = x1 + 0.7916
                x2 = x2 + 0.7916
            End If
        Next
End Sub

Am I using the .Move function wrong it seems to be in line with the documentation provided on the Microsoft Documentation.

I have tried multiple different syntax combinations for the .move function but none have worked so far. I am able to move elements on the y-axis but no the x-axis.


Solution

  • .Move parameters are in Twips.

    There are approximately 1440 twips to a logical inch or 567 twips to a logical centimeter

    So it's not surprising they all get clustered in the top left if you use values < 1.
    Try:

    Const TPC = 567  ' Twips per centimeter
    x1 = 0.0417 * TPC
    t1 = 0.5417 * TPC
    
    lObj.Move Left:=x1, Top:=t1