PDA

View Full Version : Solved: Problem of accessibility within a project



jungix
06-28-2006, 06:25 AM
I have a project divided into several subs, with some global variables. It works perfectly on 2 computers (mine and another one that I tested), but it doesn't work on a 3rd computer.

The code looks like this:


Public Sub Main ()

Dim k As Integer

Call Macro1

...


End Sub

Public Sub Macro1 ()

k = ...

End Sub


I don't declare k in Macro1 again, and the 3rd computer gives an error "Can't access to the project" when I try to access k. Why isn't it working? What have I got to change on this computer so that everything works properly

lucas
06-28-2006, 07:34 AM
Discussion on global variables and scope that might be what you need:
http://vbaexpress.com/forum/showthread.php?t=8554

jungix
06-28-2006, 08:05 AM
Hum, that's crazy

I didn't precise it, but Macro1 is in another module.

K is declared in the main Sub, but I call Macro1 before ending Sub, so k should still exist, shouldn't it?

What is most crazy is that everything works perfectly well on my computer, so maybe it's just a question of trust in the options somewhere. I guess I could just declare the variable as Public, but I'd like to understand.

lucas
06-28-2006, 08:24 AM
A good quote from Gerry on the subject:


Well, yes of course you can write it each procedure. But No, No, No - it will not have the same effect as declaring it as Public. The value would never be passed from procedure to procedure.

As stated, a variable declared in a procedure only exists in that procedure. You can have 10 procedures with each one having a variable named myVar. Each myVar is absolutely independent of the others. They ONLY hold value within that procedure.



Quote by Ken Puls on the subject:


This is an issue of "scope."
Scope describes the "visibility" of a variable. If you make your variable declaration inside a routine (as you have in this case) it is only available inside that routine.

You can raise the scope to module level by making the declaration at the start of the module, outside any routines. It will now be visible to all the module's procedures and functions

jungix
06-28-2006, 09:19 AM
Even when I used public names for variables, I got an error for the following line, which works perfectly fine on my computer:

Left(Text, Len(SearchString) + 1) = SearchString & " "

where Text and SearchString are strings. Do you need an add-in to use the Left function which might not be installed on the other computer?

ALe
06-28-2006, 10:13 AM
why don't

Public k As Integer
Public Sub Main ()



Call Macro1

...


End Sub

Public Sub Macro1 ()

k = ...
End Sub

jungix
06-28-2006, 10:18 AM
Yes that's what I did but I still have another problem a bit further

Left(Text, Len(SearchString) + 1) = SearchString & " "

where everything is defined in the good sub, and it still doesn't work.

lucas
06-28-2006, 10:23 AM
what is the error?

jungix
06-28-2006, 10:39 AM
The erros is still: "Can't find project or library"

lucas
06-28-2006, 10:46 AM
are they all the same version of excel?
Sounds like a missing reference.
Check the vbe tools-references

jungix
06-28-2006, 10:55 AM
Both are Excel XP Professional with Frontpage versions, and have the same vbe tools-references.

lucas
06-28-2006, 11:24 AM
you have dimmed text and searchstring globally?

mdmackillop
06-28-2006, 11:31 AM
Why not just pass k to the sub


Public k As Integer
Public Sub Main()
k = 5
Call Macro1(k)
End Sub

Public Sub Macro1(k As Integer)
MsgBox k
End Sub

jungix
06-28-2006, 11:35 AM
No, but Text is declared in this Sub and SearchString is an argument of the Sub and is already used before

jungix
06-28-2006, 02:08 PM
Up

the Left Right and Mid method cause problems. What have I got to add to enable them. On all computers I have exactly the same references:

Visual Basic For Applications
Microsoft Excel 10.0 Object Library
OLE Automation
Microsoft Office 10.0 Object Library

Do you know what library is needed to use this functions on strings?

mdmackillop
06-28-2006, 02:13 PM
Look for any references marked "Missing" and remove the check next to them.

ALe
06-29-2006, 01:39 AM
Once I had the same problem and I'm not sure about the reason.

It occured to me for a project with a richtextbox whose library was missing in a pc. When I removed the richtextbox the error was still there. I solved it changing Mid in Mid$ and Left in Left$ and it worked but I don't know the reason.

ALe
06-29-2006, 01:42 AM
In conclusion I agree with mdmackillop. It must be related to some libraries that your project show as missing

jungix
06-30-2006, 05:16 AM
That worked (Pdfdistiller was missing), I don't know how it was related since I didn't use it but now it's working. Thanks

ALe
06-30-2006, 05:36 AM
Jungix, mark this thread as solved

jungix
06-30-2006, 12:13 PM
I'd like to, but I don't know how to edit my title

mdmackillop
06-30-2006, 12:25 PM
to?

lucas
06-30-2006, 01:34 PM
Hey jungix,
you can mark your thread solved using the thread tools at the top of the page.