Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-24
20
Medium Priority
?
511 Views
Last Modified: 2013-11-28
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.
0
Comment
Question by:Milewskp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +2
20 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34974078
well, whatever the width of the detail section will also be the width of the form.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34974991
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
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 1000 total points
ID: 34975016
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 total points
ID: 34975439
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34975635
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
 
LVL 1

Author Comment

by:Milewskp
ID: 34975734
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
 
LVL 75
ID: 34975770
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
 
LVL 1

Author Comment

by:Milewskp
ID: 34985266
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34985296
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34985299
oh and then in the design view of the DS form see the width of the FORM
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34985495
Milewskp,
On the open event of the form put this:
MsgBox "The Width of the Form Window is: " & Me.WindowWidth
0
 
LVL 75
ID: 34985759
"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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34985765

http:#a34974078

well, whatever the width of the detail section will also be the width of the form.
0
 
LVL 75
ID: 34985772
But the Question was:
"How can I get and set the width of a datasheet view form via code?"
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34988377
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
 
LVL 1

Author Comment

by:Milewskp
ID: 34991505
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34991803
0
 
LVL 1

Author Comment

by:Milewskp
ID: 34993120
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34994243
OK
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35071352
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question