Home > Archive > Visual Basic > November 2005 > How to reference a form's controls from a class module?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to reference a form's controls from a class module?
|
|
| Carl Imthurn 2005-11-29, 6:55 pm |
| What am I missing about referencing a form control's methods and properties from a class?
Here is the relevant code:
' in the General Declarations section of my form:
Option Explicit
Private ReportInfo As clsReportInfo
' the Load event of my form (edited for relevance):
Private Sub Form_Load()
Set ReportInfo = New clsReportInfo
End Sub
' the click event of the command button
Private Sub cmdRefreshProcedureListBoxes_Click()
ReportInfo.RefreshProceduresFromDatabase Me, lstLocations, dtpBeginningDate, dtpEndingDate
End Sub
I'm passing in to the class' method the following parameters:
1) a form (me)
2) a listbox which will be used to build a "location string" (lstLocations)
3) a beginning date for passing in to a stored procedure
4) an ending date for same
I get an error when the application attempts to reference the listbox from the class module.
' Here's the method that gets called:
Public Sub RefreshProceduresFromDatabase(frm As Form, lstTEST As ListBox, dtStart As Date, dtEnd As Date)
Dim i As Integer
Dim str As String
' build the string of selected locations for passing in to the stored procedure
' THE ERROR HAPPENS ON THE FOLLOWING LINE
' RUN-TIME ERROR 438 --
' Object doesn't support this property or method
For i = 0 To (frm.lstTEST.ListCount - 1)
If frm.lstTEST.Selected(i) Then
If str <> "" Then
str = str & ","
End If
str = str & Left$(frm.lstTEST.List(i), InStr(1, frm.lstTEST.List(i), " ") - 1)
End If
Next i
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Source = "EXEC s_SelectPedsProcedures '" & Format(dtStart, "yyyy-mm-dd") _
& "', '" & Format(dtEnd, "yyyy-mm-dd") & "', '" & str & "'"
rs.ActiveConnection = pADOConnection
rs.CursorLocation = adUseClient
rs.Open
' do some stuff here
rs.Close
Set rs = Nothing
End Sub
My question:
How does one reference the controls on a form from a class module? And,
If you can't do that, how do I get around that? Basically, I'm trying to encapsulate this functionality
so that I could re-use this class in this application and others.
Thanks in advance for your help -- it is always appreciated.
Carl
| |
| Larry Serflaten 2005-11-29, 6:55 pm |
|
"Carl Imthurn" <nospam@all.com> wrote
> What am I missing about referencing a form control's methods and properties from a class?
You attemtped to append references. (Replies inline)
> ' Here's the method that gets called:
>
> Public Sub RefreshProceduresFromDatabase(frm As Form, lstTEST As ListBox, dtStart As Date, dtEnd As Date)
Consider now, (from the form's perspective) that 'frm' is actually a reference to
'Me', and 'lstTEST' is actually 'Me.lstLocations'
> ' THE ERROR HAPPENS ON THE FOLLOWING LINE
> ' RUN-TIME ERROR 438 --
> ' Object doesn't support this property or method
>
> For i = 0 To (frm.lstTEST.ListCount - 1)
So what you have here would be:
For i = 0 To (Me.Me.lstLocations.ListCount - 1)
To resolve the issue, you have already passed in the reference
to the exact listbox you want to use, so use that:
For i = 0 To lstTEST.ListCount - 1
HTH
LFS
| |
| Ken Halter 2005-11-29, 6:55 pm |
| "Carl Imthurn" <nospam@all.com> wrote in message
news:%23h0vEWQ9FHA.3416@TK2MSFTNGP15.phx.gbl...
> ' the click event of the command button
> Private Sub cmdRefreshProcedureListBoxes_Click()
> ReportInfo.RefreshProceduresFromDatabase Me, lstLocations,
> dtpBeginningDate, dtpEndingDate
> End Sub
>
>
> Public Sub RefreshProceduresFromDatabase(frm As Form, lstTEST As ListBox,
> dtStart As Date, dtEnd As Date)
TMI.... since you're passing the listbox, there's no need to pass the
form.... since the listbox is now referenced by a variable called 'lstTEST',
the form no longer recognizes the name.
'=======Form Code
Option Explicit
Private Sub Command1_Click()
Dim o As Class1
Set o = New Class1
Call o.StuffTheBox(List1)
End Sub
'=======Class1 Code
Option Explicit
Public Sub StuffTheBox(LB As ListBox)
Dim i As Integer
LB.Clear
For i = 1 To 250
LB.AddItem "Item " & i
Next
End Sub
'=======
If you want to pass the form, you'll need to refer to the box by it's real
name.
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
Please keep all discussions in the groups..
| |
| Carl Imthurn 2005-11-29, 6:55 pm |
| That was it -- thanks to both of you for pointing out a dumb oversight very gently and kindly . . . ;-)
Carl
Carl Imthurn wrote:
> What am I missing about referencing a form control's methods and
> properties from a class?
>
> Here is the relevant code:
>
> ' in the General Declarations section of my form:
> Option Explicit
> Private ReportInfo As clsReportInfo
>
> ' the Load event of my form (edited for relevance):
> Private Sub Form_Load()
> Set ReportInfo = New clsReportInfo
> End Sub
>
> ' the click event of the command button
> Private Sub cmdRefreshProcedureListBoxes_Click()
> ReportInfo.RefreshProceduresFromDatabase Me, lstLocations,
> dtpBeginningDate, dtpEndingDate
> End Sub
>
> I'm passing in to the class' method the following parameters:
> 1) a form (me)
> 2) a listbox which will be used to build a "location string"
> (lstLocations)
> 3) a beginning date for passing in to a stored procedure
> 4) an ending date for same
>
>
> I get an error when the application attempts to reference the listbox
> from the class module.
>
> ' Here's the method that gets called:
>
> Public Sub RefreshProceduresFromDatabase(frm As Form, lstTEST As
> ListBox, dtStart As Date, dtEnd As Date)
>
> Dim i As Integer
> Dim str As String
> ' build the string of selected locations for passing in to the stored
> procedure
>
> ' THE ERROR HAPPENS ON THE FOLLOWING LINE
> ' RUN-TIME ERROR 438 --
> ' Object doesn't support this property or method
>
> For i = 0 To (frm.lstTEST.ListCount - 1)
> If frm.lstTEST.Selected(i) Then
> If str <> "" Then
> str = str & ","
> End If
> str = str & Left$(frm.lstTEST.List(i), InStr(1,
> frm.lstTEST.List(i), " ") - 1)
> End If
> Next i
>
> Dim rs As ADODB.Recordset
> Set rs = New ADODB.Recordset
> rs.Source = "EXEC s_SelectPedsProcedures '" & Format(dtStart,
> "yyyy-mm-dd") _
> & "', '" & Format(dtEnd, "yyyy-mm-dd") & "', '" & str & "'"
> rs.ActiveConnection = pADOConnection
> rs.CursorLocation = adUseClient
> rs.Open
> ' do some stuff here
> rs.Close
> Set rs = Nothing
>
> End Sub
>
> My question:
> How does one reference the controls on a form from a class module? And,
> If you can't do that, how do I get around that? Basically, I'm trying to
> encapsulate this functionality
> so that I could re-use this class in this application and others.
>
> Thanks in advance for your help -- it is always appreciated.
>
> Carl
>
|
|
|
|
|