form width in VBA

ajetrumpet

Banned
Local time
Today, 05:26
Joined
Jun 22, 2007
Messages
5,638
all,

i am trying to dynamicaly change my form width when it opens. here is some relevant code:
Code:
Private Sub Command232_Click()

'FOR TESTING ONLY
Me.Width = 19320
DoCmd.Save

End Sub

Private Sub Form_Current()

DoCmd.Maximize

End Sub

Private Sub Form_Open(Cancel As Integer)

Me.Width = 19320

Debug.Print Me.Width

Call DefaultsNeeded
the width is set in twips, but my form width is not changing. it is not "sticking" so to speak. i have edits allowed, design changes allowed in all views too. any advice anyone?

i have also tried the me.INSIDEWIDTH property to no avail.
 
You have to open it in design view (via code) before setting the property and then in the DoCmd.Close you would choose the acSaveYes.

You can open it in design view but also as hidden so it is not visible during this operation.
 
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.Width = 1440 '1440= 1 in.
    [B]DoCmd.RunCommand acCmdSizeToFitForm
[/B]    'Debug.Print Me.Width
End Sub
this will change the width of the form from outside-border to outside-border, NOT the detail's width. so if the detail is 5" and you use the code above, the form will be 1" across, hiding most of the detail; if the detail is 1" and the code above sets the width to 5", there will be a lot of extra grey to the right.
 
use Me.Move

You can pass the left, top, width, and height to resize your form.
 
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.Width = 1440 '1440= 1 in.
    [B]DoCmd.RunCommand acCmdSizeToFitForm
[/B]    'Debug.Print Me.Width
End Sub

wazz, i have already tried that runcommand. it doesn't work man. am i using it wrong?
 
did you remove the maximize code?
 
yes i did wazz.

it does not center all of the controls in the form. i'm still working on it though...right now i am moving all of them manually by looping. i just have to get the math right...
 
In my experience you can always size a form by setting it's InsideWidth and InsideHeight properties. Note that when these properties are assigned a new value the Resize event for the form is triggered.

Here's a sample that enforces a minimum form size during a resize...
Code:
Private Sub Form_Resize()
[COLOR="Green"]  'disable resize handling during resize handling[/COLOR]
  Me.OnResize = ""
[COLOR="Green"]  'enforce minimums, which fire resize events if applied[/COLOR]
  if me.insidewidth < 4*1440 then me.insidewidth = 4 * 1440
  if me.insideheight < 4*1440 then me.insideheight = 4 * 1440
[COLOR="Green"]  're-enable resize handling[/COLOR]
  me.onresize = "[Event Procedure]"
end sub
 
so i take it you want to change the size of the detail section? you'll have to go with SOS's suggestion for that (or a combination of his and mine, in various events.)
 
And if you want to save that you would need to do what I said for saving the design changes. Otherwise you could put something into a table and then use that when loading up the form.
 
guys,

i've got the width sized on the form open event just fine. it is now the size of the screen width, which is what i wanted.

now i'm trying to move the controls around so they are centered.

the creater of this thing MANUALLY centered the form based on the width of the entire form header. the detail section is manually centered underneath the header section.

so i don't know any other way to do this other than using math to move all the controls around inside a loop. here is what i am trying:
Code:
For Each c In Me.Controls
  c.left = c.left + ((Me.Width - 15300) / 2)
Next c

that does not work, and i know the math isn't right. 15300 is the original size of the form width in twips. i'm trying to use that figure, and the width of the window, which is now the width of the form, to try and center every control on open...
 
i have the problem solved, but probably not too elegant. it will do for now. here is my slipshot fix to this problem...
Code:
Dim c As Control
Dim player As Object
Set player = Forms!MainSystem!ClientMovie.Object
player.Controls.Pause

DoCmd.OpenForm "listenskills", acDesign, , , , acHidden
Forms!listenskills.Width = Me.WindowWidth
    
For Each c In Forms!listenskills.Controls
  c.left = c.left + ((Forms!listenskills.Width - 591 - 15300) / 2) '15300 = ORIGINAL FORM WIDTH
Next c

DoCmd.Save acForm, "listenskills"
DoCmd.OpenForm "listenskills", acNormal

Forms!MainSystem!ButtonListen.SpecialEffect = 2
591 is the width in twips of the extra space off the form screen that is put in by access when you set the form width = window width.
 
Thanks Wazz, that's very kind.

AJ: I'd expect the math to center a control to look like this...
Code:
Private Sub Form_Resize()
   Dim c As Control
   Dim fc As Long

   [COLOR="Green"]'get center of form[/COLOR]
   fc = Me.InsideWidth / 2   
   [COLOR="Green"]'traverse controls[/COLOR]
   For Each c In Me.Controls   
[COLOR="Green"]      'move control half its own width left of center[/COLOR]
      c.Left = fc - c.Width / 2
   Next
End Sub
 
i certainly do appreciate everyon'es help on this. i have gotten it figured out, but it was a bearcat.

here is the code i had to use:
Code:
Dim c As Control
Dim player As Object
Set player = Forms!MainSystem!ClientMovie.Object
player.Controls.Pause

DoCmd.OpenForm "listenskills", acDesign, , , , acHidden

If Forms!listenskills.Width <> 19320 Then '19320 = MONITOR WIDTH IN TWIPS

Forms!listenskills.Width = 19320

For Each c In Forms!listenskills.Controls
  c.left = c.left + ((Forms!listenskills.Width - 591 - 15300) / 2) '15300 = ORIGINAL FORM WIDTH
Next c

DoCmd.Save acForm, "listenskills"

End If

DoCmd.OpenForm "listenskills", acNormal

Forms!MainSystem!ButtonListen.SpecialEffect = 2
 

Users who are viewing this thread

Back
Top Bottom