Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > VBA With < Combo Box Shows ID | Remove Time >
VBA With
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   12 months ago

Optimize Your Access VBA Code Using the With Keyword


 S  M  L  XL  FS  |  Slo  Reg  Fast  2x  |  Bookmark Join Now

In this Microsoft Access tutorial, I will show you how to streamline your VBA code using the With keyword. You'll learn how to simplify your code by reducing redundancy, enhance readability, and potentially improve performance in loops. This technique can significantly ease maintenance and debugging while minimizing typing effort.

Recommended Courses

Learn More

FREE Access Beginner Level 1
FREE Access Quick Start in 30 Minutes
Access Level 2 for just $1

Free Templates

TechHelp Free Templates
Blank Template
Contact Management
Order Entry & Invoicing
More Access Templates

Resources

Diamond Sponsors - Information on our Sponsors
Mailing List - Get emails when new videos released
Consulting - Need help with your database
Tip Jar - Your tips are graciously accepted
Merch Store - Get your swag here!

Questions?

Please feel free to post your questions or comments below or post them in the Forums.

KeywordsVBA With in Microsoft Access

TechHelp Access, With keyword VBA, streamline VBA code, reduce object reference, improve code readability, VBA maintainability, control properties reference, set control value, record sets VBA, loop optimization VBA, enhance code debugging, avoid nested With, simplify VBA syntax, save typing effort, Microsoft Access VBA tutorial

 

 

 

Comments for VBA With
 
Age Subject From
12 monthsA day or two late butJeffrey Kraft
12 monthsMost ExcellentSandra Truax

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to VBA With
Get notifications when this page is updated
 
Transcript Welcome to another TechHelp fast tips video brought to you by accesslearningzone.com. I'm your instructor Richard Rost. Today we're gonna see how to streamline your Microsoft Access VBA code with the With keyword.

What is With? Well, With is a VBA keyword to simplify and streamline your code by reducing the need to repeatedly reference the same object. It improves your code readability and maintainability, and it makes it a whole lot easier to type.

For example, consider this little block of code here. If we're building a string called s and we want to add to it the first name, last name, phone number, and so on, fields from the customer f form, we have to say s equals s and Forms!CustomerF!FirstName, Forms!CustomerF!LastName. You get it, right? You have to repeat that every time. Now, you can copy and paste it, but if you have to make a change later on, that's a lot of work.

So that's where With comes in. With With, you just say With and then the object name right here. So it's Forms!CustomerF. And then after that, inside this With block, all you have to do is say !FirstName, !LastName, !PhoneNumber, and so on instead of having to copy Forms!CustomerF for every one of those lines.

It's also handy if you want to reference control properties. Let's say the first name, all right? You want to set its value equal to Joe, and then you want to change a bunch of property values like Visible is True, Enable is True, Locked is False, whatever. With the With keyword, now you can say .Value because Value is a property, right? .Value equals Joe instead of saying FirstName equals Joe, and then .Visible equals True, .Enabled equals True, .Whatever Whatever Whatever. And then End With, and all of that happens to the first name.

I use With a lot with record sets. Now, this is a simple record set, and to be honest, if I just have an rs, that's easy to type: rs!FirstName, rs!LastName. But if I have multiple record sets going on and they have long names, usually at least one of them I'll put inside of a With block. So then it looks like this: With rs, right? While Not .EOF. So that's rs.EOF, right? And then status!FirstName = rs!FirstName, rs!LastName, rs!Phone. .MoveNext is part of rs, right? And this is great, especially if you have longer record set names. Okay, and then when you're all done End With, and everything goes back to normal after that.

So why use With? It's less typing. You don't have to copy and paste a bunch of stuff. It's easier to read and debug afterwards, I think. It's easier to maintain if you have to make changes, and it's potentially faster in loops. I'm going to say maybe on that because I haven't tested it personally, and I don't like telling you guys something's true if I haven't 100% verified it myself. But I have read in many different places, different books and articles say that it can be faster in your loops. I'm not sure. If any of you want to test it out, let me know, please do. It makes sense to me that it would be faster because, again, it only has to reference that one object one time instead of a bunch of times every single time.

When not to use With? Well, don't bother with With if the object is only used once. You wouldn't say With Forms!CustomerF if then you're only using FirstName or even just FirstName and LastName. That's up to you. I personally don't like nesting With blocks. You can. You can nest Withs inside of each other, but I never do that because then it gets really confusing because then the most recent With is the one that takes over. Just don't do it. Use it for one thing at a time. Trust me.

So that's it. That's the With keyword in VBA. It's another tool for your box, another Lego for your place. But that's gonna be your TechHelp fast tip for today. Hope you learn something. Live long and prosper, my friends. I'll see you next time.

TOPICS:
Introduction to the With keyword in VBA
Simplifying code with the With keyword
Improving code readability
Streamlining object references
Setting properties with the With keyword
Using With for form controls
Using With with record sets
Benefits of using the With keyword
When to use the With keyword
When not to use the With keyword
Avoiding nested With blocks

COMMERCIAL:
In today's video, we're going to learn how to streamline your Microsoft Access VBA code using the With keyword. This handy tool allows you to simplify and enhance readability by reducing the repetitive referencing of objects. We'll show you how to use With to improve your code when dealing with form elements and record sets, making your code easier to maintain, debug, and potentially faster in loops. However, we'll also discuss when not to use With to keep things simple and clear. Join us to add this powerful tool to your VBA toolbox. You'll find the complete video on my YouTube channel and on my website at the link shown. Live long and prosper my friends.
Quiz Q1. What is the primary purpose of using the With keyword in VBA?
A. To slow down the execution of code
B. To simplify and streamline code by reducing repeated references to the same object
C. To enhance error handling
D. To replace the need for loops

Q2. In the example provided, how does the With keyword benefit the code when building a string with multiple fields?
A. It allows automatic data validation of the fields
B. It creates a backup of the data before modifying it
C. It reduces the need to repeatedly type the object reference (e.g., Forms!CustomerF)
D. It generates a visual representation of the string in Access

Q3. How can the With keyword be used with control properties in Access VBA?
A. By exporting control properties to a CSV file
B. By changing the database schema
C. By allowing the user to type properties as methods
D. By directly modifying properties like .Value, .Visible, .Enabled within the With block

Q4. What is one potential advantage of using the With keyword in loops, as mentioned in the video?
A. It guarantees the code will compile without errors
B. It is universally loved and preferred by all programmers without exception
C. It definitely improves the code execution speed
D. It can potentially be faster since the object is referenced only once per block

Q5. Under what condition does the video recommend not using the With keyword?
A. When working with data types other than strings
B. When the object is referenced only once in the code
C. When structuring SQL queries within VBA
D. When debugging complex database connections

Q6. What does the instructor recommend regarding nesting With blocks?
A. Always use nested With blocks for clarity
B. Avoid nesting With blocks to prevent confusion
C. Nest With blocks only if using more than three properties
D. Use nested With blocks only when using inline functions

Answers: 1-B; 2-C; 3-D; 4-D; 5-B; 6-B

DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.
Summary Today's TechHelp tutorial from Access Learning Zone focuses on enhancing your Microsoft Access VBA code using the With keyword. As your instructor, I'm here to help you understand how With can simplify and streamline your coding process. The goal is to make your code more readable, easier to manage, and less tedious to type.

In VBA, With is a powerful keyword that allows you to avoid repeating references to the same object. Let's consider an example. Suppose you're creating a string named 's,' and you need to append the first name, last name, phone number, and other fields from a form named CustomerF. Normally, you would repeat the form reference like this: s = s & Forms!CustomerF!FirstName & Forms!CustomerF!LastName, and so on. This repetition can be cumbersome, especially if modifications are needed.

With allows you to address this issue effectively. You can initiate a With block by specifying the object, such as Forms!CustomerF. Within this block, you can simply reference the fields with !FirstName, !LastName, !PhoneNumber, etc., eliminating the need to repeat Forms!CustomerF each time.

This approach is also beneficial when dealing with control properties. For example, to set the first name's value to "Joe" and change various property settings like Visible, Enabled, and Locked, you can use the With keyword. By doing this, you can write .Value = Joe, .Visible = True, .Enabled = True, and so on within the With block, applying these changes to the first name control without repeating its name.

I often use With in conjunction with record sets. If I'm working with a simple record set named 'rs,' typing rs!FirstName or rs!LastName isn't too burdensome. However, if dealing with multiple record sets with lengthy names, placing at least one of them inside a With block is advantageous. For example, you could write: With rs, followed by While Not .EOF, and other references like .MoveNext, to simplify code when handling long record set names.

So, why use With? It reduces the number of repetitive lines you need to write, improves readability and debugging, simplifies maintenance, and might even speed up loops. While I haven't personally tested the performance benefits, literature suggests it could be faster due to fewer repeated object references. If you're curious, feel free to test it and share your findings.

There are situations where With might not be necessary. If an object is referenced only once, using With might not offer significant benefits. Additionally, I'm not a fan of nesting With blocks as it can complicate the code structure. It's better to use With for one object at a time to keep things simple.

In conclusion, the With keyword is a valuable tool in your VBA toolkit. It's another building block for efficient coding in Microsoft Access. For a more detailed walkthrough, including step-by-step instructions on everything discussed here, visit my website using the link below.

Live long and prosper, my friends.
Topic List Introduction to the With keyword in VBA
Simplifying code with the With keyword
Improving code readability
Streamlining object references
Setting properties with the With keyword
Using With for form controls
Using With with record sets
Benefits of using the With keyword
When to use the With keyword
When not to use the With keyword
Avoiding nested With blocks
 
 
 

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 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 12/14/2025 2:41:44 AM. PLT: 1s
Keywords: TechHelp Access, With keyword VBA, streamline VBA code, reduce object reference, improve code readability, VBA maintainability, control properties reference, set control value, record sets VBA, loop optimization VBA, enhance code debugging, avoid nested W  PermaLink  VBA With in Microsoft Access