Free Lessons
Fast Tips
Topic Index
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
Home > Forums > Developers
Back to Access Developer Forum    Comments List
Public Const Upload Images   Link   Email  
Sandra Truax         
16 months ago
I have been trying for days to get this to work, and I have no idea if it can even be done or not, so I am giving up and having to ask.

I can use the following code in my globalmod, and if I reference cm1 anywhere, it will give me 2023.  But I CANNOT figure out how to make the Const calculate the current year for me so that I don't have to update it each year.  I have included codes I have tried that don't work.

Works: Public Const CM1 As String = 2023

Don't work
Public Const CM1 As String = Year(date)
Public Const CM1 As String = Format(date,"yyyy")
Public Const CM1 As String = format(now(), "yyyy")
Public Const CM1 As String = "" & Format(Date, "yyyy") & ""

Can someone help with this?
Sandra Truax         
16 months ago
What I am trying to accomplish is not having to duplicate code like the following in multiple places on multiple forms.

"D:\J\" & Format(Now(), "yyyy") & "\" & Format(Now(), "mm") & "\Access\Notes"
Kevin Robertson            
16 months ago
You can't set a Constant equal to a function. It has to be a literal value.
I would set up a Public Variable instead.
Sandra Truax         
16 months ago
Thanks Kevin. Explains why I couldn't get it to work. I tried Variables also, with no luck, but will try again now that I know that is what I need to use.
Garry Smith       
16 months ago
I was using global variables and tempvars in my applications. After reading Mike Wolfe's page on a Global form, I tried testing this out to replace my globules vars and tempvars. It has been working out great for me. While coding, I leave the form visible for inspecting the entries and can change them if needed and continue on with testing.
The page to the article is It is worth the read.
Sandra Truax         
16 months ago
Thank you Garry Smith! I tried this in a couple of my forms, and so far I'm loving it.
Garry Smith       
16 months ago
Sandra, I created a function to easily set the values on Unbound textboxes that I have on the form for temporary values.
setG("txtTransIDOne", Me.MyCompnent.Value). I have eliminated creating any TempVars or Global values since I started using this model. While developing I leave the form visible behind other forms so I can always see the values I write to the forms and grab them when needed. It works great for debugging.
Sandra Truax         
16 months ago
Thanks Garry. I think I'm going to have to sleep on this one. I'm having trouble following what you are talking about but I have bookmarked the link on and will have to play with the code to see if I can figure it all out.  Again, thank you for sharing.
Garry Smith       
16 months ago
Sandra, just add the following code to the same module as the G() function and try it.
' Procedure : setG
' Comment   : Extended use of Mike Wolfe's G(CtlName As String) As Variant function from
' Author    : Garry Smith 1/17/2022
' Purpose   : Sets the value of a control on the Global form.  Useful assigning values to
'           :  an Bound/Unbound textbox on the form Global for the current session
'           : The function G(CtlName As String) can be used to retreive the value in the text box
' Notes     - Attempts to open Form if function fails.
'           - Requires a form named Global with HasModule set to Yes.
'           : Example setG("txtTransIDOne", 123) 'sets the txtTransIDOne with the value of 123 for later use
'           : If desired just add this function to the module as the G(CtlName As String)
Function setG(CtlNameToSet As String, ValueToAssign As Variant) As Variant
    On Error GoTo Err_G
    Forms!Global(CtlNameToSet) = ValueToAssign
    setG = True
    Exit Function
    Select Case Err.Number
    Case 2450 'Can't find the form 'Global'
        Dim TriedAlready As Boolean
        If TriedAlready Then
            'Replace with your own error logger:
            'LogErr Err, Errors, "Module1", "G", , CtlName & ": Force Global Form open failed"
            DoCmd.OpenForm "Global", acNormal, , , , acHidden
            TriedAlready = True
        End If
    Case Else
        'Replace with your own error logger:
        'LogErr Err, Errors, "Module1", "G", , CtlName
        setG = False
    End Select
    Resume Exit_G
End Function
Sandra Truax         
16 months ago
Thank you Garry!  I appreciate your help. I'd never figured this out by myself.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.


The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.

Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
My Account
My Courses
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

Copyright 2024 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 7/15/2024 4:27:41 AM. PLT: 1s