Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner Class/Module Question

Status
Not open for further replies.

stnkyminky

Programmer
Oct 15, 2001
476
US
I am attempting to use classes in a new project I'm working on.
I have a class called clsAdd. In clsAdd I store information about individual shipments, PO#'s, Addys, etc.
I instantiate a reference to clsAdd in a form called frmMain.
In frmMain I call a function in ClsAdd called PopulateFields. PopulateFields queries the DB and stores the individual results in the variables defined in ClsAdd. Once PopulateFields is complete the focus goes back to frmMain which does some other things then loads a form call frmAdd, which shows the result of the query.

frmAdd's Form_Load Event contains a reference to clsAdd and attempts to retrieve the values of clsAdd's variables and assign then to text boxes. When trying to assign the values to the text boxes, nothing is in the clsAdd variables.


What am I doing wrong? Scott
Programmer Analyst
 
Mind posting the code here or emailing it to me at seanpatrickmorgan@yahoo.com (either just the Form_Load code and the clsAdd code, or you can zip the whole project if you are comfortable doing that)? The first thing I think of is how you are passing the variables (By Value as opposed to By Reference - you would want to use By Reference if you are using the variables as parameters into the function but want them to be changed inside the function), but really I would have to know more about the function to answer with any level of surety.
 
Thanks again.
-Scott

'Onclick event that triggers PopulateFields

Dim add As ClsInfo

Public cmdEnter_Click()
Dim foo As RegExp

Set foo = New RegExp
foo.Pattern = "^[0-9]{5}$"
If foo.Test(txtShop.Text) = False Then
MsgBox "Please re-enter the shop order number."

With txtShop
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With

Set foo = Nothing
Exit Sub
End If
Set foo = Nothing

Set add = New ClsAdd
'Populate fields on frmAddShop

add.PopulateFields txtShop.Text, "Add"

Set add = Nothing
frmAddShop.Show
Unload Me


'From frmAdd
'Where the text boxes should be assigned values

Dim add As ClsInfo

Public Sub Form_Load()

On Error GoTo ErrorHandler

Set add = New ClsAdd

txtJobOrder.Text = add.IdJob

Screen.MousePointer = vbDefault

Exit Sub
ErrorHandler:

Select Case modLib.ProgError(Err, Me.Name)
Case 3 ' Abort
End
Case 4 ' Retry
Resume
Case 5 ' Ignore
Resume Next
End Select

End Sub



'Class Info
Option Explicit

Public Property Let PcMk(ByVal vData As String)
End Property

Public Property Get PcMk() As String
End Property

Public Property Let Descr(ByVal vData As String)
End Property

Public Property Get Descr() As String
End Property

Public Property Let IdItem(ByVal vData As String)
End Property

Public Property Get IdItem() As String
End Property

Public Property Let OrdLine(ByVal vData As String)
End Property

Public Property Get OrdLine() As String
End Property

Public Property Let AddTime(ByVal vData As String)
End Property

Public Property Get AddTime() As String
End Property

Public Property Let AddDate(ByVal vData As Date)
End Property

Public Property Get AddDate() As Date
End Property

Public Property Let Add4(ByVal vData As String)
End Property

Public Property Get Add4() As String
End Property

Public Property Let Add3(ByVal vData As String)
End Property

Public Property Get Add3() As String
End Property

Public Property Let Add2(ByVal vData As String)
End Property

Public Property Get Add2() As String
End Property

Public Property Let Add1(ByVal vData As String)
End Property

Public Property Get Add1() As String
End Property

Public Property Let CustName(ByVal vData As String)
End Property

Public Property Get CustName() As String
End Property

Public Property Let CustId(ByVal vData As String)
End Property

Public Property Get CustId() As String
End Property

Public Property Let CustPo(ByVal vData As String)
End Property

Public Property Get CustPo() As String
End Property

Public Property Let IdSo(ByVal vData As String)
End Property

Public Property Get IdSo() As String
End Property

Public Property Get IdJob() As String
End Property

Public Property Let IdJob(ByVal strNewValue As String)
End Property

Public Property Get IdBdl() As String
End Property

Public Property Let IdBdl(ByVal strNewValue As String)
End Property

Public Function PopulateFields(ByVal ShopOrderNumber As String, ByVal method As String)

End Function


'Class Add
Option Explicit

Implements ClsInfo

Private m_IdSo As String
Private m_IdBdl As String
Private m_IdJob As String
Private m_CustPo As String
Private m_CustId As String
Private m_CustName As String
Private m_AddTime As String
Private m_addDate As Date
Private m_Add1 As String
Private m_Add2 As String
Private m_Add3 As String
Private m_Add4 As String
Private m_OrdLine As String
Private m_IdItem As String
Private m_Desc As String
Private m_PcMk As String


Private Sub Class_Reset()
m_IdSo = ""
m_IdBdl = ""
m_IdJob = ""
m_CustPo = ""
m_CustId = ""
m_CustName = ""
m_AddTime = ""
m_addDate = ""
m_Add1 = ""
m_Add2 = ""
m_Add3 = ""
m_Add4 = ""
End Sub



Private Property Let ClsInfo_Add4(ByVal RHS As String)
m_Add4 = RHS
End Property

Private Property Get ClsInfo_Add4() As String
ClsInfo_Add4 = m_Add4
End Property

Private Property Let ClsInfo_Add3(ByVal RHS As String)
m_Add3 = RHS
End Property

Private Property Get ClsInfo_Add3() As String
ClsInfo_Add3 = m_Add3
End Property

Private Property Let ClsInfo_Add2(ByVal RHS As String)
m_Add2 = RHS
End Property

Private Property Get ClsInfo_Add2() As String
ClsInfo_Add2 = m_Add2
End Property

Private Property Let ClsInfo_Add1(ByVal RHS As String)
m_Add1 = RHS
End Property

Private Property Get ClsInfo_Add1() As String
ClsInfo_Add1 = m_Add1
End Property

Private Property Let ClsInfo_AddDate(ByVal RHS As Date)
m_addDate = RHS
End Property

Private Property Get ClsInfo_AddDate() As Date
ClsInfo_AddDate = m_addDate
End Property

Private Property Get ClsInfo_AddTime() As String
ClsInfo_AddTime = m_AddTime
End Property

Private Property Let ClsInfo_AddTime(ByVal RHS As String)
m_AddTime = RHS
End Property

Private Property Get ClsInfo_CustId() As String
ClsInfo_CustId = m_CustId
End Property

Private Property Let ClsInfo_CustId(ByVal RHS As String)
m_CustId = RHS
End Property

Private Property Get ClsInfo_CustName() As String
ClsInfo_CustName = m_CustName
End Property

Private Property Let ClsInfo_CustName(ByVal RHS As String)
m_CustName = RHS
End Property

Private Property Get ClsInfo_CustPo() As String
ClsInfo_CustPo = m_CustPo
End Property

Private Property Let ClsInfo_CustPo(ByVal RHS As String)
m_CustPo = RHS
End Property

Private Property Let ClsInfo_Descr(ByVal RHS As String)
m_Desc = RHS
End Property

Private Property Get ClsInfo_Descr() As String
ClsInfo_Descr = m_Desc
End Property

Private Property Get ClsInfo_IdBdl() As String
ClsInfo_IdBdl = m_IdBdl
End Property

Private Property Let ClsInfo_IdBdl(ByVal RHS As String)
m_IdBdl = RHS
End Property

Private Property Let ClsInfo_IdItem(ByVal RHS As String)
m_IdItem = RHS
End Property

Private Property Get ClsInfo_IdItem() As String
ClsInfo_IdItem = m_IdItem
End Property

Private Property Get ClsInfo_IdJob() As String
ClsInfo_IdJob = m_IdJob
End Property

Private Property Let ClsInfo_IdJob(ByRef RHS As String)
m_IdJob = RHS
End Property

Private Property Get ClsInfo_IdSo() As String
ClsInfo_IdSo = m_IdSo
End Property

Private Property Let ClsInfo_IdSo(ByVal RHS As String)
m_IdSo = RHS
End Property


Private Property Let ClsInfo_OrdLine(ByVal RHS As String)
m_OrdLine = RHS
End Property

Private Property Get ClsInfo_OrdLine() As String
ClsInfo_OrdLine = m_OrdLine
End Property

Private Property Let ClsInfo_PcMk(ByVal RHS As String)
m_PcMk = RHS
End Property

Private Property Get ClsInfo_PcMk() As String
ClsInfo_PcMk = m_PcMk
End Property

Private Function ClsInfo_PopulateFields(ByVal ShopOrderNumber As String, ByVal method As String)
'==============================================================================
'= Title: Public Function PopulateFields() =
'=----------------------------------------------------------------------------=
'= Description: Populates the field in frmAddShop =
'=----------------------------------------------------------------------------=
'= Author(s): Scott Rose =
'=----------------------------------------------------------------------------=
'= Inputs: =
'=----------------------------------------------------------------------------=
'= Creation Date: 10/10/2001 =
'= Version: 1.0 =
'= Revisions: =
'==============================================================================

Dim db As clsDBConnection


Dim strPath As String


On Error GoTo ErrorHandler

Set db = New clsDBConnection

'Change mouse pointer to hourglass
Screen.MousePointer = vbHourglass

'SQL Statement
db.Project_strSql = "Select ID_SO, ID_ORD, SEQ_LINE_ORD, ID_ITEM_PAR, ID_PO_CUST, ID_CUST_SOLDTO, ID_JOB, NAME_CUST, ADDR_1, ADDR_2, ADDR_3, ADDR_4, DATE_ADD, TIME_ADD, DESCR_ITEM_1, DESCR_ITEM_2 FROM tblTest where ID_SO like '%" & ShopOrderNumber & "'"

db.ProjectConnect
Set db.Project_rs = db.Project_My_Conn.Execute(db.Project_strSql)

'If EOF then the shop order was not found.
'Prompt, return focus on the form and set bstatus to false
If db.Project_rs.EOF Then
MsgBox ShopOrderNumber & " does not exist. Please re-enter another shop order."
bStatus = False
db.ProjectDisconnect
Set db = Nothing
Exit Function
Else

If m_IdJob <> &quot;&quot; And m_IdBdl <> &quot;&quot; Then

'Set check = New clsDBConnection

db.Project_strSql = &quot;Select ID_SO from zebradb where ID_JOB = '&quot; & _
m_IdJob & &quot; ' and ID_BDL = '&quot; & _
m_IdBdl & &quot;' and ID_SO = &quot; & m_IdSo
'check.ProjectConnect

Set db.Project_rs = db.Project_My_Conn.Execute(db.Project_strSql)

If Not db.Project_rs.EOF Then

MsgBox &quot;The shoporder number &quot; & m_IdSo & _
&quot; has already been entered for bundle &quot; & m_IdBdl

bStatus = False
db.ProjectDisconnect
Set db = Nothing
Exit Function

Else
If db.Project_rs(&quot;ID_JOB&quot;) <> m_IdJob And bpass = False Then
MsgBox &quot;The job number does not match the previous job number.&quot;

bStatus = False

db.ProjectDisconnect
Set db = Nothing


Exit Function

Screen.MousePointer = vbDefault
End If
End If
End If


If (bStatus = False And m_IdJob = &quot;&quot;) Or (bStatus = True) Or (bStatus = False And m_IdJob <> &quot;&quot;) Then
'Set text box values

m_IdSo = LTrim(db.Project_rs(&quot;ID_SO&quot;))
m_OrdLine = db.Project_rs(&quot;ID_ORD&quot;) & db.Project_rs(&quot;SEQ_LINE_ORD&quot;)
m_IdItem = db.Project_rs(&quot;ID_ITEM_PAR&quot;)
m_IdJob = db.Project_rs(&quot;ID_JOB&quot;)
m_Desc = Replace(db.Project_rs(&quot;DESCR_ITEM_1&quot;), &quot;*&quot;, &quot;,&quot;)
m_PcMk = IIf(IsNull(db.Project_rs(&quot;DESCR_ITEM_2&quot;)), &quot;&quot;, db.Project_rs(&quot;DESCR_ITEM_2&quot;))
m_IdJob = db.Project_rs(&quot;ID_JOB&quot;)
m_CustPo = db.Project_rs(&quot;ID_PO_CUST&quot;)
m_CustId = db.Project_rs(&quot;ID_CUST_SOLDTO&quot;)
m_CustName = db.Project_rs(&quot;NAME_CUST&quot;)
m_Add1 = IIf(IsNull(db.Project_rs(&quot;ADDR_1&quot;)), &quot;&quot;, db.Project_rs(&quot;ADDR_1&quot;))
m_Add2 = IIf(IsNull(db.Project_rs(&quot;ADDR_2&quot;)), &quot;&quot;, db.Project_rs(&quot;ADDR_2&quot;))
m_Add3 = IIf(IsNull(db.Project_rs(&quot;ADDR_3&quot;)), &quot;&quot;, db.Project_rs(&quot;ADDR_3&quot;))
m_Add4 = IIf(IsNull(db.Project_rs(&quot;ADDR_4&quot;)), &quot;&quot;, db.Project_rs(&quot;ADDR_4&quot;))
m_addDate = db.Project_rs(&quot;DATE_ADD&quot;)
m_AddTime = db.Project_rs(&quot;TIME_ADD&quot;)


'show that this pass is without error
bStatus = True
bpass = False

'db.Project_rs.MoveNext

'if the job ids do not match then show there were errors

db.ProjectDisconnect
'Set add = Nothing
Set db = Nothing


End If
'End Select
End If

Screen.MousePointer = vbDefault

End Function
Scott
Programmer Analyst
 
I think that you're creating 2 separate instances of clsAdd. If I understand you correctly, you have 2 separate form-level clsAdd objects, both called add.

Although I didn't see any reference to frmMain in the posted code above (all I noticed was frmAddShop & frmAdd), I think this is what is going on...

The add object that you declared in frmAddShop will be destroyed when you set it to nothing so the add object that belongs to frmAddShop will have no data in it. Even if it did, in frmAdd you're dealing with a completely different object whose properties haven't been assigned to anything from your db.

Your options are to call populatefields from within frmAdd or make add a public variable in one of your forms so that it can accessed with the dot notation:

frmMain.add

I don't recommend the latter because creating public variables like that is not good practice and you'll have to avoid unloading the form with that has the add object until you're done using the object all together.


Hope this helps!
Josh
 
So would I be better off removing the class module and replacing it with a module (.bas) since I am wanting to retain values that are accessible throughout the program? The new module would contain the same declarations as does the class...correct? Scott
Programmer Analyst
 
Sorry - went out for lunch and didn't see you code until I got back. Josh is right about your problem. I agree whole heartedly with him about creating global variables - it's really not a good practice when it's not absolutely necessary. The way you have designed your class, however, that might be the easiest way to do it without having to rewrite your code. A module (.BAS) is not the same thing as a class (modules don't have member variables or &quot;properties&quot; like classes do - instead you would have module level variables that are public, effectively making them global variables to the project), so you can't just cut and paste your class code into a module and solve your problem. I think the easiest solution here is to go ahead and make a global Add object so you don't have to rewrite your code. Just add a module to your project and create a public variable called &quot;objAdd&quot; or something like that and it will be available to every form in your project. A good thing to keep in mind when designing classes is that when you use member variables (a.k.a. properties) like that you are creating an object that maintains &quot;state&quot;, which is just a fancy way of saying it holds onto certain information throughout its life cycle and you need to be aware of that when you are using it. To avoid this problem, you can always create classes that are &quot;stateless&quot; by making the methods &quot;black boxes&quot;. All this means is that any data manipulation takes place by passing arguments into and out of the methods rather than by manipulating member variables, which you then have to set and retrieve.
 
All,
Thanks for the help.

Nagrom,
Could you give an example of a stateless class. Scott
Programmer Analyst
 
The easiest way to think of a stateless class is one where you don't use member variables (that's not the best explanation, but it will serve). In that case, you would use arguments for the methods to pass data.

'========================================================
'Example class method code:
'========================================================

Public Function SwitchStrings(ByRef strString1 As String, ByRef strString2 As String) As Long

' Declare local variables
Dim strTempString As String
Dim lngReturnCode As Long

' Initialize the return code
lngReturnCode = -1

' Setup simple error handling
On Error GoTo SwitchStringsError

' Change the parameter values
strTempString = strString1
strString1 = strString2
strString2 = strTempString

' Set return code to indicate success
lngReturnCode = 0

SwitchStringsError:

' Return the return code value for the function
SwitchStrings = lngReturnCode

End Function

'========================================================

If you implement this method in a class and then create an object objMyClass you can use that method as shown below:

'========================================================

' Declare variables
Dim objMyClass As New MyClass.Whatever
Dim lngReturnValue As Long
Dim strX As String
Dim strY As String

' Create the object
Set objMyClass = New MyClass.Whatever

' Set the initial string values
strX = &quot;X&quot;
strY = &quot;Y&quot;

' Call the method
lngReturnValue = objMyClass.SwitchStrings(strX, strY)

'========================================================

When you do that then strX will have the value &quot;Y&quot; and strY will have the value &quot;X&quot;. The variable lngReturnValue will have a value of 0. Everything is done inside the function, hence the term &quot;black box&quot;. This way you don't have to set member variables, call a method, then retrieve member variables. Now it's stateless because the only data you're working with is what you pass into the method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top