How to Get and Set the width of a datasheet view form via code

How can I get and set the width of a datasheet view form via code? The Form.width property seems to affect the width of the Detail control, not the form.
LVL 1
MilewskpAsked:
Who is Participating?
 
Jeffrey CoachmanConnect With a Mentor MIS LiasonCommented:
If this is a Datasheet view subform, then you can set the width of the Subform "Object" from the main form like so:

Me.YourSubForm.Width=1440
0
 
Rey Obrero (Capricorn1)Commented:
well, whatever the width of the detail section will also be the width of the form.
0
 
Jeffrey CoachmanMIS LiasonCommented:
Like cap, I don't understand your question.

"How can I get and set the width of a datasheet view form"
Technically, A Datasheet has no Width.
It's width is set to the summed width of all the textboxes...(actually Column widths in this case)
You can set this by using the ColumnWidth setting for each field. (remember that this is in Twips, and one inch is 1440 twips)

"The Form.width property seems to affect the width of the Detail control"
Do you mean Detail "Section"?

Can you take a step back and explain what your ultimate goal is here?
(is this a datasheet view subform?)

JeffCoachman

0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Connect With a Mentor Database ArchitectCommented:
Suppose you have - in Design View the setting shown in image 1
... which opens too short - image 2
and you really want the result in image 3

Then you can do either of these - to control the width:

>> sets the datasheet form width to 5 inches
Private Sub Form_Load()
    DoCmd.MoveSize 1440, 1440, 1440 * 5, 1440 * 5
'                      ... width                  ^^^^^^^^
End Sub

OR

>> sets the Width of the datasheet form the Form Width (7" in this example)
Private Sub Form_Load()
    DoCmd.MoveSize 1440, 1440, Me.Width, 1440 * 5
End Sub

Is this what you are talking about ?

Now ... IF ... this datasheet is in a SubForm control as Jeff mentioned ... then in addition you will need to deal with the width of the subform control also.

mx
Capture1.gif
Capture2.gif
Capture3.gif
0
 
Jeffrey CoachmanMIS LiasonCommented:
Yes, as always, sometimes it is best to explain in more detail what you are trying to accomplish.
So as not to have Experts use resources that could be redirected to helping other members.

Sound fair?

JeffCoachman
0
 
MilewskpAuthor Commented:
Hi cap,
<whatever the width of the detail section will also be the width of the form. >
In my experience the width of the deatil section and the width of the form are indedpendent for DS view forms.

Hi Jeff,
<A Datasheet has no Width. It's width is set to the summed width of all the textboxes>
I'm talking about the width of the form window, not the width of the datasheet.

<If this is a Datasheet view subform>
It's the main form.

Hi mx,
Yes, MoveSize allows me to SET the form width, but I'd also like to be able to GET the width.

0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
How about

Private Sub Form_Load()


    DoCmd.MoveSize 1440, 1440, Me.Width, 1440 * 5
    Dim x
    x = Me.Width
    MsgBox "The width is: " & x / 1440
   
   
End Sub




Capture1.gif
Capture2.gif
0
 
MilewskpAuthor Commented:
Hi mx,
As I said, the width property for ds forms is the width of the detail section, not the width of the form window.
0
 
Rey Obrero (Capricorn1)Commented:
Milewskp,

if you have a Datasheet form,  place this in the Load event of the of the form

debug.print me.width, me.width/1440


then open the DS form, alone not as a subform
see what it gives..
0
 
Rey Obrero (Capricorn1)Commented:
oh and then in the design view of the DS form see the width of the FORM
0
 
Jeffrey CoachmanMIS LiasonCommented:
Milewskp,
On the open event of the form put this:
MsgBox "The Width of the Form Window is: " & Me.WindowWidth
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"the width property for ds forms is the width of the detail section, not the width of the form window."
Not the case.  There is *no* Width property for the Detail Section - see Image.

The Width of the Detail Section and the Width of (any) Form is one in the same.  However, how 'wide' a Form actually opens, based on various settings ... can be totally controlled by

    DoCmd.MoveSize 1440, 1440, 1440 * 5, 1440 * 5
'                      ... width    
... as I showed above.

This Q has been answered @ http:#a34975439 and http:#a34975770

mx
Capture1.gif
0
 
Rey Obrero (Capricorn1)Commented:

http:#a34974078

well, whatever the width of the detail section will also be the width of the form.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
But the Question was:
"How can I get and set the width of a datasheet view form via code?"
0
 
GRayLCommented:
I've had a play with this for a few hours and discovered that the width of a form in datasheet view is the sum of each of the columns in the ActiveDataSheet.  You can play with this in the Immediate Pane.
First get a count of the number of controls in the ActiveDataSheet

cons = screen.ActiveDataSheet.Controls.Count

Then set the width of each control (every second one is a label) to that required to fit the data ( including column names).  This will get rid of any -1 (default) values of columnwidth by making them equal to -2 (fit the data)

for c = 0 to cons-1 step 2:Screen.ActiveDataSheet.Controls(c).ColumnWidth = -2

Now get the width of the form fw  in twips (divide by 1440) to get inches:

for c = 0 to cons-1 step 2:fw = fw + Screen.ActiveDataSheet.Controls(c).ColumnWidth:next c
7335 - this was for my test form

Set the width of any control

Screen.ActiveDataSheet.Controls(0).ColumnWidth = 1000
------------------------------------------^-the first column

or use the control name:

Screen.ActiveDataSheet!DrID.ColumnWidth = 1000

0
 
MilewskpAuthor Commented:
Hi GrayL,
I implemented your suggestion with the attached mdb:
 - Open Form1.
 - When you move from record to record, a msgbox tells you the sum of the column widths.

But, I wasn't asking about the sum of the column widths, I was asking about the width of the form window.
db1.mdb
0
 
Rey Obrero (Capricorn1)Commented:
0
 
MilewskpAuthor Commented:
Hi Jeff,
<Me.WindowWidth>
You're using the read only property Me.WindowWidth to get the width of the form window.  Thanks. And sorry to all - I didn't spot this post before.

<sometimes it is best to explain in more detail what you are trying to accomplish.
So as not to have Experts use resources that could be redirected to helping other members.>
You're correct, in hindsight (always 20/20), I should have said: How can I get and set the width OF THE FORM WINDOW of a datasheet view form via code?

-----------------------------------------------------------

The question has now been completely answered:
 - To get the width of the form window, use: Me.Windowwidth  (thanks Jeff)
 - To set the width of the form window, use: Docmd.MoveSize (thanks mx)
And thanks to everyone else for your input.

I'll split the points between Jeff and mx.
0
 
Jeffrey CoachmanMIS LiasonCommented:
OK
0
 
Jeffrey CoachmanMIS LiasonCommented:
I window width helped solve the issue then you accepted the worg post of mine.

The post you accepted here was:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26846052.html?cid=1131#a34975016

But the actual post that you used was this:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26846052.html?cid=1131#a34985495

So it may be clearer to others searching here what the actual solutions were if you clicked the "Request Attention" link, reopened this Q, and re-selected the more appropriate posts...
;-)

Jeff
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.