Solved

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

Posted on 2011-02-24
20
496 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
Comment Utility
well, whatever the width of the detail section will also be the width of the form.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oh and then in the design view of the DS form see the width of the FORM
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
Milewskp,
On the open event of the form put this:
MsgBox "The Width of the Form Window is: " & Me.WindowWidth
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"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
Comment Utility

http:#a34974078

well, whatever the width of the detail section will also be the width of the form.
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

by:Milewskp
Comment Utility
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
Comment Utility
OK
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

16 Experts available now in Live!

Get 1:1 Help Now