Shamil Salakhetdinov
e-mail:
Written in September, 1998
Originally published on http://www.wji.com/access
in September 1998
(www.wji.com/access is down now)
This article provides an introductory information on how to implement simple DEEP-objects in MS Access 97. DEEP-object is an instance of a custom class bound on run-time to the events of forms, reports and/or their controls. DEEP-object is a way to implement “codeless” yet flexibly customizable forms and reports, create highly reusable inter-application generic and specific custom classes/object code repositories, design, develop and simulate visual layer OLE-servers in MS Access and easily convert/transform them into VB ActiveX DLLs and much more…
This article presents the following topics:
DEEP-object uses WithEvents-feature of MS Access 97.
This feature is poorly described in MS Access 97's online help and technical
publications. First public announcement/example of using WithEvents-feature
with MS Access 97 visual layer objects I know can be found using this
URL (Access-L discussion list archive):
http://peach.ease.lsoft.com/scripts/wa.exe?A2=ind9807D&L=access-l&P=R831
This was a key information I used to find DEEP-object concept as a result of the discussion:
http://peach.ease.lsoft.com/scripts/wa.exe?A2=ind9807C&L=access-l&P=R21471
http://peach.ease.lsoft.com/scripts/wa.exe?A2=ind9807C&L=access-l&P=R21833
http://peach.ease.lsoft.com/scripts/wa.exe?A2=ind9807C&L=access-l&P=R26661
Code Behind Form – AKA CBF – is a class module bound to form or report. Since MS Access 2.0 CBF was primarily used to write a code processing events of forms, reports and their controls. MS Access 97 introduced the possibility to create custom class modules separated from forms and reports and extended the concept of CBF allowing to use it to create true custom objects with public properties (get, let and set), methods, encapsulated data and visual layer mock-up – form or report layout.
CBF is a great concept/feature but it has an obvious limitation – programmer has to bind event processing code to the events on design time. This limitation leads to the usage of code templates and trivial but not easy additional work programmer/utility have to do in the case of template code modifications.
In this section you'll find sample "standard" CBF which processes form's and its combo-box's events to navigate in bound form. To use this code you have to:
Private Sub Form_Load()
Form.KeyPreview = True
End Sub
Private Sub Form_Current()
FormContextSynchronize
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
FormContextSynchronize
End Sub
Private Sub Form_AfterUpdate()
FormContextSynchronize
End Sub
Private Sub cboEmployee_AfterUpdate()
With Form.RecordsetClone
.FindFirst "[EmployeeId] = " & Form![cboEmployee]
If Not .NoMatch Then
Form.Bookmark = .Bookmark
End If
End With
End Sub
Private Sub FormContextSynchronize()
Dim strEditMode As String
' Set edit mode to show it in form's caption
If Form.NewRecord Then
strEditMode = "(New) "
ElseIf Form.Dirty Then
strEditMode = "(Edit) "
Else
strEditMode = "(View) "
End If
' Set form caption to Employee:
Form.Caption = strEditMode & "Employee: " & _
Form![LastName] + _
" " + Form![FirstName]
' Synchronize combo-box with the current record
With Form![cboEmployee]
.Requery
.Value = Form![EmployeeID]
End With
' Disable navigation combo-box for dirty form
With Form![cboEmployee]
If Form.Dirty Then
.BackColor = 12632256
.Enabled = False
Else
.BackColor = 16777215
.Enabled = True
End If
End With
End Sub
Note: All the code in this article is written without error-handling etc. bullet-proofing techniques to not loose the main subject – DEEP-objects – behind dozen of code lines. Non-traditional usage of Form default object variable in CBF instead of standard Me object variable isn't necessary but it saves you time when you move CBF into custom class module – you can easily find other similar techniques.
Private WithEvents Form As Form
Private WithEvents cboEmployee As ComboBox
Public Sub BindMe(ByRef rfrm As Form)
Set Form = rfrm
With Form
.OnLoad = "[Event Procedure]"
.OnCurrent = "[Event Procedure]"
.OnKeyUp = "[Event Procedure]"
.AfterUpdate = "[Event Procedure]"
Set cboEmployee = ![cboEmployee]
cboEmployee.AfterUpdate = "[Event Procedure]"
End With
End Sub
Public pcol As New Collection
Public Function BindMe(ByRef rfrm As Form)
Dim objDEEP As New clsFrmEmployee
objDEEP.BindMe rfrm
pcol.Add objDEEP
End Function
Private Sub Class_Initialize()
Set Form = New Form_frmEmployeeNoCBF2
With Form
.OnCurrent = "[Event Procedure]"
.KeyPreview = True
.OnKeyUp = "[Event Procedure]"
.AfterUpdate = "[Event Procedure]"
Set cboEmployee = ![cboEmployee]
cboEmployee.AfterUpdate = "[Event Procedure]"
.Visible = True
End With
Form_Current
End Sub
This article seems to be just an introduction to a lot of articles, discussions, sample code, applications and even books which can be written to investigate/use DEEP-object concept. Here are some obvious application areas of it:
DEEP-objects can be used in VB 5.0 too but this is a little bit another story/code.
I don't think that DEEP-object concept is an absolutely new concept for Object Oriented and Event-driven programming in general but it seems to be a brand-new concept for VBA (at least MS Access 97 VBA) programming. Please correct me if I'm wrong.
Click to download sample database (deepcnpt.zip - 29286 bytes)
| HOME |
Copyright (c) 1998,1999 by Shamil Salakhetdinov.
|
| Last updated: June 4, 1999 |