我在自定义用户表单中创建表单控件,并且在创建后很难访问它们。 ComboBoxes和TextBoxes的数量取决于用户输入。我正在使用CommandButton来计算正确的语法,但是找不到控件。我在CommandButton_Click方法中使用了几种不同的命名约定,但没有任何作用。
我创建控件的userform代码如下:
Sub createDetails()
Dim details As Variant
details = TextBox3.Value
remainTot = TextBox2.Value
If TextBox3.Value = "" Or TextBox3.Value = 0 Then
MsgBox "Must have at least 1 detail"
Exit Sub
Else
End If
For i = 1 To details
n = i - 1
Dim SubPay As Control
Dim CatPay As Control
Dim AmtPay As Control
Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "lbl_" & i, True)
With theLbl
.Caption = "Detail " & i
.Left = 20
.Width = 60
.Top = n * 24 + 110
.Font.Size = 10
End With
Set SubPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "SubComboBox_" & i, True)
With SubPay
.Top = 108 + (n * 24)
.Left = 60
.Height = 18
.Width = 100
.Name = "subBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 6
.TabStop = True
.RowSource = "PayeeList"
End With
Set CatPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "CatComboBox_" & i, True)
With CatPay
.Top = 108 + (n * 24)
.Left = 165
.Height = 18
.Width = 100
.Name = "catBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 7
.TabStop = True
.RowSource = "CatList"
End With
Set AmtPay = frmInvoice.Controls.Add("Forms.TextBox.1", "AmtTextBox" & i, True)
With AmtPay
.Top = 108 + (n * 24)
.Left = 270
.Height = 18
.Width = 50
.Name = "amtBox" & i
.Font.Size = 10
.TabIndex = n * 3 + 8
.TabStop = True
End With
Next i
Dim TBox As Control
Set TBox = frmInvoice.Controls.Add("Forms.TextBox.1", "TotalLbl", True)
With TBox
.Top = 130 + ((details - 1) * 24)
.Left = 270
.Height = 18
.Width = 50
.Name = "totBox"
.Font.Size = 10
'.TabIndex = (details - 1) * 3 + 9
.TabStop = False
.Value = TextBox2.Value
End With
Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "totLbl", True)
With theLbl
.Caption = "Total"
.Left = 225
.Width = 40
.Top = 135 + ((details - 1) * 24)
.Font.Size = 10
End With
frmInvoice.Height = 200 + details * 24
With CommandButton1
.Top = 150 + details * 24
.TabStop = True
.TabIndex = (details - 1) * 3 + 9
End With
With CommandButton2
.Top = 150 + details * 24
.TabStop = False
'.TabIndex = (details - 1) * 3 + 10
End With
End Sub
无效的CommndButton代码是:
Private Sub CommandButton1_Click()
frmInvoice.Controls("amtBox1").Value = 1
frmInvoice.Controls(amtBox1).Value = 2
frmInvoice.Controls(AmtTextBox1).Value = 3
frmInvoice.Controls("AmtTextBox1").Value = 4
End Sub
非常感谢任何帮助。
我的用户形式的屏幕截图:
答案 0 :(得分:0)
尝试使用
frmInvoice.Controls("amtBox1").Text
而不是
frmInvoice.Controls("amtBox1").Value
答案 1 :(得分:0)
我认为你的错误是因为
Private Sub CommandButton1_Click()
frmInvoice.Controls("amtBox1").Value = 1 'is true
frmInvoice.Controls(amtBox1).Value = 2 'is false because there isn't double quotes
frmInvoice.Controls(AmtTextBox1).Value = 3 'is false because there isn't dobule quotes
frmInvoice.Controls("AmtTextBox1").Value = 4 'is false the AmtTextBoxt1 name is changed "amtBox1"
End Sub
Maby,你想要这个吗?
Private Sub CommandButton1_Click()
Dim i As Integer
Dim total As Currency
With frmInvoice
For i = 1 To TextBox3.Value
total = total + .Controls("amtBox" & i).Value
Next i
.Controls("totBox").Text = Format(total, "####.00")
End With
End Sub