2016-03-23 13 views
0

私は月ごとにプロジェクトのステータスを表示するフォームを作成しました。私は列/コントロールボックスのプロパティを毎月の開始時に一度更新したい(年が進むにつれて現在の月を再表示する)。将来の月にはまだデータがないので、私は次のコードを書いた。可視プロパティを設定する特定の命名構造を持つループスルーコントロール

Public Function updateProgressForm() 

Dim currentMonth As String 
Dim currentYear As Long 
Dim txtDate As String 
Dim todaysmonth As Long 
Dim txtboxMonth As Integer 
Dim txtboxName As String 
Dim txtboxLabel As String 

todaysmonth = Month(Now()) 
currentMonth = MonthName(todaysmonth, 3) 
currentYear = Year(Now()) 
txtDate = currentMonth & " " & currentYear 

DoCmd.OpenForm "IndProgressTracking", acDesign, , , , acWindowNormal 
Forms!IndProgressTracking!Month12.ControlSource = txtDate 
Forms!IndProgressTracking!Month12Label.Caption = txtDate 

For txtboxMonth = 1 To 12 Step 1 
    txtboxName = "Month" & txtboxMonth 
    txtboxLabel = txtboxName & "Label" 
    If IsNull(txtboxName) Then 
    txtboxName.Visible = False 
    txtboxLabel.Visible = False 
    Else 
    txtboxName.Visible = True 
    txtboxLabel.Visible = True 
End If 
Next 

txtboxName = "Month1" 
Forms!IndProgressTracking.Controls!txtboxName.Visible = True 

End Function 

私にエラーを出す部分は、txtboxName.Visibleです。これは無効な修飾子です。したがって、私は最後の2行を代わりに試みましたが、最後の行(終了関数の前に)は、私が(txtboxName)を参照したコントロールを見つけることができないと述べています。私は毎月のテキストボックスとラベルを持っているので、Forループを使用して単純化することを望んでいたコントロール名を使用してそれぞれを個別に呼び出す必要がある場合、それほど大きな問題ではありません。どんな提案も歓迎です!

答えて

2

txtboxNameを制御オブジェクトではなく文字列として定義しました。
はそれの名前を使用してコントロールを参照するには、次の

Forms!IndProgressTracking.Controls(txtboxname).Visible = True 
+1

おかげ@HansUp - 私はいつもそれだけで1行だ場合でも、私のコードをテストし、時には後ろに正しい参照を置くことを忘れてみてください。 –

関連する問題