0

I want to pass an object to a class and have that object retain all references, then need to pass that object over to a Form. Trying my code produces run-time error 438, "Object doesn't support this property or method".

I don't know what I'm doing wrong, but I need to pass an object reference to a different form and be able to pull the values from it.

Private TestObj As New Test
Private ScanObj As New Scan

Private Sub cmdLogin_Click()
  Set TestObj.ScanObj = ScanObj    'ScanObj is the object that holds my scan data
  Set frmScreen.TestObj = TestObj  'Set the TestObj to another Form that needs to use these vals
  frmScreen.Test                   'Expecting to see a MsgBox with one of the set object vals, but get nothing back. Why?
  frmScreen.Show
End Sub

Does my expectation of how this should work, actually work this way?


EDIT:

I've simplified it.

My first form (Login) where I am gathering all my data:

Private TestObj As New Test
Private ScanObj As New Scan

Private Sub cmdLogin_Click()
  Set TestObj.ScanObj = ScanObj    'ScanObj is the object that holds my scan data
  Set frmScreen.TestObj = TestObj  'Set the TestObj in my Test class

  TestObj.Test                     'Call the Test class Test routine that should show one of my values, but instead get runtime 438

  frmScreen.Show
  Unload Me
End Sub

Then the code from the Test class:

Public ScanObj As Object

Public Sub Test()
  MsgBox ScanObj.Get_P
End Sub
NaN
  • 517
  • 1
  • 5
  • 12
  • I don't see any obvious issues. It _should_ work. – Brian M Stafford Jun 02 '20 at 15:01
  • `run-time error 438, "Object doesn't support this property or method"` `get nothing back.`. Which one is it? – GSerg Jun 02 '20 at 15:02
  • Thanks for looking, guys. @GSerg, both; I get the error 438 and that's it, no value is shown in MsgBox. What I am expecting to see in `frmScreen` are the values from my object but instead get error 438 – NaN Jun 02 '20 at 15:05
  • Does it matter that the above code block is in a Form and not a class? – NaN Jun 02 '20 at 15:09
  • I just tried the code you posted. It ran as expected. Perhaps the error is in the implementation of the Test and Scan classes? – Brian M Stafford Jun 02 '20 at 15:14
  • If the types of the objects are know at compile time (like they appear to be here), this should be a compile time error "Method or data member not found". I do not see how *this* code could produce a runtime error 438. – GSerg Jun 02 '20 at 15:19
  • @BrianMStafford, thanks Brian. What I noticed is if I do: `MsgBox TestObj.MyMethodThatShouldEchoSomeValueFromMyObject()` from my *`Form1`* (Sub example above) form, I can see all of my values in that object. But, when I try and MsgBox something from the frmScreen Form, the object is empty. – NaN Jun 02 '20 at 15:20
  • @GSerg, I don't know, I'm very new to this language. I'm more of a C guy. This language is a little different. – NaN Jun 02 '20 at 15:21
  • Should I be referring back to Form1 for the object? I thought I could just pass it over to the class or form where I need it? – NaN Jun 02 '20 at 15:22
  • My point was that you have not shown enough code to understand the problem, and the code you have shown cannot cause it. It would appear though that you would be better off not using [implicit form instances](https://stackoverflow.com/a/54573544/11683). I am assuming you do have `Option Explicit` everywhere (unrelated to implicit forms). – GSerg Jun 02 '20 at 15:25
  • Yes, you can pass objects as needed. Like I said above, the posted code works as expected. I get MsgBox's from frmScreen.Test with values from the objects. – Brian M Stafford Jun 02 '20 at 15:26
  • I'm going to post more code up top, now. Thanks for the help – NaN Jun 02 '20 at 15:36
  • 1
    Does your Scan class contain a `Get_P` method? Also, in the Test class why not declare ScanObj As Scan? – Brian M Stafford Jun 02 '20 at 15:59
  • @BrianMStafford, yes, it does. In fact, I can access all Object properties from the first, frmLogin class. Once I `Set` it elsewhere and check the destination, it's empty. Could it be as GSerg suggested? Could it be implicit form instances? – NaN Jun 02 '20 at 16:32
  • In your edited code, the problem still occurs even not using a form. So what's happening inside of `Get_P`? – Brian M Stafford Jun 02 '20 at 16:54
  • @BrianMStafford, It is doing the very same thing. Can you please provide a working example between a form and a class where this actually works? I must be doing something wrong. If while in the first form I do `MsgBox ScanObj.Get_P`, I can see the string I'm looking for. Once I `Set` that object to a Public property in another FORM (not class or module), the object is empty. – NaN Jun 02 '20 at 16:59
  • @BrianMStafford, Inside Get_P all I'm doing is returning a string. – NaN Jun 02 '20 at 17:14

1 Answers1

0

Going back to the original sample code, here is how I set it up:

Login Form

Option Explicit

Private TestObj As Test
Private ScanObj As Scan

Private Sub Form_Initialize()
  Set TestObj = New Test
  Set ScanObj = New Scan
End Sub

Private Sub cmdLogin_Click()
  Set TestObj.ScanObj = ScanObj
  Set frmScreen.TestObj = TestObj
  frmScreen.Test
  frmScreen.Show
End Sub

Screen Form

Option Explicit

Public TestObj As Test

Public Sub Test()
   MsgBox "Test Name is: " & TestObj.Name
   MsgBox "Scanner Name is: " & TestObj.ScanObj.Get_P
End Sub

Test Class

Option Explicit

Public ScanObj As Scan
Public Name As String

Private Sub Class_Initialize()
   Name = "Some Name"
End Sub

Scan Class

Option Explicit

Public Function Get_P() As String
   Get_P = "Some String"
End Function
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • Brian, thank you. I can see that your example works fine but I am not able to figure out why I can't get this to work. I'll keep playing with it in the meantime and see what I can find. I'll post back after I find it. Thank you again for all your help! – NaN Jun 02 '20 at 17:38
  • Brian, in my original example, in the `cmdLogin_Click()` method, I called the Sub `frmScreen.Test`. What if I don't want to call the Test function from the Login form but instead attempt to use `TestObj` in the frmScreen Form Class? Would that work? – NaN Jun 02 '20 at 17:46
  • Brian, all the scan class is doing is splitting a string and saving 4 properties. I want to call these properties by simply access my object. I removed `frmScreen.Test` from the Login form and then in frmScreen Form, I have this: `Private Sub Class_Initialize() Call Test End Sub Public Sub Test() MsgBox "Test Name is: " & TestObj.Name MsgBox "Scanner ID is: " & TestObj.ScanObj.Get_P End Sub` The object is empty – NaN Jun 02 '20 at 17:50
  • Brian, I was able to figure out that I needed to change to `Form_Initialize`. On initializing TestObj, wouldn't I do that inside the Form_Initialize Sub of the frmScreen Form? Maybe something like: `Set TestObj = New Test`? I've already tried this and the object is empty – NaN Jun 02 '20 at 18:10
  • Brian, this is what I have in there now. Again, it's not working. `Private Sub Form_Initialize() Set TestObj = New Test MsgBox TestObj.ScanObj.Get_P End Sub` – NaN Jun 02 '20 at 18:12
  • You could leave everything else the same and move your `Call Test` to Form_Load instead of Form_Initialize. There's several ways to do it. Just make sure objects are initialized before you try to use it's methods. – Brian M Stafford Jun 02 '20 at 18:12
  • Yes, this is working!! :) After all that, I'm still not sure what I was doing wrong. Thank you! – NaN Jun 02 '20 at 18:18