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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make code, fields public??

Status
Not open for further replies.

DahMurf

Programmer
Apr 12, 2003
44
US
Let me start by saying I'm really lost! I know I don't have the terminology down right so I'm just going to state what I want to do & hope that someone can lead me along in a simple way with a simple example. I have a few different questions. I’ve made some complex code work but I haven’t done anything globally outside of any given form. I think that’s where I’m getting lost.

Scenario:
The user opens FormOne and keys some data. A value is not found in a list for one of the fields. I have my own code to handle this condition that in turn asks if the user would like to add that value. If the user chooses to add that value then FormTwo will be opened. When they are done with FormTwo (on close) I’d like to store just one of the fields that was keyed on FormTwo to be passed back to be displayed on FormOne.

I have a few questions for this scenario.
1) I’ve seen written that I can make a public field to hold the value to be accessed within the application. My problem is how & where do I declare the public field.

2) Do I need to fully qualify the field to access it in FormOne. If so, what would the fully qualified text look like?

3) The code I have for handling the not found in list is something I’d like to use in more then one form. How can I take this code and put it in a public place to be accessed by more then one form? I need some specifics too, as in click on the module tab and create a new module named whatever or click on the Macro tab. I’d also need to pass a value into the code.

I guess I’m just not getting how to go global and what the code would be called. I see functions & modules but I don’t quite get the difference.

Any help would be appreciated!
 
To create public variables, in the database window select the button labeled Modules and then select New (you're opening up a new module). Now declare your variables like this:

Public MyFirstVariable as String

I usually have just one module that contains all of my public declarations and I name the module basPublicDeclarations.

If you want to create a function/sub that can be seen by all forms, etc. Do the same thing as above. Create a New Module and simply add your code (by default, it is a public function/sub). For example,

Function MyNewFunction()
...
End Function

If you are going to keep opening/closing the form, you might consider changing its visible property from true to false.

To access a control on another form, one syntax is:

Forms!YourFormName!YourControlName.Value
 
Thank you so much! I got the public field to work.

My next question is how do I call (what's the syntax to call) my new function from within the AfterUpdate private sub of my form?

Thanks!
 
Thanks for the response! That's what I tried but I keep getting the message:
Compile error:
Expected variable or procedure, not module.
I get this when I get into my sub that contains my call.

I've never done a function before so I may be missing something simple. Like putting it in the wrong place, naming it wrong. I don't know. I saved my module with the same name as what I called my function is that ok?

Any help would be greatly appreciated!
 
OHHHH! Nevermind!!!!
I just found that you can't name your module the same as the function.
I knew it would be something simple! Yeah!
Thanks!
:D
 
Next problem. I'm trying to send a value back from my function. I see the value set by viewing the contents of the function name while in the function. When I return to the sub that called the function the value is gone.

Is there some kind of public declaration I should be making in the function? I could set a different public field to hold the value but I thought the function could return the value itsself without an additional field. Just trying to learn how do to this correcly!

Thanks for any help!
 
Nevermind again! I had to declare a field & assign it to the function where it was called from within my sub.

dim NewValue as string
NewValue = MyFunction(Myinitialvalue)

Do you ever get the feeling you're talking to yourself. I think they only say you're crazy when you answer yourself so I'll consider this answering the other people that wanted to know too!

Thanks for the help to those that helped!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top