Global variable using automation

rushwire

Registered User.
Local time
Today, 01:52
Joined
Jul 13, 2010
Messages
11
Does anyone know how to get the value of a global variable in another Access application using automation (or any other way)?

I am unable to change any of the code in the application that is being referenced by automation.

Thanks for your help.

James
 
Insert a module via VBA code

Does anyone know how to insert a module via VBA code into another application via automation. I do not want to import a module.

I would like to do something like below. However the line "dbs.Modules.Add("MyNewModule") is not correct and will not compile. I have just shown it to demonstrate what I am looking for.

sDatabase = "C:\Test.mdb"

' Opens up the database
Dim dbs As Access.Application
Set dbs = CreateObject("Access.Application")
dbs.OpenCurrentDatabase (sDatabase)

dbs.Modules.Add("MyNewModule")

dbs.Quit
Set dbs = Nothing
 
Re: Insert a module via VBA code

Please explain why you need to do this for there might be a better way to accompish your goal.
 
I think that a global variable's value must be returned to a function and from that function you can get the value. Or if it was a TempVar then it would be possible.

Why can't you amend the code that is within? Copywright issues?
 
I am not sure what exactly a TempVar is so thanks for that I shall do some research.

I am unable to change the code within as the code I am writing is auditing software to audit several 100 databases. Hence no ability to change code.

James
 
A TempVar became available in Access 2007 and above and must already be set in the db.

Your only way would be to Insert the lines as a new global function and set the variable to that function. Then you can get the value from there.
 
Re: Insert a module via VBA code

I am trying to read the value of a global variable in a separate access application. I had the thought that if I create a module within the separate application I could add a function to it using "Module.InsertLines" and then using "eval", I could read the value of the global variable.

This is also connected to another of my posts.

http://www.access-programmers.co.uk/forums/showthread.php?t=195878

James
 
Re: Insert a module via VBA code

Why not insert into a current module?
Code:
dim linesCount as long, mdl as Module

set mdl = dbs.Modules(1)
linescount = mdl.Countoflines + 2

mdl.insertlines "Public Function GetVariable()", linesCount
...
So on and so forth
 
Re: Insert a module via VBA code

Thank you for your suggestion. I actually tried this and it did work although additionally I have to ensure that the module is not a form module as if it is you are not able to access the function.

I was just hoping that there was a way to add a module as I felt this would be a tidier solution.

Your help is much appreciated though.

Thanks
James
 
Re: Insert a module via VBA code

After reading both posts it appears that you are attmepting to bastardise a mdb that you are not the author of. The implications of this can lead to loss of any contracted support you may have on the said applications. Also if they have been distributed correctly, ie mde or accde or that the vba has been password protected no matter what code you have it will not be able to include it.

If you think about it logically you could enter any amount of code into an alien mdb, but unless you some how magically add futher code to existing modules how are you going to integrate it with existing vba?

If I was the author of a program I would be very concerned if someone has the ability to amend or add to my code without my knowledge.

First thing I would do if I discovered that would be to revoke my support contract due to external interferance.

Alsohow do you know what naming conventions have been adopted, and what would be the implications of inserting ambigious names and functions into the target mdb.

I know you have discussed the moving of public variables from one to another but in what circumstances would you want/need to do it?
 
Re: Insert a module via VBA code

Well, there you go. Ponder on the points DCrake has come up with. Without valid reasons we wouldn't be able to show you how to create a module in access using vba.
 
Re: Insert a module via VBA code

DCrake,
I can assure I am not doing anything illegal and nor am I breaking copyright. These are mdbs created by the company I work for. Most of these applications have a global variable all called the same name which is "VersionNo" which is a variable used to hold the version of the application. (not the way I would do it, but the way it was done)

I am trying to write a bit of code that goes into each database, gets the value of that global variable, logs it into a text file and then moves on to the next database. I don't want to do this manually as there are several hundred databases and nor in fact do I wish to change the code in anyway. My preference would be just to read the variable directly but I couldn't work out how to do that. So in fact if I was to insert code to solve this issue, I would insert the code into a new module, get the value, and then delete the whole module. This is one reason why I would prefer to do it as a separate module as that would keep it clean and away from everything else. It is not in my interest to change the application. In fact I would rather not change the application!

So now you have all the background facts (which to be honest were irrelevant to the actual original question), perhaps you could help answer the question as opposed to worrying about copyright infringement and bastardising mdbs etc. People need to do numerous things in code, just because you are unable to see the immediate use does not necessarily mean it is unlawful.

James
vbaInet - Thanks again your earlier constructive suggestion.
 
Re: Insert a module via VBA code

So now you have all the background facts (which to be honest were irrelevant to the actual original question), perhaps you could help answer the question as opposed to worrying about copyright infringement and bastardising mdbs etc. People need to do numerous things in code, just because you are unable to see the immediate use does not necessarily mean it is unlawful.

James
vbaInet - Thanks again your earlier constructive suggestion.

James -

It is not irrelevant and we are entitled to know because we do not allow ourselves to knowingly help with illegal activity and frankly we don't know who you are (and you aren't a longtime member of the forum) and whether you have the right to do this. So, the questions ARE appropriate as there are a lot of people who are out on the Internet trying to hack things.

So, don't get all worked up about the questions he asked.
 
Re: Insert a module via VBA code

Okay, so you say that you have a global variable which has the version number. So where does IT get the value from? It can't get it out of thin air and it doesn't have it automatically - something has to assign it. So where is it really stored?
 
Re: Insert a module via VBA code

Bob is trying to get you to think beyond the variable. It could be getting its value from a table perhaps? If it is you may want to query that table instead. Since you have access to the db it shouldn't be difficult finding the table.
 
Re: Insert a module via VBA code

The variable is a constant and is set in the declarations.​
 
Re: Insert a module via VBA code

So, you could just use the TransferDatabase command to move a module over to the database.
 
Re: Insert a module via VBA code

It seems the OP wants to extract certain records based on the Version Number from that variable? Or something along those lines? Might be tedious transferring.
 
Re: Insert a module via VBA code

So here it is:
Code:
    Dim vbComp As VBIDE.VBComponent
    
    Set vbComp = dbs.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
    
    vbComp.Name = "Name of New Module"
Then you can refer to the module (via the other code given) using that new name.
 
Re: Insert a module via VBA code

So here it is:
Code:
    Dim vbComp As VBIDE.VBComponent
    
    Set vbComp = dbs.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
    
    vbComp.Name = "Name of New Module"
Then you can refer to the module (via the other code given) using that new name.


Why not just export a new module to the database:
Code:
DoCmd.TransferDatabase acExport, "Microsoft Access", "YourPathAndFileNameToSendTo", acModule, "ModuleNameHere", "ModuleNameThere"
 

Users who are viewing this thread

Back
Top Bottom