Online Book Reader

Home Category

Access Cookbook - Ken Getz [55]

By Root 1916 0
frm.Hwnd, rct

' If the form is a popup window, its parent won't

' be the Access main window. If so, don't

' bother subtracting off the coordinates of the

' main Access window.

If hwndParent <> Application.hWndAccessApp Then

acb_apiGetWindowRect hwndParent, rctParent

' Subtract off the left and top parent coordinates, since you

' need coordinates relative to the parent for the

' acb_apiMoveWindow( ) function call.

rct.lngX1 = (rct.lngX1 - rctParent.lngX1 - adhcBorderWidthX)

rct.lngY1 = (rct.lngY1 - rctParent.lngY1 - adhcBorderWidthY)

rct.lngX2 = (rct.lngX2 - rctParent.lngX1 - adhcBorderWidthX)

rct.lngY2 = (rct.lngY2 - rctParent.lngY1 - adhcBorderWidthY)

End If

End Sub

The acbSaveSize procedure first retrieves the current coordinates for the requested form and then saves those values to the registry. Figure 2-17 shows the registry after saving the settings for the sample form. The function creates a key named Form Sizes in the registry, with a subkey for each form whose coordinates you save. Within each subkey, the procedure creates a separate value entry for each of the four coordinates. The source code related to the acbSaveSize procedure is:

Private Const acbcRegTag = "Form Sizes"

Private Const acbcRegLeft = "Left"

Private Const acbcRegRight = "Right"

Private Const acbcRegTop = "Top"

Private Const acbcRegBottom = "Bottom"

Public Sub acbSaveSize(frm As Form)

Dim rct As acbTypeRect

GetRelativeCoords frm, rct

With rct

SaveSetting acbcRegTag, frm.Name, acbcRegLeft, .lngX1

SaveSetting acbcRegTag, frm.Name, acbcRegRight, .lngX2

SaveSetting acbcRegTag, frm.Name, acbcRegTop, .lngY1

SaveSetting acbcRegTag, frm.Name, acbcRegBottom, .lngY2

End With

End Sub

When it comes time to retrieve the saved coordinates, the acbRestoreSize procedure retrieves the four coordinates from the registry and then, if the width and the height of the new form would be greater than 0, resizes the form. Its source code is:

Public Sub acbRestoreSize(frm As Form)

Dim rct As acbTypeRect

Dim lngWidth As Long

Dim lngHeight As Long

rct.lngX1 = GetSetting(acbcRegTag, frm.Name, acbcRegLeft, 0)

rct.lngX2 = GetSetting(acbcRegTag, frm.Name, acbcRegRight, 0)

rct.lngY1 = GetSetting(acbcRegTag, frm.Name, acbcRegTop, 0)

rct.lngY2 = GetSetting(acbcRegTag, frm.Name, acbcRegBottom, 0)

lngWidth = rct.lngX2 - rct.lngX1

lngHeight = rct.lngY2 - rct.lngY1

' No sense even trying if both aren't greater than 0.

If (lngWidth > 0) And (lngHeight > 0) Then

' You would think the MoveSize action would work here, but that

' requires actually SELECTING the window first. That seemed like

' too much work, when this procedure will move/size ANY window.

' Also, MoveSize must DISPLAY the window before it can move it.

' It looked quite ugly.

acb_apiMoveWindow frm.Hwnd, _

rct.lngX1, rct.lngY1, lngWidth, lngHeight, True

End If

End Sub

You may want to store properties other than the size and location of the form—for instance, the current record number for a bound form, or which control was last selected. In any case, the example in 02-10.MDB stores information in such a way that you can store as many properties as you would like by adding to the group describing each form in the registry.

See Also


For more examples using the Windows API, see Chapter 11.

2.11. Open Multiple Instances of a Form


Problem


In an application, you have a form showing information about a customer. You would like to be able to open another copy of the form so you could move to a different row, compare values, perhaps copy from one row to another, or just look at more than one customer's record at once. As far as you can tell, you can have only one open copy of a form at a time.

Solution


In older versions of Access, you were limited to having only a single copy of a form open at any time. Starting with Access 95, you can open multiple instances of a form, under complete program control. There's no user interface for this functionality, however, so you must write code to make it happen. This solution demonstrates how to create, handle,

Return Main Page Previous Page Next Page

®Online Book Reader