CallByName

Nowhere Man
06-06-2005, 04:29 PM
Hi,

I've recently discovered the great CallByName function. Because I'm still learning about this function I still have a lot of questions.

What I would like to do is make a dynamic listbar. When I click an item on the menubar (from vbAcellerator, the outlook toolbar) I pass the key string to a sub where I retreive the object and method. The string for the object contains the name of a form and the string for the method has the value "Show"

I've discovered that the CallByName function doesn't accept a string as object, is there a way to change the string of the object to an actual object?

this is the code I have right now

Dim key As String
Dim sObject As string
Dim sMethod As String
Dim iPositionSplit As Long
Dim iStrLen As Long

'split the object and method from the string
key = "frmSelLln-show"
iPositionSplit = InStr(key, "-")
iStrLen = Len(key)
sObject = Mid(key, 1, iPositionSplit - 1)
sMethod = Mid(key, iPositieSplit + 1, iStrLen - iPositionSplit)

'call methode
CallByName sObject, sMethod, VbMethod

TeraBlight
06-06-2005, 06:38 PM
Hi,

this is rather similar to the problem I'm having.

But it's a little simpler, because in your case, a Collection does exist inside VB that should solve your problem: It's called "Forms" (surprise!)

I just tried this in a sample project:


Dim myForm As Variant
For Each myForm In Forms
MsgBox myForm.Name
Next


and I get get a series of message boxes with the name of each form in turn.
Now you just need to compare the myForm.Name with your string and brake the loop at that point, and myForm will contain a reference to the form you are looking for:


Dim myForm As Variant
For Each myForm In Forms
If myForm.Name = nameIAmLookingFor Then Exit For
Next

CallByName myForm, [...]


Hope that helps...

edit: Ah, I just noticed that it says in the help that "Forms" contains a list of *all loaded* forms rather than *all* forms in a project. Something to bear in mind!

Nowhere Man
06-07-2005, 01:57 AM
edit: Ah, I just noticed that it says in the help that "Forms" contains a list of *all loaded* forms rather than *all* forms in a project. Something to bear in mind!

I guess I can't use this then, can I??
The form I'm calling is not loaded. It is a big project with about 20 or 30 forms. Some of them are in a ActiveX DLL for scalability reasons. I can't load all the forms in the memory, that would be to much or am I wrong?

Peperl
06-07-2005, 02:37 AM
Hi

Maybe is not complicated as you think, think that you know what is the form that you want to use, so, maye this works:


' assuming that the name of the form
Dim tForm as frmSelLln-show


so then, you have stored that form into an object


hope this helps

Nowhere Man
06-07-2005, 02:46 AM
thx peperl

but I think it's not possible to use this (I'm really a dificult guy to work with :chuckle: ) I don't know what form the user is going to need or what button the user is going to click. So that's why I used the key-value.

everything before the "-" is the object the user needs and the rest is the method. So I have the name of the object I need in a String. All I need to do is use that string to define the object itself. Sounds easy but I really can't figure it out!!

EDIT:
Oh yes, I forgot this. It is a large project so I don't keep al my forms loaded in memory. So I don't think it is possible to search through the Forms collection or any other collection (or am I completely wrong here!?!?)

TeraBlight
06-07-2005, 02:59 AM
Hey, it really *IS* simple. I had no idea it could be this easy... :)

You should be able to adapt this to your needs:

EDIT: If the content of the "methodName" string is always the same (like "Show"), you don't even need the CallByName, you can just do f.Show instead!

Nowhere Man
06-07-2005, 03:15 AM
TeraBlight,
you are a Livesaver!! I would kiss you, but I'm not gonna :D

This works like a charm!!

I just wondered Is it possible to adapt the code so it also accepts functions from a class?? Say if I want to use ActiveX DLL's that contain forms, I can't call the forms within that DLL directly can I?? so I have to make create an object instance of the DLL and then call the forms through a function. Or can I call those forms directly??

TeraBlight
06-07-2005, 03:42 AM
I'm not at all familiar with DLL access, so I don't think I'm the right person to help you out.
However, I'm just playing around with the TypeLib functionalities, and the tutorial I'm using mentions ActiveX DLLs, so this could be exactly what you need! It certainly looks like a promising way to do this kind of thing...

Have a look at
http://gpwiki.org/index.php/VB:Building_a_Better_Scripting_Language_by_Using_Dynamic_Classes

You need to add "TypeLib Information" to your project via Project->References to make the examples work.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum