Variables changing to fields (1 Viewer)

chrisdd

New member
Local time
Today, 05:42
Joined
Jun 25, 2021
Messages
14
I'm sure there is something very very simple that I'm missing here but I have 2 global variables FDateFrom and FDateTo which contain 2 dates for a search.

I need to output these in a text field - every time I add the name of the variables to the string Access overrides me and changes them to fields???

="Date Range: " & [FDateFrom] & " to " & [FDateTo]

Should be

="Date Range: " & FDateFrom & FDateTo
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
, 23:42
Joined
Feb 28, 2001
Messages
27,395
In what context? If you are defining that as the .ControlSource of a text box, you cannot use variables in that context. You have a couple of choices.

Either in the form's .Current event, manually load those variables to the target text field OR create a function that returns the desired values.

Access doesn't recognize variables in the .ControlSource property. (Or in other control properties, either.)

It has to do with the form's controls being part of the Graphic User Interface (GUI) and variables being part of the Applications Program Interface (API) - which are two different spaces.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:42
Joined
Sep 21, 2011
Messages
14,533
So you are going to have
01/05/202416/05/2024 ? As your DateRange?
 

chrisdd

New member
Local time
Today, 05:42
Joined
Jun 25, 2021
Messages
14
In what context? If you are defining that as the .ControlSource of a text box, you cannot use variables in that context. You have a couple of choices.

Either in the form's .Current event, manually load those variables to the target text field OR create a function that returns the desired values.

Access doesn't recognize variables in the .ControlSource property. (Or in other control properties, either.)

It has to do with the form's controls being part of the Graphic User Interface (GUI) and variables being part of the Applications Program Interface (API) - which are two different spaces.
Many thanks for this - working now!
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 05:42
Joined
Sep 12, 2006
Messages
15,729
Note that for most uses dates have to be surrounded by ## characters. Also some usages will treat the dates as US dates.

So "between #1/5/24# and #31/5/24#" can end up getting treated as between Jan 5th and May 31st. It's worth testing to make sure it does what you want.
 

DickyP

Member
Local time
Today, 05:42
Joined
Apr 2, 2024
Messages
87
Note that for most uses dates have to be surrounded by ## characters. Also some usages will treat the dates as US dates.

So "between #1/5/24# and #31/5/24#" can end up getting treated as between Jan 5th and May 31st. It's worth testing to make sure it does what you want.
Why I alway, when specifying dates, using #1/MAY/2024# and #31/May/2024# in the example above.

Blame it all on Gates and Allen when they invented MS-DOS in days of yore, and IBM adopted it to save money. At least in those days when we had the choice of CP/M (vastly superior in my opinion) you could only use US for format dates on CP/M - not great for a Brit but at least we knew where we stood.
 

theDBguy

I’m here to help
Staff member
Local time
, 21:42
Joined
Oct 29, 2018
Messages
21,581
Just in case it might be useful, look up TempVars too. I think you can use them directly in Control Sources without using code.
 

June7

AWF VIP
Local time
, 20:42
Joined
Mar 9, 2014
Messages
5,501
TempVars can be referenced in ControlSource and queries and macros and they retain values even when unhandled runtime error is triggered (globals do not).

Does require code (macro or VBA) to establish and populate.
 

Users who are viewing this thread

Top Bottom