Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

define value outside module or in an *.ini file

Status
Not open for further replies.

AlexFeddes

Programmer
Jan 17, 2009
22
NL
Hello,
I think I posted this question in the wrong forum earlier - appologies.

But I would like to define text and values 'globally' so that they can be used throughout all modules.

Below is an example of what I want.
Preference would be to define MB1 and MB2 in an outside msgbox.ini file (C:\msgbox.ini) and let the module call for that
'[MsgBox]
'MB1 = "Test 1"
'MB2 = "Test 2"


Function ACF(MsgBox) As Boolean
'This doesn't work yet
MB1 = "Test 1" 'This line I want to get from a global variable so that it is written in the Function, but called up in the various underlying modules
MB2 = "Test 2"

End Function

Sub AA01()
MB1 = "Test 1" 'This line I actually want to get from a global variable (*.ini file) instead of declaring them in each module seperately

MsgBox MB1

End Sub

Sub AA02()

MsgBox MB2

End Sub

Sub AA03()

MsgBox MB1

End Sub

Thank you for looking into this.
Alex

 
What part about using the config file are you specifically having difficulty with?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Hello Harry,
I would like either the code (statement) to call for data in the C:\msgbox.ini file.

For example:
Sub AA01()
Call from C:\msgbox.ini MB1

MsgBox MB1

End Sub

This would then open a message box with the text defined in the msgbox.ini file:
[MsgBox]
MB1 = "Test 1"

Alex

 
Might be worth checking out faq222-3955

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I'll do that - thanks and let you know if it works.

But do you know any 'simple' solution by writing it first of all as a global variable at the top of my procedure?

For example:
> Function ACF(MsgBox) As Boolean
'This doesn't work yet
MB1 = "Test 1" 'This line I want to get from a global variable so that it is written in the Function, but called up in the various underlying modules
MB2 = "Test 2"

End Function

Sub AA01()
MB1 = "Test 1" 'This line I actually want to get from a global variable (*.ini file) instead of declaring them in each module seperately

MsgBox MB1

End Sub

Sub AA02()

MsgBox MB2

End Sub

Sub AA03()

MsgBox MB1

End Sub
 
Just declare the variable as Public at the top of your code (outside of any procs or functions) as you say, or as Public in a module, or even a Public Const so it can't be changed.

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Hello Harry,
can you give me a sample code how to do this - I've given you the complete sample code as I wanted to do this, but this didn't work - can you re-write this so it will work.

Thanks,
Alex
 
Use Public Const in the Declarations section of your module.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Can someone give me an example of the code that will make this work?

Below doesn't return the value!
Public Function ACF()
MB1 = "Test 1"
MB2 = "Test 2"

End Function

Alex
 


Code:
Public Function [b]ACF[/b]()
  [b]ACF[/b] = "Test 1"
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Many thanks for below code - that answered the first part of the question. Second about the *.ini file remaining!

Is there anyone that can supply the code to call for data from an *.ini file?

Regards,
Alex


Public Function ACF()
ACF = "Test 1"
End Function

Sub AA01()
MsgBox ACF

End Sub

Sub AA02()
MsgBox ACF

End Sub
 




If you want different values you'll have to supply the ACF function with one or more arguments.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,
can you give me an example of that?

I take it that you're talking about the function and not calling values from an text (ini) file?

Alex
 
Remember this?;
>Just declare the variable as Public at the top of your code (outside of any procs or functions) as you say, or as Public in a module, or even a Public Const so it can't be changed.

I guess you will be requiring an example of that;

Public MyVar as String

or

Public Const MyConst = "some text
 
Is there anyone that can supply the code to call for data from an *.ini file?

Look at your original post in the Office forum. I posted an example there. I am not going to bother repeating myself here.

You use PrivateProfileString.


But really, you need to do some more of your own searching.

Below doesn't return the value!
Public Function ACF()
MB1 = "Test 1"
MB2 = "Test 2"

End Function
You state in your handle you are a programmer. Surely you know how a Function works, and why your code "doesn't return the value!"

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hello Gerry and others,
many thanks for your feedback - I need some time to try and make an ini file work, though the rest of this week is taken up by other things.

Thanks for your help and feedback and I'll let you know once I've found a workable solution.

Gerry you are right that my programming skills are not great (yet) which I stated in my profile: I've signed up as a programmer, but I'm a private learner and am limited in my programming skills. Though I'm impressed with what you can achieve with combined efforts.

Thanks once more,
Alex
 
I'm a private learner and am limited in my programming skills. "

Me too. Which is why, for myself personally, I could not bring myself to label myself as a programmer.

Let me help a wee bit.
Code:
Public Function ACF()
  ACF = "Test 1"
End Function
is a pointless procedure.

1. Functions are used to return a single value, and, as a best practice, should be declared as a data type.
Code:
Public Function ACF() [b]As String[/b]
  ACF = "Test 1"
End Function
However, as there are NO other instructions, the Function does one thing, you can do the same thing with:
Code:
Public CONST ACF As String = "Test 1"
Using Functions and Subs only makes sense when they really DO something. Not declare something.

Code:
Function ACF(MsgBox) As Boolean
'This doesn't work yet
MB1 = "Test 1" 'This line I want to get from a global variable so that it is written in the Function, but called up in the various underlying modules
MB2 = "Test 2"

End Function
is not really a Function...it should be a Sub. Function always set (return) the VALUE of the function.

Further, the function takes in a required parameter - MsgBox - but never declares the data type, nor use that parameter. This is pointless. Next, the data type of the function is declared as Boolean (True/False), but there are no boolean values within the function. "Test 1" and "Test 2" are strings.

It may help to state clear and considely what it is you are trying to do. As I stated in the other post, you use System.PrivateProfileString and the filename, Section, and key to access data from an .INI file.

So you can get the values of MB1, or MB2 in the .INI file easy enough:

[MsgBox]
MB1 = "Test 1"
MB2 = "Test 2"

But I am very unsure of what the heck you want to do with them. For example, you could do something like:
Code:
Function Yadda (strIn As String) As String
   Yadda = PrivateProfileString("c:\msgbox.ini, _
          MsgBox, strIn)
End Function
This is a FUNCTION, and returns a string value. Yadda = a string. In this case, it is the KEY value (represented by the variable strIn), of the Section [MsgBox], in the file c:\msgbox.ini.

So......
Code:
Option Explicit
Function Yadda(strIn As String) As String
   Yadda = System.PrivateProfileString("c:\msgbox.ini", "MsgBox", strIn)
End Function

Sub tryIt()
   MsgBox Yadda(InputBox("Type either MB1 or MB2."))
End Sub
BTW: I would be very careful about using reserved words like MsgBox. Even if it is a slightly different spelling.

The Sub tryIt uses a SINGLE instruction to display a MessageBox that gets the return from Yadda, after first getting the return from an InputBox (that asks for you to type in MB1, or MB2) and passes that to Yadda as a required parameter.

If you type in MB1 (and hit Enter....or if you are one of those weird people that lift up their hand from the keyboard, reach over to the mouse, move the mouse pointer to the OK button, and then click the left button....) - then you get "Test 1"

If you type in MB2, you get "Test 2".

Now this does NOT qualify as a global variable. VBA has to go out and GET the value every time you use it, but it can be used globally. You can use the same method in any module you like. As long - of course - as the Function is in a standard module!


"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top