Position form near control that called it

B

Barry

Howdy:
I would like to position a form (a calendar) near the control that issued a
call to open the calendar form. So, wherever on my calling form there is a
control that requires a date, the calendar form will open just beneath that
control. Is this a job for MoveSize? Any examples around to show the way?
I look forward to your input.
Thanks,
Barry
 
S

Stuart McCall

Barry said:
Howdy:
I would like to position a form (a calendar) near the control that issued
a
call to open the calendar form. So, wherever on my calling form there is
a
control that requires a date, the calendar form will open just beneath
that
control. Is this a job for MoveSize? Any examples around to show the
way?
I look forward to your input.
Thanks,
Barry

There's more to this than you might imagine. Here's what I use:

http://www.smccall.demon.co.uk/Forms.htm#OpenUnder

HTH
 
B

Barry

Stuart:
Thanks. that certainly is more than I had bargained for. I was hoping that
there was a method I might actually understand. This will give me something
to chew on for a while. Is there no other way to find the position of a
control using the Access form's Left, Top and Down in relation to the Access
window?
Thanks again,
Barry
 
D

Dirk Goldgar

Barry said:
Howdy:
I would like to position a form (a calendar) near the control that issued
a
call to open the calendar form. So, wherever on my calling form there is
a
control that requires a date, the calendar form will open just beneath
that
control. Is this a job for MoveSize? Any examples around to show the
way?
I look forward to your input.


Some time ago, I worked this out for a newsgroup post:

'------ start of code ------

Dim strFormToOpen As String
Dim ctlTarget As Access.Control
Dim lngBorderHoriz As Long
Dim lngBorderVert As Long

strFormToOpen = "SomeFormName"

Set ctlTarget = Me.ActiveControl

DoCmd.OpenForm strFormToOpen

lngBorderHoriz = (Me.WindowWidth - Me.InsideWidth) / 2
lngBorderVert = (Me.WindowHeight - Me.InsideHeight) / 2

With Forms(strFormToOpen)
.Move _
(Me.WindowLeft - .WindowWidth) + lngBorderHoriz +
ctlTarget.Left, _
Me.WindowTop + lngBorderVert + ctlTarget.Top
End With
'------ end of code ------

In the above case, I opened the form to the left of the calling control, but
it should be straightforward to modify the code to get other relative
positions.
 
S

Stuart McCall

Barry said:
Stuart:
Thanks. that certainly is more than I had bargained for. I was hoping
that
there was a method I might actually understand. This will give me
something
to chew on for a while. Is there no other way to find the position of a
control using the Access form's Left, Top and Down in relation to the
Access
window?
Thanks again,
Barry
<snip>

The way Access forms are constructed and their built-in behaviour makes this
more of a task than we'd like. Fortunately you don't need to understand the
code in order to use it. Just follow the easy instructions in the
DropDownForm procedure.
 
B

Barry

Dirk:
Thanks so much for your help. At least this is something that I can wrap my
head around. I will give it a try in my app as soon as I have a moment.
Thanks again.
Barry
 
S

Stuart McCall

Bill said:
Good example Dirk.
This is much simpler than using APIs like I've found in most other
examples.

Thanks

I need this to open a popup form to the right of the command button so I
created this variation.
works by calling it as a sub procedure with parameters
and uses more variables for position to get the relative position to the
right of the control


'------ start of code ------

' sCForm is the Calling Form Name
' sCCtrl is the Calling Control Name
' strFormToOpen is the Name of the Form to Open
'
Sub SetFormPosition(sCForm As String, sCCtrl As String, strFormToOpen As
String)
Dim ctlTarget As Access.Control
Dim lngBorderHoriz As Long
Dim lngBorderVert As Long
Dim lngCmdWidth As Long
Dim lngCmdHt As Long
Dim lngCmdTop As Long
Dim lngFrmHt As Long
Dim lngLeftBuffer As Long

Set ctlTarget = Forms(sCForm).Controls(sCCtrl)

DoCmd.OpenForm strFormToOpen
With Forms(sCForm)
lngBorderHoriz = (.WindowWidth - .InsideWidth) / 5 ' adjust value for
position as needed
lngBorderVert = (.WindowHeight - .InsideHeight) / 2

lngCmdWidth = ctlTarget.Left + ctlTarget.Width
lngCmdHt = ctlTarget.Height
lngCmdTop = ctlTarget.Top
lngFrmHt = Forms(strFormToOpen).WindowHeight

' Buffer value for space from right of control to left of popup form
lngLeftBuffer = 100

nWinLeftPos = .WindowLeft + lngBorderHoriz + lngCmdWidth +
lngLeftBuffer
nWinTopPos = .WindowTop + lngBorderVert + lngCmdTop + lngCmdHt -
lngFrmHt

End With

' Moves the form position
Forms(strFormToOpen).Move nWinLeftPos, nWinTopPos

End Sub

'------ end of code ------

That code will work for a form set up in a particular way, not the general
case though. If that's sufficient for your needs then fair enough. However:
Try switching on the main form's header. Try it with and without a record
selector on the main form. Try it with the control to be opened under close
to the right or bottom of the screen. Also try with the main form control on
a tab page. Those are just the things that spring to mind - I wrote my
module quite some time ago so I'm a bit vague on all the pros and cons, but
you will meet them, guaranteed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top