|
-
Oct 9th, 2002, 11:16 AM
#1
Thread Starter
Lively Member
Passing forms to classes...
Hello, I'm just getting into Object based Programming (with VB6, which I have to use at work for now - .NET later, hopefully soon).
Here's the situation: I'm trying to encapsulate code for populating combo boxes throughout in a multiform project. I have made a class called, surprisingly, "populateCombo". Here is the code I have so far in that class module (just one method):
Public Sub populateCombo(ByVal frmWhich As Integer, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
Dim objRS As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
If frmWhich = 0 Then
Do While Not objRS.EOF
frm1.Combo(cboNum).AddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
ElseIf frmWhich = 1 Then
Do While Not objRS.EOF
frm2.Combo1(0).AddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
End If
End Sub
It's pretty self-explanatory, but the if-then structures sort of defeat the purpose of reusable code. Is there a way to pass in form names when I call populateCombo rather than having the frmWhich and repeating the code for each form? The current configuration works fine, but I'd like to make it slicker, for example, when I call the procedure I'd like to do something like this: Call populateCombo("frm1", 1, "<table name>", "<field name>"). Is this even possible or am I stuck with the above situation?
Thank you! This forum has been a big help to me!
Ed Womack
-
Oct 9th, 2002, 02:06 PM
#2
PowerPoster
VB Code:
Public Sub populateCombo(ByRef frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
Dim objRS As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
Do While Not objRS.EOF
frmWhich.Combo1(0).AddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
End Sub
Just pass "Me" as a parameter.
-
Oct 9th, 2002, 03:02 PM
#3
Thread Starter
Lively Member
I put in the code as in above (this is from my screen):
Public Sub populateCombo(ByVal frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)
Dim objRS As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
Do While Not objRS.EOF
frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
End Sub
I also changed the class invocation to:
Dim cboTest as New populateCombo
Call cboTest.populateCombo(Me, 0, "<table name>", "<field name>")
When I run the program I get a "Run-Time error '438': Object does not support this property or method." The debug button takes me back to this line:
frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
When I try to type in the line from scratch above, intellisense will not let me enter "Combo1." It gives me a list of form properties, but nothing about the forms controls. There is a "Controls" property on the list, however.
I have a feeling I'm very close...
Thank you,
Ed Womack
-
Oct 9th, 2002, 03:18 PM
#4
Is the name of the combo box combo1? Unless you need the form for something else then it'd be better to pass the combo instead of the whole form.
VB Code:
Public Sub populateCombo(ByVal cbo As ComboBox, ByVal tblName As String, fieldName As String)
Dim objRS As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
Do While Not objRS.EOF
cboAddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
End Sub
'syntax
Call cboTest.populateCombo(Me.Combo1(1), "<table name>", "<field name>")
-
Oct 9th, 2002, 03:51 PM
#5
Thread Starter
Lively Member
Wow! That did it! Now I can pass the entire Combo box to the class function from anywhere in my program. Incredibly cool.
One thing; on the code in the last message the line:
cboAddItem objRS.Fields(fieldName)
should read:
cbo.AddItem objRS.Fields(fieldName)
I'm sure it's just a typo.
Thank you for the help! I appreciate it!!
Ed Womack
-
Oct 10th, 2002, 01:24 AM
#6
PowerPoster
Originally posted by ewomack
I put in the code as in above (this is from my screen):
Public Sub populateCombo(ByVal frmWhich As Form, ByVal cboNum As Integer, ByVal tblName As String, fieldName As String)****
Dim objRS As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT " & fieldName & " FROM " & tblName & ";"
objRS.Open strSQL, g_objConn, adOpenForwardOnly, adLockReadOnly
Do While Not objRS.EOF
frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
objRS.MoveNext
Loop
Set objRS = Nothing
End Sub
I also changed the class invocation to:
Dim cboTest as New populateCombo
Call cboTest.populateCombo(Me, 0, "<table name>", "<field name>")
When I run the program I get a "Run-Time error '438': Object does not support this property or method." The debug button takes me back to this line:
frmWhich.Combo1(cboNum).AddItem objRS.Fields(fieldName)
When I try to type in the line from scratch above, intellisense will not let me enter "Combo1." It gives me a list of form properties, but nothing about the forms controls. There is a "Controls" property on the list, however.
I have a feeling I'm very close...
Thank you,
Ed Womack
You are using ByVal instead of ByRef in the form parameter.
-
Oct 10th, 2002, 09:39 AM
#7
Thread Starter
Lively Member
Whoop! That one slipped by - I don't want to pass copies of the combo box - thanks, I 'll make that change!
Ed Womack
-
Oct 21st, 2002, 09:48 PM
#8
Passing a control ByVal does not pass a copy of the control. If I recall correctly it simply passes a copy of the pointer to the control.
You should also be aware that in a Public class you cannot pass controls in this manner. You must declare them as Object and typically you would include code to make sure the correct kind of control was passed
Code:
Public Sub DoSomething(ComboBoxControl as Object)
If Typeof ComboBoxControl is VB.ComboBox Then
... processing
Else
Err.Raise ...
End if
End Sub
or you could set a local variable = to the combobox parameter (you will get a type mismatch error if someone tries to pass a textbox)
Code:
Public Sub DoSomething(ComboBoxControl as Object)
Dim objCombo as VB.ComboBox
Set objCombo = ComboBoxControl
End Sub
Have fun.
Bruce
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|