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 Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Auto Populate Address
Amir Ouranus 
      
4 months ago
Hello!
I have an Address Sub-Form in my employee form that keeps track of the all of the employees addresses. The first record in the Address Sub-Form has a combo box that the user chooses the type of address; Work, Home, Main Office, ect.

Most employees work in the main office so I would like to write a VBA code that if the Main Office is selected, it will auto populate the rest of the address automatically. This is what I have so far but I'm definitely missing something. Any help will be greatly appreciated.

Private Sub Form_AfterUpdate()

    If AddressType = "Main Office" Then
        Address1 = "6478 Prentiss Dr."
        Address2 = "Suite 900"
        City = "Irvine"
        State = "CA"
        ZipCode = "90200"
        ZipCode 4 = "1234"
    End If

End Sub
Alex Lewis  @Reply  
       
4 months ago
I'll start off by saying that if you have spaces in your field names at all, you have to put square brackets around them in your VBA code. For example, [ZipCode 4] = "1234."

What does your form look like?
Amir Ouranus OP  @Reply  
      
4 months ago
I don't have spaces. The field Name for ZipCode 4 is actually ZipCode+4.
But it seems I have to put brackets because it takes out the + sign every time.
Alex Lewis  @Reply  
       
4 months ago
You mentioned the AddressType is a combo box on your form. Try moving your code to the After Update event for your combo box.
Amir Ouranus OP  @Reply  
      
4 months ago
No, that didn't work.
Raymond Spornhauer  @Reply  
          
4 months ago
If your database is setup correctly, AddressType should not be a string... it should be a number.

The RowSource for the combo box should have an ID field, then the description as you described as a second column in that field.

If this is correct, you need to find the ID for the Main Office then change your code to this:

If AddressType = # Then
        Address1 = "6478 Prentiss Dr."
        Address2 = "Suite 900"
        City = "Irvine"
        State = "CA"
        ZipCode = "90200"
        ZipCode 4 = "1234"
    End If

-Raymond
Amir Ouranus OP  @Reply  
      
4 months ago
YES!!!!
Thank you Raymond!!!
Amir Ouranus OP  @Reply  
      
4 months ago
Ok, quick question; looking around the Internet some people had the code like this:

If AddressType = "Agency Core Office" Then
        Me.Address1 = "6478 Prentiss Dr."
        Me.Address2 = "Suite 900"

Ignoring the first line, we now know that is incorrect; would this method work as well?
If yes; is it better?
What advantages or disadvantages does it have?
Alex Lewis  @Reply  
       
4 months ago
It works, but you don’t need the “Me.” keyword in front of the address field. Me is automatically assumed when writing field names in VBA.

Bang! vs. Dot: https://599cd.com/blog/display-article.asp?ID=1561
Raymond Spornhauer  @Reply  
          
4 months ago
It depends on how the database and combo box are set up.

A good database should have a table for the combo box options.  Using your example, your table could look like this:

AddressTypeID    AddressType
1                       Main Office
2                       Work
3                       Home

In your combo box... you set the RecordSource to "SELECT AddressTypeID, AddressType FROM YOURTABLE"

The combo box properties will have a "Bound Column" which is the AddressTypeID, (which is a number).

=================================================================
You could have a Table that just has the AddressType (which is a string)

Then your combo RecordSource  would be "SELECT AddressType FROM YOURTABLE"

Using this method, the original code you posted would have worked, but is not he correct way to set this up your database.


-Raymond

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access 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/20/2026 5:41:28 AM. PLT: 1s