Ms access Access 2010 VBA中的循环
我是Access和VBA新手,我一直在寻找如何在VBA中进行循环。我在一份报告上画了8个方框,看起来一切都很好,但我想知道我是否可以使用循环创建下面的代码来缩短它:Ms access Access 2010 VBA中的循环,ms-access,vba,Ms 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 '********
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
我建议使用循环“缩短”代码,即使用数组存储此信息,然后使用公式执行计算以生成/存储值
下面是一些“几乎”的代码插入值或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
的函数,在屏幕显示的上下文中并不总是可能的。当我遇到这种情况时,我会创建循环,然后逐项列出另一个代码块,在明确定义每个分配的情况下,该代码块的注释和维护更加清晰。您可以使用数组:
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
更高级的方法是将所有名称和其他变量保存在一个表中,然后在循环中读取该表。我正在尝试您的代码。它似乎不起作用。我把责任推到我身上,就像我说我不熟悉VBATry这个简单的例子:
Dim boxInfo(1,1)As Integer For I As Integer=0到1 boxInfo(I,0)=1+I boxInfo(I,1)=2*I下一步
为了确保分配工作正常,您可以始终执行msgbox boxInfo(0,0)
最后。非常感谢汤姆,这起作用了,谢谢你约瑟夫的帮助