我最近遇到一个问题,我打开一个连续对话框,显示从1到X的任何记录,X通常在2-5范围内。 我发现非常烦人的是,Access2010的默认设置使continue表单占据了整个屏幕高度,即使只有很少的记录也是如此。
因此,为了解决该问题,我想出了一些代码来使窗口大小与记录数量大致匹配,并能够指定要显示的最大记录数量。 如果记录量超过了最大值,我还将使用代码来修改滚动条设置,以便仅当记录数超过最大值时才显示滚动条。
代码如下所示,我将其放置在表单的Load事件中:
Private Sub Form_Load()
'Variables
Dim lngCount As Long
Dim lngWindowHeight As Long
Dim lngOldWindowHeight As Long
Dim lngDeltaTop As Long
Dim lngMaxRecordsToShow As Long
lngMaxRecordsToShow = 10
'Find the amount of records in form
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
If Not rs.EOF Then rs.MoveLast
lngCount = rs.RecordCount
'Check whether there are more then Max amount of records
If lngCount > lngMaxRecordsToShow Then
lngCount = lngMaxRecordsToShow
'Enable vertical scrollbar
Me.ScrollBars = 2 'Vertical
Else
'Disable scrollbars
Me.ScrollBars = 0 'None
End If
'Calculate new windowheight.
'If you do not have a header/footer, or they are not visible adjust accordingly
lngWindowHeight = Me.FormHeader.Height + _
Me.Detail.Height * lngCount + _
Me.FormFooter.Height + _
400
'The 567 is to account for title bar Height.
'If you use thin border, adjust accordingly
'The form will be "shortened" and we need to adjust the top property as well to keep it properly centered
lngOldWindowHeight = Me.WindowHeight
lngDeltaTop = (lngOldWindowHeight - lngWindowHeight) / 2
'Use the move function to move the form
Me.Move Me.WindowLeft, Me.WindowTop + lngDeltaTop, , lngWindowHeight
'Cleanup
Set rs = Nothing
End Sub
我用:
AutoRezise =真
AutoCenter =真
这段代码。 如果不这样做,可能会出现奇怪的结果,但是您可以自己进行测试。