Refer to X String from another public function or form (1 Viewer)

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
I have a Public Function like the example below. X is a String inside the function and takes value from a field in a form. The question is how can I refer to String X through another function or through a another form? I want to use another function to refer to the result of X in SetValuesFromForm, can something like this be done?

Public Function SetValuesFromForm()
Dim X As String

X= Forms!Form1.TextField1

End Function
 

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
Make it Global or a TempVar
Make it Global or a TempVar
How can i do it Global? Its about 30 fields..so i thing TempVar for each maybe its not good idea
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:25
Joined
May 21, 2018
Messages
8,533
if that form is open then
Code:
Public Function GetValueFromForm() as variant
  getValueFromForm= Forms!Form1.TextField1
End Function

if you are setting a value and then closing the form I would use the Tempvars as already said
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 18:25
Joined
Sep 21, 2011
Messages
14,314
Make it Global or a TempVar
How can i do it Global? Its about 30 fields..so i thing TempVar for each maybe its not good idea
One minute it is one variable x, now it is 30??? :(
Write them all to a temp table then?
Put them all into a hidden form and refer to that?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:25
Joined
May 21, 2018
Messages
8,533
Its about 30 fields..so i thing TempVar for each maybe its not good idea
Can you explain why you are saving 30 Fields? There is probably a way smarter approach. Use Access as it was designed. Do not try reinventing the wheel. A beginner who does not know what Tempvars are or a global variable does not need to be writing code to save 30 variables.
 

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
Can you explain why you are saving 30 Fields? There is probably a way smarter approach. Use Access as it was designed. Do not try reinventing the wheel. A beginner who does not know what Tempvars are or a global variable does not need to be writing code to save 30 variables.
because i'm getting values bu running external service from Postgres
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:25
Joined
May 21, 2018
Messages
8,533
Still no idea how
' i'm getting values bu running external service from Postgres"
and this
"I want to use another function to refer to the result of X in SetValuesFromForm, can something like this be done?"
are in any way remotetly related. If 30 values have an associated name then may want to use a Dictionary object. TempVars would still work since it is a collection with an indexed name. What is the issue with using tempVars? But is not storing a name and a value then the array would be fast and efficient.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:25
Joined
Feb 19, 2002
Messages
43,296
i'm getting values bu running external service from Postgres"
Can't you just link to the table? Then you don't need any code.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:25
Joined
Jan 20, 2009
Messages
12,852
Referring to anything outside a Public function other than by passing it through the function parameters is very, very poor design. Using Public variables or TempVars is just plain wrong. (Don't bother arguing.)

Encapsulation is a fundamental programming that would be breached by such suggestions.


If the function needs to refer to 30 members of a form then it should probably be a Method of that form.

If it needs to refer to multiple forms then the form objects should be passed as parameters so their members can be referred to from inside the function. However it sounds to me like the whole structural concept is likely to be flawed.
 

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
As I suspected, I probably haven't made it clear what the problem is

From an external server in the company, I receive data as a string from a Postgres table. I get this data through a service (java scripts) and it is in real time. I call the service through a module (ReadServerData) and I get all the data as a string. I read this data from a form (Login) if I call ReadServerData (public function).

What I am asking is how can I selectively read some of the string data which is inside the ReadServerData Function. For example, I want to refer to only one of them in the login form, let's say the Serial No of the application, which is SerialNo (Dim SerialNo As String into public function)

That's all.
 

ebs17

Well-known member
Local time
Today, 19:25
Joined
Feb 7, 2020
Messages
1,949
I receive data as a string
You mean you have to split the string into individual pieces of information? That would then be simple string processing with methods like
- Left/Right/Mid/Instr/Len
- Split
- regular expressions
 

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
You mean you have to split the string into individual pieces of information? That would then be simple string processing with methods like
- Left/Right/Mid/Instr/Len
- Split
- regular expressions
Already are split...! I just want to refer to just one String of them from another form, routine or function...! How?
 

ebs17

Well-known member
Local time
Today, 19:25
Joined
Feb 7, 2020
Messages
1,949
How are they there, these substrings?

The belief that others are constantly looking over your shoulder is widespread, but it is less based on facts than you might think.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:25
Joined
Feb 19, 2013
Messages
16,618
you say the data is already split - split how? separate rows? into an array of some kind? and split with a field description and value? just a value?

Why can't you provide an example (doesn't have to be real data, just representative) of what you are looking at?
 

Cotswold

Active member
Local time
Today, 18:25
Joined
Dec 31, 2020
Messages
528
Am I missing something here? From #1 I would have the solution as :
Code:
Public Function SetValuesFromForm() as String
    Dim X As String

    X= Forms!Form1.TextField1
    SetVaulesFromForm= X

End Function ' SetValuesFromForm()

Then on any form or another Function add the lines

Dim Y as string
Y = SetvaluesfromForm()

Is that not the solution to #1??

If the function is in a module, I have never found the need to prefix them Private or Public. I just use Function().
Also, it is good practice to set the type of variable for all variables passed to and from a Function.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:25
Joined
Feb 19, 2013
Messages
16,618
my guess would be

Code:
Function SetValuesFromForm(fname as string) as String

   SetValuesFromForm= Forms!Form1(fname)

End Function
 

gstylianou

Registered User.
Local time
Today, 20:25
Joined
Dec 16, 2013
Messages
357
Am I missing something here? From #1 I would have the solution as :
Code:
Public Function SetValuesFromForm() as String
    Dim X As String

    X= Forms!Form1.TextField1
    SetVaulesFromForm= X

End Function ' SetValuesFromForm()

Then on any form or another Function add the lines

Dim Y as string
Y = SetvaluesfromForm()

Is that not the solution to #1??

If the function is in a module, I have never found the need to prefix them Private or Public. I just use Function().
Also, it is good practice to set the type of variable for all variables passed to and from a Function.
wHERE YOU WAS ALL THIS TIME?? :))
THANKS A LOT
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:25
Joined
Jan 20, 2009
Messages
12,852
If the function is in a module, I have never found the need to prefix them Private or Public. I just use Function().
Also, it is good practice to set the type of variable for all variables passed to and from a Function.
A Function in a Standard Module is Public by default. If they don't need to be called from outside the module (ie only called by other Subs or Functions in the module) then they should be declared Private, hiding them from the rest of the project.

Functions in Class or Object Modules are Private by default. They become Methods of the Object or Class when declared Public.

Types of the parameters and return should be declared or they will be unnecessarily complex Variants by default.

Parameters should also be declared as ByVal or ByRef. ByVal means the value of the argument is passed. ByRef means a reference to the argument is passed. Changes to the value of the parameter inside the function or sub will be reflected back to the argument that has been passed. This is one way a function or sub can return multiple values.

Without the declaration, the parameters are ByRef by default. Arrays are always ByRef even if declared ByVal. The mismatch is ignored by the compiler.

Putting parentheses around an argument for a ByRef parameter in the call will cause it to be passed as a value instead.
 

Users who are viewing this thread

Top Bottom