Bind class properties to controls

hbrems

has no clue...
Local time
Today, 19:31
Joined
Nov 2, 2006
Messages
181
Dear all,

I know that in Access you can bind the value of one control to the value of another control. For example:

Code:
txtFirstName.ControlSource = lstEmployees.Column(1)

Is there a way to do the same for class objects. In other words, can I bind the properties of an object to a control. Something like this:

Code:
private objEmployee as clsEmployee

Private Sub Form_Load()
    ' Create employee
    Set objEmployee = New clsEmployee
    objEmployee.FirstName = "John"
    
    ' Bind to control
    txtFirstName.ControlSource = objEmployee.FirstName
End Sub
 
objEmployee is a variable of type clsEmployee. You can't call any form of variables in the Control Source property of a control. Only functions are allowed.
 
Maybe so…

clsEmployee Class Module: -
Code:
Option Explicit
Option Compare Text

Private strFirstName As String


Public Property Let FirstName(strFName As String)
    strFirstName = strFName
End Property


Public Property Get FirstName() As String
    FirstName = strFirstName
End Property

Behind a Form: -
Code:
Option Explicit
Option Compare Text


Public objEmployee As clsEmployee


Private Sub Form_Current()

    CheckCurrentRecord
    
End Sub


Private Sub Form_Load()

    [color=green]' Create employee[/color]
    Set objEmployee = New clsEmployee
    
    [color=green]' Bind to control[/color]
    txtFirstName.ControlSource = "=objEmployee.FirstName"
    
End Sub


Private Sub CheckCurrentRecord()

    On Error Resume Next
    
    With objEmployee
        Select Case Me.CurrentRecord
            Case 1
                .FirstName = "John"
            
            Case 2
                .FirstName = "Mary"
                
            Case Else
                .FirstName = "Haven't a clue"
        
        End Select
    End With
    
    If (Err.Number) Then
        MsgBox "Form may not be bound..."
    End If

End Sub

Chris.
 

Users who are viewing this thread

Back
Top Bottom