【问题标题】:Creating checkboxes dynamically from array VBA从数组 VBA 动态创建复选框
【发布时间】:2019-01-26 20:42:46
【问题描述】:

我有一个用户表单如下:

当我选择我想要的数据库并单击选择时,它会将我带到另一个页面:

所有这些复选框都是根据 sql 查询的输出创建的。

在本例中,SQL 查询为:

ID            NAME
8             AI_EQ
14            BB_EQ
19            DM_EQ
22            GD_EQ

等等……

我在选择按钮中添加如下复选框(来自记录集rst):

'creating checkboxes    
If Not rst.EOF And Not rst.BOF Then
       rst.MoveFirst
        i = 0
        Do
      With MultiPage1.Pages(1).Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
.Top = yPos
.Left = 7
.Caption = rst![name]
.Width = 450
        .Height = 24
        .WordWrap = True
        .Value = False
        yPos = yPos + 17
  i = i + 1
      rst.MoveNext
End With          
            Loop Until rst.EOF
End If
 rst.Close
 cnn.Close  
Set rst = Nothing
Set cnn = Nothing

然后,我希望“完成”按钮考虑用户选择的任何复选框。例如,如果他选择前两个,我会想要“8,14”ID,这样我就可以将它们添加到新的 SQL 查询中。

我想我必须创建一个数组来实现这一点,而不仅仅是拥有rst![name]。但是我创建它的尝试失败了,即使我拥有正确的数组,我也不知道如何调整它以使其在选择 NAME 时返回 ID。

这就是我尝试创建数组的方式(但它没有返回任何内容)

 Dim MyArray() As Variant
For i = 0 To rst.RecordCount
MyArray = rst.GetRows(i)
rst.MoveNext
Next

任何提示/帮助将不胜感激。

【问题讨论】:

  • 如果要添加到数组中,则需要Redim Preserve 数组并添加到该数组中,因为您循环遍历要添加到数组中的项目。跨度>

标签: arrays vba excel checkbox


【解决方案1】:

正如提到的悉达多:

  'creating checkboxes    

If Not rst.EOF And Not rst.BOF Then
    rst.MoveFirst
    i = 0
    Do
        With MultiPage1.Pages(1).Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
            .Top = yPos
            .Left = 7
            .Caption = rst![name]
            .Width = 450
            .Height = 24
            .WordWrap = True
            .Value = False
            yPos = yPos + 17
            .Tag = rst![ID]
            i = i + 1
            rst.MoveNext
        End With
    Loop Until rst.EOF
End If
rst.Close
cnn.Close  
Set rst = Nothing
Set cnn = Nothing

【讨论】:

    【解决方案2】:

    由于复选框是动态创建的,请执行此操作。

    1. 遍历所有控件并检查它们是否为复选框
    2. 检查是否已检查
    3. 检查.Tag 中是否有内容并将其存储在变量中。

    代码

    Private Sub CommandButton1_Click()
        Dim cCont As Control
        Dim cbString As String
    
        For Each cCont In Me.Controls
            If TypeName(cCont) = "CheckBox" Then
                If cCont.Value = True Then
                    If cCont.Tag <> "" Then
                        If cbString = "" Then
                            cbString = cCont.Tag
                        Else
                            cbString = cbString & "," & cCont.Tag
                        End If
                    End If
                End If
            End If
        Next cCont
    
        Debug.Print cbString
    End Sub
    

    但要使其正常工作,当您创建复选框时,请将 .Tag 属性设置为 ID 值。

    With MultiPage1.Pages(1).Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
        '
        '~~> Rest of the code
        '
        .Tag = rst![ID]
        '
        '~~> Rest of the code
        '
    End With
    

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 2016-06-16
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多