在Access 2010 VBA中循环

时间:2014-08-25 19:49:54

标签: ms-access access-vba

我是Access和VBA的新手,我一直在搜索如何在VBA中进行循环。我在报告上绘制了8个框,一切看起来不错,但我想知道是否可以使用循环创建下面的代码来缩短它:

  Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)

  Dim X1 As Single, Y1 As Single
  Dim X2 As Single, Y2 As Single
  Dim Color As Long
  '************************************
  Dim page_start_left_1 As Integer
  Dim page_start_top_1 As Integer
  Dim page_end_left_1 As Integer
  Dim page_end_top_1 As Integer
  Dim page_end_Width_1 As Integer
  Dim page_end_Height_1 As Integer
  '****
  Dim page_start_left_2 As Integer
  Dim page_start_top_2 As Integer
  Dim page_end_left_2 As Integer
  Dim page_end_top_2 As Integer
  Dim page_end_Width_2 As Integer
  Dim page_end_Height_2 As Integer
  '****
  Dim page_start_left_3 As Integer
  Dim page_start_top_3 As Integer
  Dim page_end_left_3 As Integer
  Dim page_end_top_3 As Integer
  Dim page_end_Width_3 As Integer
  Dim page_end_Height_3 As Integer
  '****
  Dim page_start_left_4 As Integer
  Dim page_start_top_4 As Integer
  Dim page_end_left_4 As Integer
  Dim page_end_top_4 As Integer
  Dim page_end_Width_4 As Integer
  Dim page_end_Height_4 As Integer
  '************************************
  ' Set to 1st question on page 1
  page_start_left_1 = Me!Q1text.Left
  page_start_top_1 = Me!Q1text.Top
  ' Set to last question on page 1
  page_end_left_1 = Me!Q7text.Left
  page_end_top_1 = Me!Q7text.Top
  page_end_Width_1 = Me!Q7text.Width
  page_end_Height_1 = Me!Q7text.Height
  ' Set to 1st question on page 2
  page_start_left_2 = Me!Q8text.Left
  page_start_top_2 = Me!Q8text.Top
  ' Set to last question on page 2
  page_end_left_2 = Me!Q27text.Left
  page_end_top_2 = Me!Q27text.Top
  page_end_Width_2 = Me!Q27text.Width
  page_end_Height_2 = Me!Q27text.Height
  ' Set to 1st question on page 3
  page_start_left_3 = Me!Q28text.Left
  page_start_top_3 = Me!Q8text.Top
  ' Set to last question on page 3
  page_end_left_3 = Me!QBP3text.Left
  page_end_top_3 = Me!QBP3text.Top
  page_end_Width_3 = Me!QBP3text.Width
  page_end_Height_3 = Me!QBP3text.Height
  ' Set to 1st question on page 4
  page_start_left_4 = Me!QADHD1text.Left
  page_start_top_4 = Me!QADHD1text.Top
  ' Set to last question on page 4
  page_end_left_4 = Me!QCoOccur4text.Left
  page_end_top_4 = Me!QCoOccur4text.Top
  page_end_Width_4 = Me!QCoOccur4text.Width
  page_end_Height_4 = Me!QCoOccur4text.Height
  '************************************************* *******************
  '1st Page left box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_1 + 7000
  Y1 = page_start_top_1 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_1 + page_end_Width_1 + 2900
  Y2 = page_end_top_1 + page_end_Height_1
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B
  '1st Page right box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_1 + 10130
  Y1 = page_start_top_1 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_1 + page_end_Width_1 + 5460
  Y2 = page_end_top_1 + page_end_Height_1
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B

  '************************************************* *******************
  '2st Page left box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_2 + 7000
  Y1 = page_start_top_2 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_2 + page_end_Width_2 + 2900
  Y2 = page_end_top_2 + page_end_Height_2
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B
  '2st Page right box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_2 + 10130
  Y1 = page_start_top_2 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_2 + page_end_Width_2 + 5460
  Y2 = page_end_top_2 + page_end_Height_2
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B
  '************************************************* *******************
  '3rd Page left box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_3 + 7000
  Y1 = page_start_top_3 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_3 + page_end_Width_3 + 2900
  Y2 = page_end_top_3 + page_end_Height_3
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B
  '3rd Page right box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_3 + 10130
  Y1 = page_start_top_3 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_3 + page_end_Width_3 + 5460
  Y2 = page_end_top_3 + page_end_Height_3
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B

  '*********************************************************************
  '4th Page left box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_4 + 7000
  Y1 = page_start_top_4 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_4 + page_end_Width_4 + 2900
  Y2 = page_end_top_4 + page_end_Height_4
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B
  '4th Page right box
  ' X and Y coordinates for the top left corner of the box.
  X1 = page_start_left_4 + 10130
  Y1 = page_start_top_4 - 100
  ' X and Y coordinates for the bottom right corner of the box.
  X2 = page_end_left_4 + page_end_Width_4 + 5460
  Y2 = page_end_top_4 + page_end_Height_4
  Me.DrawWidth = 3 ' Width of the line (in pixels).
  Color = RGB(0, 0, 0) ' Use black line color.
  ' Draw the rectangle with the Line method.
  Me.Line (X1, Y1)-(X2, Y2), Color, B

  End Sub

2 个答案:

答案 0 :(得分:0)

我建议"缩短"带循环的代码是使用数组来存储这些信息,然后用公式执行计算以生成/存储值。

以下是一些'几乎'代码 - 插入X,Y和公式的值或声明。

Dim boxInfo(X, Y) As Short
For i As Integer = 0 To X 
   boxInfo(i,0) = formula0
   boxInfo(i,1) = formula1
   .
   .
   .
   boxInfo(i,Y) = formulaY
Next

您可以将X替换为您已定义的框数,将Y替换为您已定义的变量数。

这里的想法是公式是i的某些功能,这在屏幕显示的上下文中并不总是可行的。当我遇到这种情况时,我会创建循环,然后逐项列出另一个代码块,在明确定义每个赋值的情况下,这个代码块更清晰,可维护。

答案 1 :(得分:0)

您可以使用数组:

Dim page_start_left(8) As Integer
Dim Q(8) as string
Q(1) = "Q1text"
Q(2) = "Q7text"
...
For I = 1 to 8
  page_start_left(I).Left = me.controls(Q(I)).Left
  ...
next I

更高级的方法是保存所有姓名&表中的其他变量,然后只是在循环中读取该表。