Positioning Controls with VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm attempting to place controls on a form based on checkbox values. More specifically, when a user clicks a checkbox and then clicks an OK button, text boxes are created and placed on a new form. Everything works fine, but the textbox controls end up on top of one another with the exception of the final textbox. Here are code snippets for reference

Dim ctlTextBox As Contro
Dim chkCheckBox As Contro
Dim intSelBoxCount As Intege
Dim intSelBoxTotal As Intege
Dim intPosition As Intege

intPosition = 10

For intSelBoxCount = 1 To intSelBoxTota
intPosition = intPosition + 2
ctlTextBox.Top = intPositio
Next intSelBoxCoun

Any help or suggestions would be greatly appreciated!
 
ZZK said:
I'm attempting to place controls on a form based on checkbox values. More specifically, when a user clicks a checkbox and then clicks an OK button, text boxes are created and placed on a new form. Everything works fine, but the textbox controls end up on top of one another with the exception of the final textbox. Here are code snippets for reference:

Dim ctlTextBox As Control
Dim chkCheckBox As Control
Dim intSelBoxCount As Integer
Dim intSelBoxTotal As Integer
Dim intPosition As Integer

intPosition = 100

For intSelBoxCount = 1 To intSelBoxTotal
intPosition = intPosition + 25
ctlTextBox.Top = intPosition
Next intSelBoxCount

The 25 is a suspiciously small height for the check boxes.

Don't forget that the position and size properties are
specified in vba code as twips (1440 per inch). Try using a
larger value, such as

intPosition = intPosition + 200

You are not really using CreateControl in reponse to a
user's action, are you? That would be a BAD THING!
 
Thanks for your response! I have tried larger numbers with the same result. The created forms and controls are temporary and are disposed of after the data is posted.
 
ZZK said:
Thanks for your response! I have tried larger numbers with the same result.

I don't have enough information to guess at a reason why the
larger spacing doesn't work for you. It works fine for me.

The created forms and controls are temporary and are disposed of after the data is posted.

They're still going to cause bloat and increase the
potential for corruption.
 
Okay, suppose I take the far easier route and create a form containing all needed controls and then set the Visible property accordingly (at run time). When the form is displayed at run time, the controls will be all over the place. Any suggestions on how to work around this problem?
 
ZZK said:
Okay, suppose I take the far easier route and create a form containing all needed controls and then set the Visible property accordingly (at run time). When the form is displayed at run time, the controls will be all over the place. Any suggestions on how to work around this problem?

You can set their Left and Top property, pretty much the
same way you could if you created the control.

I just reviewed your original question again and belatedly
noticed that you do not change the control object in the
loop. No wonder only one is "moved".

I don't know how you named the controls, but the way I
manipulate a pool of invisible controls is by naming them
with a common prefix and a sequential numerical suffix. e.g.
txtBox1, txtBox2, . . ., txtBox20. This way I can use a
variable to keep track of which control I'm working on.
Your loop to position the controls would look like:

intPosition = 100
For intSelBoxCount = 1 To intSelBoxTotal
intPosition = intPosition + 200
Me("txtBox" & intSelBoxCount ).Top = intPosition
Next intSelBoxCount
 
That's pretty much what I was thinking, but I wasn't sure if I could make it work. I knew I needed to change the control each time the loop ran, but I wasn't quite sure how to do it. Thanks so much for your help!
 
Back
Top