Marc Lang
Marc Lang

Reputation: 1

Problem returning object from VB.NET COM Assembly into VBA (Access)

I have an assembly in VB .NET 2.0 that I am trying to use to call a webservice.

This will be COM visible, and return the results to Access in VBA.

The .NET Assembly passes all tests and executes perfectly.

I was experiencing "Object does not support this property or method" errors when calling the methods from VBA.

I broke it down to a certain object that was being returned and added some test methods to the .NET DLL.

There is a "Patient" object I want to return. It looks like this (made it very very simple to test it):

Option Strict On
Option Explicit On

<ComClass(Patient.ClassId, Patient.InterfaceId, Patient.EventsId)> _
Public Class Patient

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "672dfbd9-8f3a-4ba2-a33d-89fef868f2b9"
    Public Const InterfaceId As String = "74a9c54c-4427-4d31-8220-3258ecda345d"
    Public Const EventsId As String = "dc25515e-1bb7-4a66-97d5-270c00d792a9"
#End Region

    Public Sub New()

        MyBase.New()

    End Sub

    Public Property StorePatientID() As Integer
        Get
            Return m_StorePatientID
        End Get
        Set(ByVal value As Integer)
            m_StorePatientID = value
        End Set
    End Property
    Private m_StorePatientID As Integer
End Class

So about as simple as an object can be really.

I have a method that just returns a dummy record, just to test it:

Public Function GetPatientTest() As Patient
        Dim patient As New Patient

        patient.StorePatientID = 99

        Return patient
    End Function

This fails with the afformentioned error.

HOWEVER,

This method succeeds!

Public Function GetPatientArrayTest() As Patient()

        Dim strings As New List(Of Patient)
        Dim patient As New Patient

        patient.StorePatientID = 99

        strings.Add(patient)

        Return strings.ToArray

    End Function

The DLL is made com visible through "Properties" page. Builds to project/bin/debug, always do a rebuild. Always seems to be updated with new methods etc when I look at it in VBA so don't think it's looking at an old version.

Obviously no funny dependencies with these methods.

Really really struggling with this.

EDIT:

Update 16/03/2011 - Added VBA script

Public Function FindPatientsTest(ByVal surname As String, ByVal surnameBeginsWith As Boolean, ByVal forename As String, ByVal forenameBeginsWith As Boolean, ByVal dateOfBirth As String)


Dim token As String
token = Login()

Dim patient As SCIStoreWS60.patient
Set patient = New SCIStoreWS60.patient

'// This doesn't work.
'// When adding a "Watch" to the function, I can see it returns an "Object/Patient" and is the correct results
'// When adding a "Watch" to the variable "patient" I can see it is a "Patient/Patient"
patient = sciStore.GetPatientTest()

'// This works fine
Dim something As Variant
something = sciStore.GetPatientArrayTest()

End Function

Update 16/03/2011 5 minutes later - Chastising myself

Sorry, I just worked it out.

I need to "Set" the patient variable.

Set patient = sciStore.GetPatientTest()

Why didn't I need to do this for the "something" variant?

Upvotes: 0

Views: 1478

Answers (1)

Mark Hurd
Mark Hurd

Reputation: 10931

So, yes, you need to Set object references, but not arrays.

Upvotes: 1

Related Questions