X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> How To Reference And Call A Function In A Vb.net Class    
post Mar 3 2019, 08:28 PM

Posts: 150
Joined: 16-October 12

A function exists in VB.NET (actually, Microsoft.VisualBasic.Conversion.CTypeDynamic(object, System.Type)) that does not exist in VB6. I'd like to use this VB.net function in an Access 2007 project.

So, I just installed Visual Studio 2019 to get the DLL (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.VisualBasic.dll) (similar paths for other .NETFramework versions).

The problem is, (i) the VB.NET type library isn't obvious from the Access 2007 Visual Basic Editor (Tools > References); and further(ii) the Editor won't allow one to reference this DLL, and no Type Library file (*.tlb) or Object Library file (*.olb) seems to exist for the DLL.

I'd be grateful for any thoughts about how I can get use of this function. I'm guessing this is either a matter of getting the editor to recognize something, or maybe some kind of late-binding approach involving GetObject(), but a few hours into it, no solution is obvious.

My purpose, BTW, is to set an object variable using a string variable. This is leading to type mismatch errors, so I thought I'd just change the type using

Dim c As String
Dim d As Control
c = [expression]
d = CType(c, Control)

but I have to be able to call the function.

Thanks in advance for any thoughts.
Go to the top of the page
post Mar 3 2019, 10:29 PM

Posts: 1,610
Joined: 25-January 16
From: The Great Land

Where is this code located? Behind a form? Why do you need a control object variable?


Set d = Me.Controls(c)

This post has been edited by June7: Mar 3 2019, 10:30 PM

Attachments Manager is below the edit post window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Go to the top of the page
post Mar 4 2019, 03:45 AM

Posts: 700
Joined: 26-May 15
From: The middle of Germany

Why do you think you need to cast a string to a control? - How is that supposed to work? - I think it simply cannot!
The most likely solution was already suggested by June7. If that doesn't help, please describe the core problem in more detail. I'm pretty sure any external library is not the solution.

A professional Access developer tool: Find and Replace for Access and VBA
Go to the top of the page
post Mar 4 2019, 03:55 AM

UtterAccess VIP
Posts: 3,123
Joined: 12-April 07
From: Edmonton, Alberta Canada

Well the .net frame work is SO VERY different then managed code (VBA, access) or c++ or VB6.

It would be like asking an electric car frame work to convert the gas remaining in your tank for a intenal combustion car. The electric car framework is very different – and casting objects from a VASTLY different frame work is simply not going to work.

In addition to the above?

DLL’s produced by and compiled by and produced by .net as a general rule cannot be consumed by un-managed code platforms (like office and VBA).

Managed code = .net code

Un-managed code = windows x32 code. (office, VBA, VB6, c++)

As the other poster pointed out, you can always reference a control on a form by a given string value.

You can go:
Debug.print me.Country


Dim strSting as string

strString = "Country"

debug.print me(strString)

So you can always grab/get a referance to a control from a string in Access.

And you can do this for forms also.

Debug.Print Forms("FormAsString")("County")

The above as standard forms refercing (hard coded without strings) would be

debug.print Forms!FormName!Country

And thus your code would be:

Dim strControlName  as string

strControlName = "City"

dim c as control

set c = me(strControlName)

At this point, c is a control object, and one based on the string value. But you likely don’t need to define that control object, but can just reference the control using the string in the first place.

Regardless, using the net library to cast a string to say a control object would in fact return a .net type of control, not a Access one. In fact, what .net even uses for a string is likely different then a string in Access/VBA.

You might want to expand on what you are attempting here.

I am rather sure attempting to use the .net framework to cast variable types will return .net types of objects – and they are NOT compatible with VBA objects, or VBA code for that matter.

Now you can create .dll’s in .net as ActiveX (COM) objects that ca be consumed by Access. And there are a number of add-ins in .net that allow you to create standard windows x32 .dll’s that you can consume from Access.

So you can consume .dll’s from .net, but you have to write them as a class object, and use “interop”. (or use a add-in to create standard windows x32 .dlls).

So the “idea” to call or use some .net code to do things and have use of some additional functions is a great idea. I do this all the time!
So I don’t want to throw cold water on the idea of using or calling .net code from Access. There is tons of great features in .net that I use + consume in Access.

However, creating those .net objects, or calling some .net code will certainly not allow casting of object types that will be compatible for use in Access. The.net framework objects are too different.

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada
Go to the top of the page
post Mar 4 2019, 06:08 PM

Posts: 150
Joined: 16-October 12

Thanks to all for the input.

The solution for casting a string variable as a control variable seems to be June7's suggestion of including the string variable in a reference to a member of the Controls collection in setting a Control (or other Object) variable

To answer some of the questions, though:

The broader context is a search/parameter form, on which are command buttons, behind which most of the code resides. The code for each button updates FKs for records in another form's Form.Recordset according to the parameter set in a corresponding unbound combo box. Before running the Update routine, I am creating a number of tests and branches with associated message boxes to preclude unintended, not to mention irreversible, data changes. Among those tests are (a) testing that the combo box has a parameter (i.e., is not Null); (b) testing that the other form is loaded; and © testing that the other form is filtered, based on the record count. The OnClick Event Procedure sets input variables for the combo box and underlying form and passes these variables as arguments to other procedures that run the tests and return strings for inclusion in MsgBox arguments.

So, I need a control variable because I must test whether a control is null, but I also need the same input variable also to trigger return of a distinct string. One approach that occurred to me overnight is simply to cast the input variable as Control type, pass it as such, and either call Control.Name or a string function accepting a Control type argument when I need a string. June7's approach does the trick, though.

Where I had gotten hung up was that if I coded:

    Dim ctl As String
                 Dim strMe As String
                 Dim i As Boolean
                 Dim j As Boolean
                 Dim k As Boolean
                 Dim l As Boolean
                 Dim m As Boolean
                 Dim n As Boolean
                 Dim o As Boolean
                 Dim p As Boolean
                 Dim q As Boolean
                 Dim r As Boolean
                 Dim s As Boolean
                 Dim t As Boolean
                 ctl = "cboProjectID"
                 Debug.Print "ctl = " & ctl
               i = IsNull(cboProjectID)
                 j = IsNull(ctl)
                 k = Not IsNull(cboProjectID)
                 l = Not IsNull(ctl)
                 q = IsNull("ctl")
                 r = Not IsNull("ctl")
                 Debug.Print "i = IsNull(cboProjectID) = " & i
                 Debug.Print "j = IsNull(ctl) = " & j
                 Debug.Print "k = Not IsNull(cboProjectID) = " & k
                 Debug.Print "l = Not IsNull(ctl) = " & l
                 Debug.Print "q = " & q
                 Debug.Print "r = " & r
                 strMe = "Me." & ctl
                 Debug.Print "strMe =" & strMe
                 m = IsNull(Me.cboProjectID)
                 n = IsNull(strMe)
                 o = Not IsNull(Me.cboProjectID)
                 p = Not IsNull(strMe)
                 s = IsNull("strme")
                 t = Not IsNull("strme")
                 Debug.Print "m = IsNull(Me.cboProjectID) = " & m
                 Debug.Print "n = IsNull(strMe) = " & n
                 Debug.Print "o = Not IsNull(Me.cboProjectID) = " & o
                 Debug.Print "p = Not IsNull(strMe) = " & p
                 Debug.Print "s = " & s
                 Debug.Print "t = " & t

what I got was
ctl = cboProjectID
             i = IsNull(cboProjectID) = True
             j = IsNull(ctl) = False
             k = Not IsNull(cboProjectID) = False
             l = Not IsNull(ctl) = True
             q = False
             r = True
             strMe =Me.cboProjectID
             m = IsNull(Me.cboProjectID) = True
             n = IsNull(strMe) = False
             o = Not IsNull(Me.cboProjectID) = False
             p = Not IsNull(strMe) = True
             s = False
             t = True

Obviously, the string variable is not working in IsNull(), even though the text it contains, when used literally, is a sufficient reference to the control. Obviously, what is necessary is a type conversion of the variable. CType has yet to migrate from VB.NET to VBA, and isn't available otherwise. What June7 suggested, however, is essentially the same thing but by different means: converting or casting the string variable to or as a control variable by wrapping the string variable in a reference to a member of the Controls collection, in setting a control variable. So, what we find is that by coding:

           Dim a As Control
           Dim b As Control
           Dim u As Boolean
           Dim v As Boolean
           Dim w As Boolean
           Dim x As Boolean
           Set a = Me.Controls("cboProjectID")
           Set b = Me.Controls(ctl)
           u = IsNull(a)
           v = Not IsNull(a)
           w = IsNull(b)
           x = Not IsNull(b)
           Debug.Print "a = " & a.Name
           Debug.Print "b = " & b.Name
           Debug.Print "u = IsNull(a) = " & u
           Debug.Print "v = Not IsNull(a) = " & v
           Debug.Print "w = IsNull(b) = " & w
           Debug.Print "x = Not IsNull(b) = " & x

what returns is:
a = cboProjectID
           b = cboProjectID
           u = IsNull(a) = True
           v = Not IsNull(a) = False
           w = IsNull(b) = True
           x = Not IsNull(b) = False

Values for i and m are as above.

Other sources suggested use of the VBA CallByName Function as an alternative but I haven't tried that yet.

Thanks again.
Go to the top of the page
post Mar 4 2019, 07:09 PM

UtterAccess VIP
Posts: 3,123
Joined: 12-April 07
From: Edmonton, Alberta Canada

You have

But I assme strMe is the name of some control, right? then you would want

So you can always reference any control by a given string. You don't want to check if the "string" strMe is null (that is what the above first line of code does).

I think (assume) you want to check if the control is null based on the name of the control in strMe.

So you need to use me("string goes here") and that is/will return the control ojject.

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada
Go to the top of the page
post Mar 4 2019, 07:59 PM

Posts: 150
Joined: 16-October 12

Understood. My error was in equating the string variable and its contents in this context.
Go to the top of the page
post Mar 4 2019, 08:05 PM

UtterAccess VIP
Posts: 3,123
Joined: 12-April 07
From: Edmonton, Alberta Canada


And don't shy away in the future asking as to how one can call some .net code (vb.net). I do this rather often. So while that part of your inquiry did not apply, it still often a great idea and allows you to extend Access applications in a great way.

Good luck - wheels up!

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    5th August 2020 - 12:02 PM