我尝试使用VBA进行循环,并检查不同的Access字段(名为" Areas"),如果它们是True或False。
如果三个区域为True,我的Sub应返回其名称(即" AreaA,AreaF,AreaG")。
问题是,我收到错误:" 编译错误。类型不匹配。"在下面显示的行和第二个" & "突出显示。
Set chkObject = "[Forms]![1110 Datos]![Area_" & Area(i) & "]"
这些是一些现存的对象(复选框):
[表格]![1110 Datos]![Area_Cnc]
[表格]![1110 Datos]![Area_Emb]
[表格]![1110 Datos]![Area_Ens]
这是我的代码:
Sub MySub()
Dim Area() As String, Vars As String, i As Integer, chkObject As Object
Dim strAreas As String
Vars = "Cnc, Emb, Ens, Esp, Fer, For, Maq, Pin, Pon, Sie, Sol, Tel, Rou"
Area = Split(Vars, ", ")
strAreas = "" 'Set blank.
For i = 0 To UBound(Area)
Set chkObject = "[Forms]![1110 Datos]![Area_" & Area(i) & "]" 'Reference to the Object.
If chkObject.Value = True Then
If strAreas = "" Then 'Check if it is the first Area: "Cnc" OR "Esp"...
strAreas = Area(i)
Else
strAreas = strAreas & ", " & Area(i) 'To add commas if already exists one checked: "Cnc, Esp, Sie"
End If
End If
If strAreas = "" Then 'If no Areas were checked...
Debug.Print "Ningún area estaba activa."
Else
Debug.Print strAreas 'Else, return selected Areas
End If
Next
End Sub
有谁知道如何引用它?
提前致谢!!
答案 0 :(得分:0)
这是工作代码,谢谢Tim:
Sub MySub()
Dim Area() As String, Vars As String, i As Integer, chkObject As Object
Dim strAreas As String
Vars = "Cnc, Emb, Ens, Esp, Fer, For, Maq, Pin, Pon, Sie, Sol, Tel, Rou"
Area = Split(Vars, ", ")
strAreas = ""
For i = 0 To UBound(Area)
Set chkObject = Forms("1110 Datos").Controls("Area_" & Area(i))
If chkObject.Value = True Then
If strAreas = "" Then
strAreas = Area(i)
Else
strAreas = strAreas & ", " & Area(i)
End If
End If
Next
If strAreas = "" Then
Debug.Print "Ningún area estaba activa."
Else
Debug.Print strAreas
End If
End Sub