Solved

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

Posted on 2011-02-24
20
498 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
  • 6
  • 5
  • 4
  • +2
20 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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 250 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
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 250 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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
ID: 34985299
oh and then in the design view of the DS form see the width of the FORM
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

895 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now