2017-06-13 35 views
0

我在自定義用戶窗體中創建窗體控件,並且在創建後很難創建窗體控件。組合框和文本框的數量取決於用戶輸入。我正在使用CommandButton來確定正確的語法,但無法找到控件。我在CommandButton_Click方法中使用了幾種不同的命名約定,但沒有任何作用。在Excel VBA用戶表單中獲取控件

我的用戶窗體代碼來創建我的控制如下:

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 

任何幫助是極大的讚賞。我的用戶窗體的

屏幕截圖:

My userform

+0

什麼是您收到的錯誤消息? – Alok

+0

@Alok我沒有收到任何錯誤。我根本無法在我爲此創建或設置的新控件中設置值。我無法找到正確的名稱語法。 –

+0

我希望在你點擊CommandButton之前調用sub createDetails。你能證實嗎? – Alok

回答

0

嘗試使用

frmInvoice.Controls("amtBox1").Text 

,而不是

frmInvoice.Controls("amtBox1").Value 
+0

這沒有奏效。 –

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 

enter image description here

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 
+0

我從來沒有在同一時間使用所有四個。我評論了三個,並一次嘗試一個。沒有任何工作。 –

+0

在我的測試中,首先是做得很好。 –

+0

我感到困惑。我必須有一些代碼影響我訪問文本框的能力。它仍然不適合我。 –