control alignment

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi Group,

I would like to position my controls using code. When I set the variable to
= the control name by using the controlname the code works fine. However, I
would like to reference the controlname as a variable

So instead of
strTxt1 = me.billingAddress

I would like
strBookmark1 = me.txtBilling address
strtxt1 = strBookmark1

Current code extract.

intleft = 400
intTop1 = 500
intTop2 = 1000
Set strTxt1 = me.txtBillingAddress
strTxt1.Left = intleft
strTxt1.Top = intTop1
strTxt1.Visible = True
Set strlbl1 = Me.lblBilling
With strlbl1
.Left = intleftLabel
.Top = intTop1
.Visible = True
End With
Set chk1 = Me.chkBillingAddress
With chk1
.Left = intleftLabel
.Top = intTop1
.Visible = True
End With
 
You can reference a control by its name within the module for the form (or
report)
Me.Controls("NameOfControl")

so you could use this sequence

strBookMark1 = Me.TxtBillingAddress.NAME
Me.Controls(strBookMark1).Left = iLeft

Try

Dim ctlAny as Control

Set ctlAny = me.txtBillingAddress

With ctlAny
.Left = intLeft
.Top = intTop1
.Visible = True
End With

Set ctlAny = Me.lblBilling
With ctlAny
...
End With


Or you could write sub that takes four arguments (Public or Private Sub)
depending on the scope of where you need the sub to work.

Public Sub moveControl(ctlAny as Control, iLeft as Integer, iTop as Integer,
tfVisible as Boolean)

With CtlAny
.Left = iLeft
.Top = iTop
.Visible = tfVisible
End With

End Sub

Then Call it when you need it with

MoveControl Me.txtBillingAddress, intLeft, intTop1, True
MoveControl Me.lblBilling, intLeftLabel, intTop1,True
MoveControl Me.chkBillingAddress, intLeftLabel, intTop1, True

If you want to hide the control and not move it, the call would be
MoveControl Me.txtBillingAddress, Me.txtBillingAddress.Left,
Me.txtBillingAddress.Top, False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top