Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Passing Variables between Subs
Warren Lovegrove 
     
4 years ago
I'm having an issue getting the variable "ReviewType" from CPIReviewBtn across to CodeSubstitutions.  The CodeSubstitutions sub isn't recognising that ReviewType = "CPI", and hence isn't running the rest of the code.  

Private Sub CPIReviewBtn_Click()
    
    Dim db As Database
    Dim rs As Recordset
    Dim ReviewType As String, CPIDatePriorPrevious As String, CPIDatePriorCurrent As String, CPIRateApplicable As String
    ReviewType = "CPI"
    CodeSubstitutions (ReviewType)
.........

Private Sub CodeSubstitutions(ReviewType As String)

    Dim Description As String, Body As String, Cover As String, S As String, C As Currency

'Create Deed Document
    Select Case ReviewType
    Case "CPI"
        Description = DLookup("Subject", "DocumentTemplateT", "DocumentTemplateID=" & 6)
        Body = DLookup("Body", "DocumentTemplateT", "DocumentTemplateID=" & 6)
    Case "Market"
        Description = DLookup("Subject", "DocumentTemplateT", "DocumentTemplateID=" & 5)
        Body = DLookup("Body", "DocumentTemplateT", "DocumentTemplateID=" & 5)
    End Select
...........
Kevin Robertson  @Reply  
          
4 years ago
You need to declare your variable outside of all the sub routines. See Scope
Kevin Yip  @Reply  
     
4 years ago
Hi Warren, a change as shown below should fix this:

     Private Sub CodeSubstitutions(ByVal ReviewType As String)

Kevin Robertson is right that this has to do with scope.  Since you declare ReviewType as a local variable inside the Click event, once you call CodeSubstitutions(ReviewType), it takes you *outside* of the click event, so ReviewType loses its value.  The solution is to pass the *value* (not the variable) of ReviewType using ByVal.

By default, a sub passes its argument as a reference to the variable: Private Sub MySub(a As Variant) is the same as Private Sub MySub(ByRef a As Variant).  So you often need to override it by using ByVal.
Kevin Robertson  @Reply  
          
4 years ago
Warren Lovegrove OP  @Reply  
     
4 years ago
Thanks for this, solved my problem.
I had tried using Byval, but was getting same issue.  However, after watching the Scope tutorial, realised that the systems clears its memory variables after a compile error.

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

Next Unseen

 
New Feature: Comment Live View
 
 

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

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

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/22/2026 9:20:40 PM. PLT: 1s