Dynamic Array Problems

E

Eddy

In Access2003 I am trying to store the Top property value for each control
in an array, move to another sub procedure behind the report that
manipulates the controls then reset the Top property values to the controls
that I stored in the array. I am using the following code:
At the module level I declare the array:
Private varVal() As Variant

To store the values in the array I use this code:
Private Sub Set()
Dim intX as Integer
ReDim varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
varValHdr1(intX) = ctl.Top
intX = intX + 1
Next ctl


To restore the original Top values I use this code:
Private Sub Restore()
Dim intX As Integer
ReDim Preserve varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
ctl.Top = varValHdr1(intX)
intX = intX + 1
Next ctl


The code goes through the first Private Sub Set fine. When it runs the sub
Restore it loads 3 values back then gives me the error; "The Control or Sub
form control is to large for this location". I notice when I stop the code
and in the Immediate Window when I query ?ctl.Top it returns a value of 630
yet when I am in the report in design mode the Top value for the same
control is .4332

Sorry for such a elaborate question. I'm new to this and couldn't figure out
a shorter way.

Thanks again.
 
M

Marshall Barton

Eddy said:
In Access2003 I am trying to store the Top property value for each control
in an array, move to another sub procedure behind the report that
manipulates the controls then reset the Top property values to the controls
that I stored in the array. I am using the following code:
At the module level I declare the array:
Private varVal() As Variant

To store the values in the array I use this code:
Private Sub Set()
Dim intX as Integer
ReDim varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
varValHdr1(intX) = ctl.Top
intX = intX + 1
Next ctl


To restore the original Top values I use this code:
Private Sub Restore()
Dim intX As Integer
ReDim Preserve varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
ctl.Top = varValHdr1(intX)
intX = intX + 1
Next ctl


The code goes through the first Private Sub Set fine. When it runs the sub
Restore it loads 3 values back then gives me the error; "The Control or Sub
form control is to large for this location". I notice when I stop the code
and in the Immediate Window when I query ?ctl.Top it returns a value of 630
yet when I am in the report in design mode the Top value for the same
control is .4332


This is a tricky issue. Before we go into details, you need
to be aware that the control properties in design view are
specified in user friendly units of inches or centimeters,
BUT internally and in VBA they are in twips (1440 per inch).
So your .4332 is 623.8 twips which is within rounding error
of the 630 you're seeing.

Now to the error you're getting. This is caused by the
control's Top + Height going beyond the bottom of the
section. If you've also adjusted a control's Height, then
you need to reset it similarly to the way you're resetting
the Top property. If not, then either the control is being
placed too far down the section or the section is not quite
tall enough. (The same situation can arise with the Left
and Width properties.)

It may be a typo, but the code you posted is using a
different array name than what is declared. There is no
reason to use ReDim in the Restore procedure. Actually, the
way you have the code for the Set procedure, you wouldn't
need the ReDim either, just declare the original array for
your 32 elements.

The other thing that comes to mind is that you should only
be doing this in the Format event. By the time the Print
event occurs, CanGrow/CanShrink can have changed the Height
of both the section and some of the controls in it.

I don't have a lot of confidence that any of this explains
your problem, so I think some additional debugging will be
in order.
 
D

Dirk Goldgar

Eddy said:
In Access2003 I am trying to store the Top property value for each
control in an array, move to another sub procedure behind the report
that manipulates the controls then reset the Top property values to
the controls that I stored in the array. I am using the following
code:
At the module level I declare the array:
Private varVal() As Variant

To store the values in the array I use this code:
Private Sub Set()
Dim intX as Integer
ReDim varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
varValHdr1(intX) = ctl.Top
intX = intX + 1
Next ctl


To restore the original Top values I use this code:
Private Sub Restore()
Dim intX As Integer
ReDim Preserve varValHdr1(intX + 32)
For Each ctl In Me.GroupHeader3.Controls
ctl.Top = varValHdr1(intX)
intX = intX + 1
Next ctl


The code goes through the first Private Sub Set fine. When it runs
the sub Restore it loads 3 values back then gives me the error; "The
Control or Sub form control is to large for this location". I notice
when I stop the code and in the Immediate Window when I query
?ctl.Top it returns a value of 630 yet when I am in the report in
design mode the Top value for the same control is .4332

Sorry for such a elaborate question. I'm new to this and couldn't
figure out a shorter way.

Thanks again.

I hope you left out some parts of your code, because otherwise it
doesn't make sense. However, you might consider using the Tag property
of each control instead of your dynamic array:

' *** UNTESTED CODE ***
Private Sub Set()

Dim ct As Control

For Each ctl In Me.GroupHeader3.Controls
ctl.Tag = CStr(ctl.Top)
Next ctl

End Sub

Private Sub Restore()

Dim ct As Control

For Each ctl In Me.GroupHeader3.Controls
ctl.Top = CLng(ctl.Top)
Next ctl

End Sub

The fact that, in the Immediate Window, ?ctl.Top returns a value of 630
but viewing the control's property sheet in design view shows the Top
value as .4332 shouldn't concern you, because the property sheet shows
the values in inches or centimeters, whereas the values are actually
stored as "twips". One twip = 1/1440 inch.

It occurs to me that you may get errors using this method when controls
are inside other controls. For example, option buttons inside an option
group might have a problem. If your code is not otherwise in error,
maybe that's what's going on. Check out which controls cause the error,
and whether there's anything unusual about them.

I don't suppose you have resized the section so that the controls being
reset no longer fit in it?
 

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

Top