前提・実現したいこと
ユーザーフォームとクラスモジュールを使用して、フォーム上で使用できるカレンダーを作成しました。
カレンダーの日付をクリックすると、指定したテキストボックスにその日付が記入できる仕組みです。
一つのフォームでの呼び出しでは問題なく動作するのですが、その実行コードを残したまま、他のフォームにてカレンダーを起動するとエラーが出てしまいます。
カレンダーの呼び出しを行うフォームが3つあるので、3か所どこから呼び出しても正常に動作してくれるのが理想です。
このエラーが発生する原因と対処法を教えていただきたいです。
よろしくお願いいたします。
発生している問題・エラーメッセージ
実行時エラー'-2147024809(80070057) 指定されたオブジェクトは見つかりません。
カレンダーフォーム(frmCalendar)のコード
コントロールは、コンボボックス2つ(cmbYear, cmbMonth)、スピンボタン1つ(spinbutton1)です。
VBA
1 2Option Explicit 3 4Private arrayClass() As New clsCalendar 5Public entryControl As String 6Public ParentForm As UserForm 7 8Private Function CheckParam() As Boolean 9 'コンボボックスの値が指定されていなければ、False 10 If cmbYear.Value = "" Then 11 CheckParam = False 12 Exit Function 13 End If 14 If cmbMonth.Value = "" Then 15 CheckParam = False 16 Exit Function 17 End If 18 CheckParam = True 19End Function 20 21Private Sub cmbMonth_Change() 22 If CheckParam() Then 23 makeCalendar cmbYear.Value, cmbMonth.Value 24 End If 25End Sub 26 27Private Sub cmbYear_Change() 28 If CheckParam() Then 29 makeCalendar cmbYear.Value, cmbMonth.Value 30 End If 31End Sub 32 33Private Sub SpinButton1_Change() 34 35End Sub 36 37Private Sub SpinButton1_SpinDown() 38 If cmbMonth.Value = 1 Then 39 cmbMonth.Value = 12 40 cmbYear.Value = cmbYear.Value - 1 41 Else 42 cmbMonth.Value = cmbMonth.Value - 1 43 End If 44End Sub 45 46Private Sub SpinButton1_SpinUp() 47 If cmbMonth.Value = 12 Then 48 cmbMonth.Value = 1 49 cmbYear.Value = cmbYear.Value + 1 50 Else 51 cmbMonth.Value = cmbMonth.Value + 1 52 End If 53End Sub 54 55 56Private Sub UserForm_Initialize() 57 Dim lbl As MSForms.Label 58 Dim i As Integer, j As Integer, c As Integer 59 Dim weekdays() As Variant 60 '曜日の設定 61 weekdays() = Array("日", "月", "火", "水", "木", "金", "土") 62 For i = 1 To 7 63 Set lbl = Me.Controls.Add("Forms.Label.1", , True) 64 With lbl 65 .Caption = weekdays(i - 1) 66 .Width = 15 67 .Height = 15 68 .Left = 5 + (.Width + 2) * (i - 1) 69 .Top = 20 70 .BorderColor = &H666666 71 .BorderStyle = fmBorderStyleSingle 72 .Font.Size = 11 73 End With 74 Next 75 c = 1 76 For i = 1 To 6 '縦方向 77 For j = 1 To 7 '横方向 78 Set lbl = Me.Controls.Add("Forms.Label.1", , True) 79 With lbl 80 .Width = 15 81 .Height = 15 82 .Left = 5 + (j - 1) * (.Width + 2) 83 .Top = 20 + i * (.Height + 2) 84 .BorderColor = &H666666 85 .BorderStyle = fmBorderStyleSingle 86 .Font.Size = 11 87 .TextAlign = fmTextAlignRight 88 .Name = "D" & c 89 ReDim Preserve arrayClass(c) 90 arrayClass(c).NewCalendar lbl, Me 91 c = c + 1 92 End With 93 Next 94 Next 95 '3年前から3年後までの西暦をコンボボックスに追加 96 For i = Year(Date) - 3 To Year(Date) + 3 97 cmbYear.AddItem i 98 Next 99 '月をコンボボックスに追加 100 For i = 1 To 12 101 cmbMonth.AddItem i 102 Next 103 'コンボボックスに値を代入すると、Changeイベントが発生する 104 '現在の西暦と月を表示しておく 105 cmbYear.Value = Year(Date) 106 cmbMonth.Value = Month(Date) 107 'Public変数に呼び出し元のデータを格納 108 Set ParentForm = frmCRecordSearch 109End Sub 110 111Sub makeCalendar(Nen As Integer, Tsuki As Integer) 112 Dim i As Integer, c As Integer 113 Dim Weekday_of_firstday As Integer 114 Dim LastDay As Integer 115 Dim MyDate As Date 116 'ラベルの表示をクリア 117 For i = 1 To 42 118 Controls("D" & i).Caption = "" 119 Next 120 c = 1 121 'その日の曜日を数値として取得 122 Weekday_of_firstday = Getweekday1st(Nen, Tsuki) 123 LastDay = Day(DateSerial(Nen, Tsuki + 1, 1) - 1) - 1 124 For i = Weekday_of_firstday To Weekday_of_firstday + LastDay 125 Controls("D" & i).Caption = c 126 Controls("D" & i).Tag = CStr(DateSerial(Nen, Tsuki, c)) 127 Controls("D" & i).ForeColor = vbBlack 128 c = c + 1 129 Next 130 '前月の日付の設定 131 MyDate = DateSerial(Nen, Tsuki, 1) - 1 132 c = Day(MyDate) 133 For i = Weekday_of_firstday - 1 To 1 Step -1 134 Controls("D" & i).Caption = c 135 Controls("D" & i).Tag = CStr(DateSerial(Year(MyDate), Month(MyDate), c)) 136 Controls("D" & i).ForeColor = RGB(100, 100, 100) 137 c = c - 1 138 Next 139 '来月の日付の設定 140 MyDate = DateSerial(Nen, Tsuki + 1, 1) 141 c = 1 142 For i = Weekday_of_firstday + LastDay + 1 To 42 143 Controls("D" & i).Caption = c 144 Controls("D" & i).Tag = CStr(DateSerial(Year(MyDate), Month(MyDate), c)) 145 Controls("D" & i).ForeColor = RGB(100, 100, 100) 146 c = c + 1 147 Next 148End Sub 149 150Private Function Getweekday1st(Nen As Integer, Tsuki As Integer) As Integer 151 '引数の年と月に関して、1日の曜日を数字で取得(日が1、月が2・・・) 152 Dim firstDay As Date 153 firstDay = DateSerial(Nen, Tsuki, 1) 154 Getweekday1st = Weekday(firstDay, vbSunday) 155End Function 156
カレンダーのクラスモジュール(clsCalendar)
VBA
1Option Explicit 2 3Private WithEvents myLbl As MSForms.Label 4Private mForm As UserForm 5 6Public Sub NewCalendar(lbl As MSForms.Label, objForm As UserForm) 7 'オブジェクト指向言語ではコントラクタの役割 8 Set myLbl = lbl 9 Set mForm = objForm 10End Sub 11 12Private Sub myLbl_Click() 13 'myLblのTagプロパティには、日付が代入されている 14 frmCalendar.ParentForm.Controls(frmCalendar.entryControl).Text = myLbl.Tag 15 Unload mForm 16End Sub 17
カレンダーの呼び出しコード
コントロールには日付を入れるテキストボックス(yxyDate1, txtDate2)、カレンダーを表示するボタン(btnDispCalendar1, btnDispCalendar2)です。この呼び出しコードを2つ以上のフォームで行おうとするとエラーが発生してしまいます。
VBA
1Private Sub btnDispCalendar1_Click() 2 frmCalendar.entryControl = "txtDate1" 3 frmCalendar.Show 4End Sub 5 6Private Sub btnDispCalendar2_Click() 7 frmCalendar.entryControl = "txtDate2" 8 frmCalendar.Show 9End Sub
回答1件
あなたの回答
tips
プレビュー