VBA Full Address

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
237
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Is there a vba code to create a full address in one query field?

Now I have fields like (Name, Contact, Address1, Address1, Zip, City, Country)

I would create module to make one field from all these fields without error if one field is empty.

Name
Contact
Address1
Address2
Zip City
Country

Is this possible?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Just as a formula example if Name is in A1

G1 = =SUBSTITUTE(A1&", "&B1&", "&C1&", "&D1&", "&E1&", "&F1,", ,",",")

and drag down
 
Upvote 0
You could do this in a query using concatenation, ie.

NewField: Name & ", " & Contact & ", " & ..........etc
This would show all separated by commas.

BTW: Name is reserved in Access and should not be used as it may cause issues. You would be better off using something like FName or LName or Name1 instead or some other variation.
 
Upvote 0
Solved with module below:

Code:
Function FullAddress(CompanyName, Address1, Address2, ZIP, City, Country) As String

Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim E As String
Dim F As String


A = Trim(CompanyName)
B = nz(Trim(Address1), "")
C = nz(Trim(Address2), "")
D = nz(Trim(ZIP), "")
E = nz(Trim(City), "")
F = nz(Trim(Country), "")


'
FullAddress = IIf(A = "", "", A & Chr(13) & Chr(10)) & IIf(B = "", "", B & Chr(13) & Chr(10)) & IIf(C = "", "", C & Chr(13) & Chr(10)) & IIf(D = "", "", D & " ") & IIf(E = "", "", E & Chr(13) & Chr(10)) & IIf(F = "", "", F)


End Function
 
Upvote 0
Done it again. missed that its in access, oops
 
Upvote 0
Is there a vba code to create a full address in one query field?

Now I have fields like (Name, Contact, Address1, Address1, Zip, City, Country)

I would create module to make one field from all these fields without error if one field is empty.

Name
Contact
Address1
Address2
Zip City
Country

Is this possible?
Don't know if you care, but what you want to do violates normalization principles. Information like this should be 'assembled' by queries/forms/reports and not stored this way. What happens if Mr. Smith moves or gets a new phone number? You now have the same data stored in two places, which is bad enough, but also concatenated. There would be no easy or reliable way to alter part of your concatenated data.
 
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,081
Members
451,738
Latest member
gaseremad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top