I have a class for a Subform; trying to encapsulate all relevant things. The MouseUp event (assigned to the class) deletes & builds a relevant right-click context menu. But if I place a Public Function InsertPageFromABill()
within the class to handle a button in the Command Bar it is not visible to the Subform. Seems you cannot reference a class' methods in the CmdBar.OnAction property with clsName.FunctionName. I am trying not build lots of classes & trying to encapsulate the relevant things to this Subform class ideally.
If I put the function in a normal module it works fine, but this does not seem the right thing to do (should encapsulate relevant things inside the class).
What is the proper encapsulation methodology here? I could build another class for the command bar but seems too messy with unnecessary classes for the sake of creating classes. Could build another class and reference the function and gain access from there but again this seems superfluous? I would think declaring the function as Friend is the way but does not seem to work.
SubForm
clsBillOrPage
RightClickStuffModule - Though should be in the class I think this may be a minor exception where no point in writing this generic procedure all over the place it will be used all over the place with the exception to the Function InsertPageFromABill (this is the function I want to encapsulate as it is not generic and specific to the class.
within the class to handle a button in the Command Bar it is not visible to the Subform. Seems you cannot reference a class' methods in the CmdBar.OnAction property with clsName.FunctionName. I am trying not build lots of classes & trying to encapsulate the relevant things to this Subform class ideally.
If I put the function in a normal module it works fine, but this does not seem the right thing to do (should encapsulate relevant things inside the class).
What is the proper encapsulation methodology here? I could build another class for the command bar but seems too messy with unnecessary classes for the sake of creating classes. Could build another class and reference the function and gain access from there but again this seems superfluous? I would think declaring the function as Friend is the way but does not seem to work.
SubForm
Code:
'+Init & Demo:
Private Sub Form_Load()
Dim ctrl As Control
For Each ctrl In Me.Detail.Controls
If ctrl.ControlType = acTextBox Then
Set mObj = New clsBillOrPage
mObj.fInit ctrl
mColTenderBillsAndPages.Add mObj
End If
Next ctrl
End Sub
Private Sub Form_Unload(Cancel As Integer)
For Each mObj In mColTenderBillsAndPages
mObj.Dispose
Next mObj
End Sub
clsBillOrPage
Code:
Public WithEvents mTb As TextBox
Private mParentFrm As Form
Public Enum eBillOrPage
Bill = 1
Page = 2
End Enum
Public Function fInit(ctl As Control, Optional lParentFrm As Form) As clsBillOrPage
Set mParentFrm = lParentFrm
Set mTb = ctl
mTb.OnMouseUp = "[Event Procedure]"
Set fInit = Me
End Function
Public Function Dispose()
Set mTb = Nothing
Set mParentFrm = Nothing
End Function
Private Sub mTb_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
Dim strBarName As String
strBarName = "dalsBillPageRcMenu"
Dim Result As String
Dim lConcatID As String
lConcatID = Screen.ActiveControl.Parent.CONCAT_ID
If Button = vbKeyRButton Then
'Build Empty CmdBar
Dim lBar As CommandBar
Set lBar = ResetCustomBar("dalsBillOrPageRcMenu")
'Assign cmdBar to frm
Debug.Print Screen.ActiveControl.Parent.name
Screen.ActiveControl.Parent.ShortcutMenuBar = "dalsBillOrPageRcMenu"
'Is current item Bill/ Page?
Result = IsItBillOrPage(lConcatID)
If Result = Bill Then
'It's a Bill; add Bill btns
CmdBarAddBtnsBill lBar
ElseIf Result = Page Then
'It's a Page; add Page btns
MsgBox "p"
Else
MsgBox "Dunno"
End If
End If
End Sub
Public Function IsItBillOrPage(lConcatenatedID As String) As eBillOrPage
If Left(lConcatenatedID, 4) = "Bill" Then
IsItBillOrPage = Bill
ElseIf Left(lConcatenatedID, 4) = "Page" Then
IsItBillOrPage = Page
Else
MsgBox "Unaccounted record source type; only accounting for Bill/ Page"
End If
End Function
'************************************************************************************************
'+ RIGHT-CLICK MENU (add btn's):
'SELECTED ITEM IS BILL:
Public Function CmdBarAddBtnsBill(theCmdBar As CommandBar)
With theCmdBar.Controls.Add(msoControlButton)
.Caption = "+ Page"
.OnAction = "=InsertPageFromABill()"
End With
With theCmdBar.Controls.Add(msoControlButton)
.Caption = "Edit Page"
.OnAction = "=EditThePage()"
End With
End Function
RightClickStuffModule - Though should be in the class I think this may be a minor exception where no point in writing this generic procedure all over the place it will be used all over the place with the exception to the Function InsertPageFromABill (this is the function I want to encapsulate as it is not generic and specific to the class.
Code:
'If menu exists; del & build new one (no exists fn)
Public Function ResetCustomBar(BarName As String) As Office.CommandBar
On Error Resume Next
On Error GoTo Line1
CommandBars(BarName).Delete
Line1:
Set ResetCustomBar = CommandBars.Add(BarName, msoBarPopup, False, True)
End Function
'If menu exists; del & build new one (no exists fn)
Public Function ResetCustomBar(BarName As String) As Office.CommandBar
On Error Resume Next
On Error GoTo Line1
CommandBars(BarName).Delete
Line1:
Set ResetCustomBar = CommandBars.Add(BarName, msoBarPopup, False, True)
End Function
Public Function InsertPageFromABill()
Dim lBillID As String
lBillID = Screen.ActiveControl.Parent.CONCAT_ID
DoCmd.OpenForm "TenderPageEditF", , , , acFormAdd
End Function
Last edited: