Classes

G

Guest

Well, after all these years I am finally trying to learn classes. But, I
must be missing something.

IN MY CLASS MODULE, I HAVE
Private mlngMemberID As Long
Public txtLastName As String

Public Property Get MemberID() As Long 'Read only

MemberID = mlngMemberID

End Property

Public Function Load(ID As Long) As Boolean
Dim rst As DAO.Recordset
Dim SQL As String
Dim strMsg As String

Load = False

SQL = "SELECT * FROM tblMembers "
SQL = SQL & " WHERE ([MemberID]) = " & ID

Set rst = CurrentDb().OpenRecordset(SQL, dbOpenSnapshot)
With rst
If rst.RecordCount = 0 Then
MsgBox "Cannot find Member with ID = " & ID, vbCritical
End If

Me.txtLastName = CheckNull(![LastName])

Set rst = Nothing

Load = True

Exit Function

IN MY FORM MODULE, I HAVE
Private mobjMember As New clsMember

Private Sub Form_Open(Cancel As Integer)

Set mobjMember = New clsMember

mobjMember.Load (Form_frmSEGSelect!lstSelect.Column(0))

End Sub


WHen the form is displayed, the txtLastName field is still empty.
If I type in the immediate window
? mobjMember.txtLastName
I get the correct value.

What am I missing? I certainly hope that for each field on the form (over
60), I do not have to write
me.txtFIELD = mobjMember.txtFIELD

Thanks
 
M

Marshall Barton

Shell said:
Well, after all these years I am finally trying to learn classes. But, I
must be missing something.

IN MY CLASS MODULE, I HAVE
Private mlngMemberID As Long
Public txtLastName As String

Public Property Get MemberID() As Long 'Read only

MemberID = mlngMemberID

End Property

Public Function Load(ID As Long) As Boolean
Dim rst As DAO.Recordset
Dim SQL As String
Dim strMsg As String

Load = False

SQL = "SELECT * FROM tblMembers "
SQL = SQL & " WHERE ([MemberID]) = " & ID

Set rst = CurrentDb().OpenRecordset(SQL, dbOpenSnapshot)
With rst
If rst.RecordCount = 0 Then
MsgBox "Cannot find Member with ID = " & ID, vbCritical
End If

Me.txtLastName = CheckNull(![LastName])

Set rst = Nothing

Load = True

Exit Function

IN MY FORM MODULE, I HAVE
Private mobjMember As New clsMember

Private Sub Form_Open(Cancel As Integer)

Set mobjMember = New clsMember

mobjMember.Load (Form_frmSEGSelect!lstSelect.Column(0))

End Sub


WHen the form is displayed, the txtLastName field is still empty.
If I type in the immediate window
? mobjMember.txtLastName
I get the correct value.

What am I missing? I certainly hope that for each field on the form (over
60), I do not have to write
me.txtFIELD = mobjMember.txtFIELD


Hope is not an effective coding style :)

If you don't set the text box's Value, how else can a it
figure out what it is supposed to display? When you use an
unbound form, it is inherent that you must use code to
manage the value in each control.

Note that you need to get rid of the New keyword in the
module level decalartion of clsMember. It should be:

Private mobjMember As clsMember

Also, you should NOT use the Form_frmSEGSelect style of
reference to your form. The correct reference is:
Forms!frmSEGSelect
 
S

storrboy

A few more things to add to Mr. Barton's response that may help in the
future:

1) Load is a VB statement as in 'Load [object]'. Naming the function
Load may become an issue
2) There is no End With line in the Load function

3) If nothing is found in the table, the function should end or
account for the no records. You have it continuing to reference the
recordset after the message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top