Constants and Variables (1 Viewer)

Thales750

Formerly Jsanders
Local time
Today, 17:35
Joined
Dec 20, 2007
Messages
1,846

Back to the original problem: Just don't use a Constant. Use a variable.
I'm going to replace the Constants with a Variable. Hope for the best.
Thanks for the suggestion.

PS. I still don't see the need for constants, compared to variables.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
22,522
When exactly is a constant better than a variable?

Depends on your level of caution and/or confidence in your typing. For instance, in some of my hobbyist writing I have fantasy novels. In them I am doing some totally unnecessary - but fun - computations of lunar orbits for a fantasy world with multiple moons of differing periods. In that computation I often need the value of PI. If I declare a global value of

PUBLIC CONST PI AS DOUBLE = 3.141592653589793

it is a lot easier to type 2 letters rather than 16 digits and a decimal point.

If you needed Euler's constant or Avogadro's number or Planck's constant or the speed of light, would you rather type a short name or the long numbers?
 

Isaac

Lifelong Learner
Local time
Today, 14:35
Joined
Mar 14, 2017
Messages
6,274
PS. I still don't see the need for constants, compared to variables.
99.9% of the time, neither do I.

Even for short vs. long, you can still use:
dim strSomething as String
strSomething=adfasdfasdfasdfasdfasdf

I've never (for my own purposes) seen the need to learn to use variables PLUS constants - along with the gotchas you are discovering - when variables always meet my needs on their own.

But as Doc said, I can see the convenience of the one-liner, const something = something. To each his own.
 

Thales750

Formerly Jsanders
Local time
Today, 17:35
Joined
Dec 20, 2007
Messages
1,846
Depends on your level of caution and/or confidence in your typing. For instance, in some of my hobbyist writing I have fantasy novels. In them I am doing some totally unnecessary - but fun - computations of lunar orbits for a fantasy world with multiple moons of differing periods. In that computation I often need the value of PI. If I declare a global value of

PUBLIC CONST PI AS DOUBLE = 3.141592653589793

it is a lot easier to type 2 letters rather than 16 digits and a decimal point.

If you needed Euler's constant or Avogadro's number or Planck's constant or the speed of light, would you rather type a short name or the long numbers?
So you can declare as many as you like. Once they have been set can you change them?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:35
Joined
May 21, 2018
Messages
6,266
Once they have been set can you change them?
It is called a Constant for a reason, that is the whole purpose. A constant is a safety net so that you our someone else does not try to reassign a value to it. If they do the compiler will throw an error at compile time. So the question is not can you use a variable instead, the question is why is a constant better than a variable for a non changing value.
1. Easier (shorter) to declare than a variable
2. Cannot mistakenly be reassigned
3. More efficient since embedded into the compiled code.

You may say that you are never going to mistakenly try to reassign a constant, but little Access programs are not the yardstick for designing a language. You could have a program with 100s of code modules and many people working on portions.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
22,522
So you can declare as many as you like. Once they have been set can you change them?

No - but if they are truly constant, you SHOULD NOT change them. And as we have seen in passing, if you attempt to use a constant incorrectly, you get warned. This is beginning to devolve into a similar thread as the "language has strong typing"/"language has weak typing" debate.

Like any other tool in the tool box, a constant has a purpose and works pretty well to that end. Admittedly, the tools in your tool box do no good if you never need them. But if you ever do need to protectedly declare a constant, you can.
 

cheekybuddha

AWF VIP
Local time
Today, 22:35
Joined
Jul 21, 2014
Messages
998
I'm sorry if this has been mentioned already, but a quick skim didn't seem to show so.

You can't define a constant using a variable or function as part of its definition - it must be defined from a literal value or another constant.

So:
Code:
Const ONE As String = "1"                         ' 1
Const ELEVEN As String = "1" & ONE                ' 11
Const APP_PATH As String = CurrentProject.Path    ' Fails since it gets its value from a volatile function
 

Mike Krailo

Active member
Local time
Today, 17:35
Joined
Mar 28, 2020
Messages
578
The main advantage in my mind to use constants is for code readability and protection from being changed by other code. Access uses them all over the place for that exact reason. Type in ?vbGreen into the immediate window and it displays the value of 65280. Now try to assign a different number to it and it will fail for good reason (Assignment to constant not permitted). How would you like it if someone's code changed all the values of those color constants around?

I used this idea in one of my projects that used different tax types all over the place. There were only three of them 1, 2, & 3. Now if I just used the number in my code, I would have to remember which number went with which type. Not too bad while your creating the code but years later when you have to read your own code, it helps if they are defined as constants (cNoTax, cSalesTax, cUseTax).
 

Thales750

Formerly Jsanders
Local time
Today, 17:35
Joined
Dec 20, 2007
Messages
1,846
No - but if they are truly constant, you SHOULD NOT change them. And as we have seen in passing, if you attempt to use a constant incorrectly, you get warned. This is beginning to devolve into a similar thread as the "language has strong typing"/"language has weak typing" debate.

Like any other tool in the tool box, a constant has a purpose and works pretty well to that end. Admittedly, the tools in your tool box do no good if you never need them. But if you ever do need to protectedly declare a constant, you can.
Devolving or otherwise, I replaced the constant with a public variable.

It works exactly the way I was expecting.My confusion was becasue I had no idea why someone would use a constant. it turns out that a Variable would have been better from the very beginning. Hard coded to the C: Drive was not really a viable method. Especially in this modern world where remote access has grown exponentially.

Thanks All, you were all very helpful.
 
Last edited:

Thales750

Formerly Jsanders
Local time
Today, 17:35
Joined
Dec 20, 2007
Messages
1,846
You may say that you are never going to mistakenly try to reassign a constant, but little Access programs are not the yardstick for designing a language. You could have a program with 100s of code modules and many people working on portions.
Little Access Program? This little ERP Program, plans and runs the entire operations for a Chemical Plant, manages all Lab Samples, Project Manages their NCRs, and schedules rail cars and trailers arrivals and departures, We have a few hundred public variables, and no constants. And after 6 years of all this we never had single incident that you describe.

Have you ever thought about reading How to Win Friends and Influencing People?
Winning Friends
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 22:35
Joined
Sep 21, 2011
Messages
10,316
Now you have jinxed it 😀
 

isladogs

CID VIP
Local time
Today, 22:35
Joined
Jan 14, 2017
Messages
16,062
I much preferred the original comment about 'Wining Friends....'. 🤣

FWIW, I also think you misinterpreted the point made about Access programs.
No individual Access app, whether large or small, ever uses the full range of Access features.
Come to that, nor does any individual Access developer.
So whilst neither I or you may not have much use for constants in VBA, they do have a valid place in VBA as a language.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:35
Joined
May 21, 2018
Messages
6,266
Little Access Program? This little ERP Program, plans and runs the entire operations for a Chemical Plant, manages all Lab Samples, Project Manages their NCRs, and schedules rail cars and trailers arrivals and departures, We have a few hundred public variables, and no constants. And after 6 years of all this we never had single incident that you describe.

Have you ever thought about reading How to Win Friends and Influencing People?
Dude, lighten up. Sorry if you took it personally, but I was making a general statement about Access coding and not a specific comment on your application. I have no idea on the complexity and quality of your code, but it sounds like you put in a lot of work and should be proud. In general Access is a Rapid Development Application and the code is small, that was my point. If you are using it to run a Chemical Plant, that is pretty impressive and definitely pushing the limits of Access. As for winning friends on this site, that is not really my concern. However, after thousands of post on this site and others I have many people who follow me, many appreciative comments, and many people that PM me looking for assistance. So yeah I think I figured out how to make Access friends and "influence" the Access community. You seem very defensive about any even slight criticism or disagreement.

tenor.gif
 

NauticalGent

CopyPaster of the First Order
Local time
Today, 17:35
Joined
Apr 27, 2015
Messages
4,860
MajP CAN be as smooth as sandpaper at times, but he has really made life easy for me a time or two.

I may not want my daughter to date him, but he is one of the first members I look to when I need an answer to a tough situation...
 

Thales750

Formerly Jsanders
Local time
Today, 17:35
Joined
Dec 20, 2007
Messages
1,846
Sorry guys, I miss quoted the name of the book:
It's How to win Friends & Influence People.


There is a real lady Access former MVP that co-wrote the software for the Chemical Plant. I met her here, we are still friends. While working with her my capabilities more than doubled. Everything I had learned before that was from within, with help on specific question from here. The point is, she got that work 100% because she is a decent person, with generosity in both skill and motivation.

Everything we say should be for a purpose, if someone is demeaning, then that is their purpose.


Thanks again for the help.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
22,522
Despite the fact that MajP and I have experienced different situations and often butt heads over those differences, I'd be among the earliest in line to praise his technical skills. There are times that he has the personality of a stinging nettle. Other times he's straight-forward, direct, and helpful. He's human (even though that technically isn't a criterion for membership... I looked it up when I became a moderator.)

Heck, folks know me as pedantic and easily distracted. Some think I'm too whimsical. Others consider that I can be quite brusque at times. But I haven't been called any curse words in months, not since I had to ban three instances of our Hare Krushednut member.

We all come to the table differently, but we all come here to help. We all contribute what we can based on our experiences. We all try to "pay it forward" for help we have gotten from here or other sources. But we aren't automata. (At least, I don't THINK we have any robots here.) So live and let live.
 

Users who are viewing this thread

Top Bottom