I have a string variable assigned a value in Form1, but wish to use this variable (and the same value) in another form within the database (e.g. Form2).
How can this be achieved?
You could create a global variable.
Declare it outside any procedures at the top of Form1, like this:
Public strYourVariable as String
Now, once you have assigned a value to strYourVariable, it will be available in other modules and forms.
The global variables will be reset if you:
Use "End"
Get an unhandled error
Edit your VBA code
Close Access
Related
Say I wanted to have a string containing the user's name, and I wanted to be able to access and/or change that variable across all swift files. For example, I have a view controller file for the user's profile page and a settings page - I want to be able to access and change that variable in either of those files. I have tried making a class file, but whenever I make an instance of that class in my view controller, I am able to access the default value of the variable in that class, but I am not able to permanently change its value.
So essentially, I want to be able to declare a global variable that is accessible by all files in my project, and can be changed by all files in my project.
Nevermind, I decided to use user defaults to save my variables, and I can then retrieve them from any file within the project.
For example:
//Declare this inside your class
let defaults = UserDefaults.standard
//Take any variable like this
var name: String = "Bill"
//and save them to UserDefaults
defaults.set(name, forKey: "name")
You can then access the variable in another class(or in the same class, just drop the "let" before "name") by calling:
if let username = defaults.string(forKey: "name"){
let name = username
The variable is accessible by the unique key you give it when calling the defaults.set() function. You can change the value the same way you initially declared it.
I try to Set a constant which should contain the Connection String for my Database. But the Setup for my macro is so that on the first Sheet in my Excel there are a lot of Buttons. Depending on which Button the user clicks another Module gets executed, but all of them connect to the same DB at some point.
Since I don't want to change the Connection String at many different points it should be done with one Constant.
But I can never tell which Module gets executed an which doesn't.
Therefore I created a new Module which only contains one Line of Code:
Public Const ConnectionString As String = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=X;User=root;Password=X;"
Unfortunately when a Button is clicked and some Module/Sub gets executed, the Variable "ConnectionString" Contains no Value.
How can I make sure that independent of the called Sub/Module the Constant is set?
A Public constant must be used in all contexts (modules, classes, controls, forms etc.), except two cases:
a. Option Private Module is in effect;
b. The constant has also been declared in modules/procedures (Dim ConnectionString As String) but no value has been allocated. If, after declaration, a value would be allocated (ConnectionString = "xxx"), then VBA will return "xxx". So looking for ConnectionString As String in all project, the place of the real problem will be descovered, I think.
But at least part of the credit must go to #BrakNicku, who firstly put in discussion the second possibility...
Take this code for an example:
(() => {
const data = []
const ws = new WebSocket('ws:/localhost:5555');
ws.onmessage = (frame) => data.push(frame.data);
})();
Is it possible to look up the value of data without stopping the application, or breakpointing onmessage and waiting for it to occur? Is it possible to just look up the value of any variable that I know to be stored persistently somewhere?
Variables inside a function are private to within that function scope. Only code inside that function scope can examine them.
If you're in the debugger, you will need to be at a breakpoint inside that function scope in order to see that variable.
Sometimes it's appropriate to move the declaration of a variable to a higher scope so that after it is modified inside some local scope, it's value will persist and can be accessed from a higher scope. I don't know what real problem you're trying to solve here to know whether that makes sense for your situation or not.
More likely, since variables like your data variable get modified at some unknown time, the only way some outside code can know when to look at an updated value is by participating in some sort of event system or callback system that notifies outside that it now has a new value. In that case, it's common to just pass the new value to the callback or along with the event. Then the outside code gets the value that way, rather than having to declare it in some parent scope.
I need to assign a value to a variable in some method as beforeRender, as I do?
(Sails.js v0.11.0)
Example CakePHP:
public function beforeRender(){
...
}
Example Rails:
before_render : ....
You can set locals in many places. It depends on your use case.
If the variable is different for each action, then you can place it inside the action calling the view.
If the variable is different for each request, then you can place it in a policy to set locals.
If the variable is static for a single route, you can put it in your routes.
If the variable is static for an application, then you can place in many places.
You can use Locals or Globals in your rendered view, which means any services you creates (even static objects) can be used inside your renderedViews
http://sailsjs.org/#!/documentation/concepts/Views/Locals.html
http://sailsjs.org/#!/documentation/concepts/Globals
I had declared and used a global variable in ssjs library as below:
var backendDoc:NotesDocument = null;
function savedata () {
print (backendDoc.getItemValueString("fieldname")); // crash here
}
I assigned a document object to it in the Edit button just after changing docuemnt mode from read to edit:
backendDoc = document1.getDocument(); // get backend document from datasource called document1
The code in above function return error NotesDocument.getItemValueString("string")) null. Apparently, the backendDoc is null.
Any ideas how to assign value and use global variable in ssjs library? Thanks in advance
There are 2 problems with your code:
as Michael pointed out: you should use a scoped variable. Global variables in script libraries are actually application global (think applicationScope) and might be unloaded any time if memory gets tight (behavior of them depends on the XPages version)
You can't use NotesObjects here. Between the calls the C Object that backs the JS object is released and your object becomes invalid.
You can either store the NoteId in a scoped variable and retrieve the NotesDocument every time or actually use a JSON structure to keep the values you are interested in and only read/write when actually needed (load/save event). Hope this helps
I think you have to use a scoped variable in which you store the universalid of the document. This can then be used at any script to initialize the backend document.
From a ssjs you can set a scoped variable using the put method and the get method to read the variable. Example to set and read a scoped variable in session scope :
sessionScope.put(“myvar“,“myvalue“)
sessionScope.get(“myvar“)
To learn more about scoped variables watch this
http://notesin9.com/index.php/2009/11/07/episode-4-intro-to-scoped-variables/