dbTalk Databases Forums  

Prob with code in declarations section of form module ...

comp.databases.ms-access comp.databases.ms-access


Discuss Prob with code in declarations section of form module ... in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
MLH
 
Posts: n/a

Default Prob with code in declarations section of form module ... - 04-12-2009 , 08:02 AM






Am trying to use the following in declarations section of form module.
Of course, it won't compile. What's the best way to set my constants
inside the If-Then. I need a work-a-round. Can I set Form module
constants outside the form module's uppermos declarations section?

Option Compare Database
Option Explicit

Const SmallEnvWt As Double = 0.2059
Const LargEnvWt As Double = 0.625
Const PaperWeight As Double = 0.1666667
If Now < #5/11/2009# Then
Const FirstClassStamp As Double = 0.42
Const FirstClassOn9x12 As Double = 0.83
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.7
Const RetRcpt3800 As Double = 2.2
Const NonMachinableSurcharge As Double = 0.2
Else
Const FirstClassStamp As Double = 0.44
Const FirstClassOn9x12 As Double = 0.88
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.8
Const RetRcpt3800 As Double = 2.3
Const NonMachinableSurcharge As Double = 0.2
End If


Reply With Quote
  #2  
Old   
Marshall Barton
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 09:19 AM






MLH wrote:

Quote:
Am trying to use the following in declarations section of form module.
Of course, it won't compile. What's the best way to set my constants
inside the If-Then. I need a work-a-round. Can I set Form module
constants outside the form module's uppermos declarations section?

Option Compare Database
Option Explicit

Const SmallEnvWt As Double = 0.2059
Const LargEnvWt As Double = 0.625
Const PaperWeight As Double = 0.1666667
If Now < #5/11/2009# Then
Const FirstClassStamp As Double = 0.42
Const FirstClassOn9x12 As Double = 0.83
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.7
Const RetRcpt3800 As Double = 2.2
Const NonMachinableSurcharge As Double = 0.2
Else
Const FirstClassStamp As Double = 0.44
Const FirstClassOn9x12 As Double = 0.88
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.8
Const RetRcpt3800 As Double = 2.3
Const NonMachinableSurcharge As Double = 0.2
End If

Not using the Now function. Check VBA Help for Directive
where you can see how to use the compile time #If instead of
the runtime If. But the #directives only recognize literal
values (and #Const names) so they can not use runtime
functions such as Now(). Besides, what you are trying to do
is change the modules code, which I think is a terrible
thing to do every time the a program is opened.

Maybe you should rethink this to use a class (or, yuck,
global variables) to set up the values values of ordinary
variables at runtime.

OTOH, Lyle has been known to come up with innovative ways of
doing things that many folks think is impossible.

--
Marsh


Reply With Quote
  #3  
Old   
tina
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 09:23 AM



if those values will change as of x date, they can change again farther in
the future. if you hard-code them in a module, it will take a programmer to
change them each time that happens. i'd recommend putting the values into a
table, so your customer/user has access to update them as the needed,
without requiring the services of you or another programmer. something like

tblConstants
ConstID (pk)
ConstName


ConstID ConstName
1 SmallEnvWt
2 LargEnvWt
3 PaperWeight
4 FirstClassStamp
5 FirstClassOn9x12
6 OneOunceJumpRate
7 Certified3811
8 RetRcpt3800
9 NonMachinableSurcharge

tblConstantValues
cValID (pk)
ConstID (foreign key from tblConstants)
cValue
cDate

cValID ConstID cValue cDate
1 1 0.2059 1/1/09
2 2 0.625 1/1/09
3 3 0.1666667 1/1/09
4 4 0.42 1/1/09
5 5 0.83 1/1/09
6 6 0.17 1/1/09
7 7 2.7 1/1/09
8 8 2.2 1/1/09
9 9 0.2 1/1/09
10 4 0.44 5/11/09
11 5 0.88 5/11/09
12 6 0.17 5/11/09
13 7 2.8 5/11/09
14 8 2.3 5/11/09

the 1/1/09 date is a dummy date, of course; you'd have to change it to the
date of the last change, or to whatever effective date makes sense for the
use you're putting the data to.

putting the values into an updateable table means more work for you on the
programming side to ensure that the appropriate values are used for the
circumstances. but that one-time setup work will pay off in making the
database user-maintainable, rather than programmer-maintainable.

hth


"MLH" <CRCI (AT) NorthState (DOT) net> wrote

Quote:
Am trying to use the following in declarations section of form module.
Of course, it won't compile. What's the best way to set my constants
inside the If-Then. I need a work-a-round. Can I set Form module
constants outside the form module's uppermos declarations section?

Option Compare Database
Option Explicit

Const SmallEnvWt As Double = 0.2059
Const LargEnvWt As Double = 0.625
Const PaperWeight As Double = 0.1666667
If Now < #5/11/2009# Then
Const FirstClassStamp As Double = 0.42
Const FirstClassOn9x12 As Double = 0.83
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.7
Const RetRcpt3800 As Double = 2.2
Const NonMachinableSurcharge As Double = 0.2
Else
Const FirstClassStamp As Double = 0.44
Const FirstClassOn9x12 As Double = 0.88
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.8
Const RetRcpt3800 As Double = 2.3
Const NonMachinableSurcharge As Double = 0.2
End If




Reply With Quote
  #4  
Old   
lyle fairfield
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 12:17 PM



On Apr 12, 10:19*am, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote:

Quote:
OTOH, Lyle has been known to come up with innovative ways of
doing things that many folks think is impossible.
Why not just change the value of the constants?

Declare the constants as strings (VBA won't care) and change them in
code a needed:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Const FirstClassStamp$ = "0.42"
Const NewFirstClassStamp$ = "0.44"

Public Sub UpdateConstants()
CopyMemory ByVal StrPtr(FirstClassStamp), _
ByVal StrPtr(NewFirstClassStamp), _
LenB(NewFirstClassStamp)
End Sub

Public Sub DidItChange()
Debug.Print FirstClassStamp '0.42
If VBA.Date() < DateSerial(2009, 5, 11) Then UpdateConstants
Debug.Print FirstClassStamp '0.44
End Sub

I doubt if there's anyone in the world venturesome enough to actually
USE this trick, not me for sure.





Reply With Quote
  #5  
Old   
Marshall Barton
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 01:46 PM



lyle fairfield wrote:

Quote:
On Apr 12, 10:19*am, Marshall Barton wrote:

OTOH, Lyle has been known to come up with innovative ways of
doing things that many folks think is impossible.

Why not just change the value of the constants?

Declare the constants as strings (VBA won't care) and change them in
code a needed:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Const FirstClassStamp$ = "0.42"
Const NewFirstClassStamp$ = "0.44"

Public Sub UpdateConstants()
CopyMemory ByVal StrPtr(FirstClassStamp), _
ByVal StrPtr(NewFirstClassStamp), _
LenB(NewFirstClassStamp)
End Sub

Public Sub DidItChange()
Debug.Print FirstClassStamp '0.42
If VBA.Date() < DateSerial(2009, 5, 11) Then UpdateConstants
Debug.Print FirstClassStamp '0.44
End Sub

I doubt if there's anyone in the world venturesome enough to actually
USE this trick, not me for sure.


LOL, but definitely innovative.

I haven't had such a good laugh since you converted time to
twips.
--
Marsh


Reply With Quote
  #6  
Old   
Tony Toews [MVP]
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 04:02 PM



"tina" <nospam (AT) address (DOT) com> wrote:

Quote:
if those values will change as of x date, they can change again farther in
the future.
I'm with Tina on this topic. Although if your list of constants will stay constant
then there's no reason why all the values couldn't reside in one record with a data.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/


Reply With Quote
  #7  
Old   
Tony Toews [MVP]
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 04:06 PM



lyle fairfield <lyle.fairfield (AT) gmail (DOT) com> wrote:

Quote:
On Apr 12, 10:19*am, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote:

OTOH, Lyle has been known to come up with innovative ways of
doing things that many folks think is impossible.

Why not just change the value of the constants?
<innovative code snipped>

Holy cr*p. Now that's something I would never have thought of.

Quote:
I doubt if there's anyone in the world venturesome enough to actually
USE this trick, not me for sure.
You got that right.

I recall hearing of a similar trick in Cobol where you could change the value of the
Goto Label in your code. <shudder> Ah, here we go.
http://home.swbell.net/mck9/cobol/style/alter.html

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/


Reply With Quote
  #8  
Old   
Terry Kreft
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 04:11 PM



This is a dreadful idea, see tina's response for a much better
implementation of what you are doing here.

--
Terry Kreft


"MLH" <CRCI (AT) NorthState (DOT) net> wrote

Quote:
Am trying to use the following in declarations section of form module.
Of course, it won't compile. What's the best way to set my constants
inside the If-Then. I need a work-a-round. Can I set Form module
constants outside the form module's uppermos declarations section?

Option Compare Database
Option Explicit

Const SmallEnvWt As Double = 0.2059
Const LargEnvWt As Double = 0.625
Const PaperWeight As Double = 0.1666667
If Now < #5/11/2009# Then
Const FirstClassStamp As Double = 0.42
Const FirstClassOn9x12 As Double = 0.83
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.7
Const RetRcpt3800 As Double = 2.2
Const NonMachinableSurcharge As Double = 0.2
Else
Const FirstClassStamp As Double = 0.44
Const FirstClassOn9x12 As Double = 0.88
Const OneOunceJumpRate As Double = 0.17
Const Certified3811 As Double = 2.8
Const RetRcpt3800 As Double = 2.3
Const NonMachinableSurcharge As Double = 0.2
End If




Reply With Quote
  #9  
Old   
Larry Linson
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 04:57 PM



"Tony Toews [MVP]" <ttoews (AT) telusplanet (DOT) net> wrote

Quote:
I recall hearing of a similar trick in Cobol where
you could change the value of the Goto Label in
your code. <shudder> Ah, here we go.
http://home.swbell.net/mck9/cobol/style/alter.html
In COBOL's heyday (I, at least, am under the impression that it is over,
now, though there's still some use of COBOL for new development, and even an
object-oriented COBOL, I hear), you would be astonished at how many coders
actually used ALTER, and how frequently they did.

It made analyzing and documenting their previously-undocumented programs
"interesting" (in the sense of the Chinese curse about your children living
in interesting times).

Larry



__________ Information from ESET Smart Security, version of virus signature database 4002 (20090411) __________

The message was checked by ESET Smart Security.

http://www.eset.com






Reply With Quote
  #10  
Old   
tina
 
Posts: n/a

Default Re: Prob with code in declarations section of form module ... - 04-12-2009 , 10:08 PM



i thought of that, Tony, and almost set it up that way. in some ways it
might be easier to be able to expand the "constant" list without changing
table structure, but OTOH probably a programmer is going to have to write
code to utilize any newly defined constants anyway - hmm, 6 of one... <g and
shrug>


"Tony Toews [MVP]" <ttoews (AT) telusplanet (DOT) net> wrote

Quote:
"tina" <nospam (AT) address (DOT) com> wrote:

if those values will change as of x date, they can change again farther
in
the future.

I'm with Tina on this topic. Although if your list of constants will stay
constant
then there's no reason why all the values couldn't reside in one record
with a data.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.