MS Access Forum / Queries / February 2008
Address List
|
|
Thread rating:  |
FJ Questioner - 22 Feb 2008 22:36 GMT My mailing list often has two last names for a given address. Each is a separate row in my database. I want to be able to make an address label that says "The Smith & Jones Residence". How do I write a query that will pick up both last names for a given address (the address is a single field).
Thanks,
FJquestioner
John Spencer - 23 Feb 2008 00:26 GMT If it is No more than 2 names you could use a Totals query.
SELECT IIF(Min(LastName)= Max(LastName) , Min(LastName) ,Max(LastName) & " and " & Min(LastName)) as OneName , Address FROM SomeTable GROUP BY Address
Otherwise, you could take a look at Duane Hookom's concatenate function (google search) and modify it to return the needed string.
'==================================================== John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County '====================================================
> My mailing list often has two last names for a given address. Each is a > separate row in my database. I want to be able to make an address label that [quoted text clipped - 4 lines] > > FJquestioner FJ Questioner - 25 Feb 2008 15:36 GMT Thanks but I'm not sure I follow.
ID Last Name St# Street 1 Smith 11 Apple St. 2 Jone 11 Apple St.
Ultimately I want to be able to produce an address label like this:
The Smith & Jones Residence 11 Apple St.
As you suggested I went to Duane Hookom's concatenate function and I tried the following:
LastNames: Concatenate("SELECT [LastName] FROM [List] WHERE [Address] =" & [Address])
However, I've no idea if this will work because I keep getting error message "Undefined function"Concatenate" in expression"
Am I way off track?
Thanks again.
> If it is No more than 2 names you could use a Totals query. > [quoted text clipped - 23 lines] > > > > FJquestioner John Spencer - 25 Feb 2008 17:24 GMT Did you copy the concatenate function and paste it into a VBA module in your database and save the module with a name other than concatenate?
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> Thanks but I'm not sure I follow. > [quoted text clipped - 51 lines] >> > >> > FJquestioner FJ Questioner - 25 Feb 2008 17:37 GMT No I just wrote it in a regular query (as per Hookum's example). I'm not very familiar with VBA...
> Did you copy the concatenate function and paste it into a VBA module in your > database and save the module with a name other than concatenate? [quoted text clipped - 54 lines] > >> > > >> > FJquestioner John Spencer - 25 Feb 2008 18:00 GMT Well, you need to do so or the concatenate function will not work.
Do you know how to do this or are you going to require step by step directions?
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> No I just wrote it in a regular query (as per Hookum's example). I'm not > very familiar with VBA... > >> Did you copy the concatenate function and paste it into a VBA module in >> your >> database and save the module with a name other than concatenate? FJ Questioner - 25 Feb 2008 18:18 GMT Sorry, I just gave it a whirl but to ne effect. I'm going to need the step by step......
> Well, you need to do so or the concatenate function will not work. > [quoted text clipped - 7 lines] > >> your > >> database and save the module with a name other than concatenate? John Spencer - 25 Feb 2008 20:29 GMT Ok
Open Your database Select Modules Click on NEW Paste in the Concatenate code from the Sample --- Check the code SELECT Debug: Compile from the menu If you get errors then you will need to fix the cause. Most of the time the error will be due to lines wrapping.
For Duane's code, you will probably have to make sure you have the DAO library Menu: Tools: References If you don't have Microsoft DAO x.x Object Library, scroll down the list, find it, and check it. Close the dialog window
Duane's code has two places where you have to comment out a bit of code and comment in a different bit of code. He has comments on where to do that in the code. Comment out requires you to place an apostrophe at the start of the line(s) to be commented out. Comment in - remove the apostrophe.
Select Debug: Compile again and see if the code compiles.
NOW SAVE the module as MOD_Concatenate
Try your query again.
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> Sorry, I just gave it a whirl but to ne effect. I'm going to need the step > by [quoted text clipped - 13 lines] >> >> your >> >> database and save the module with a name other than concatenate? FJ Questioner - 25 Feb 2008 23:05 GMT I copied the following into my new module (although I have no clue what it means). When I ran Compile, I got the error message: "User defined Type not defined" and the "rs As New ADODB.Recordset" in the 2nd line of the commented in section was highlighted (not in yellow but in blue as if I was about to copy that section).
Incidentally I checked the appropriate Microsoft DAO x.x Object Library as you suggested.
Any idea as to what's wrong?
Thanks again.
Function Concatenate(pstrSQL As String, _ Optional pstrDelim As String = ", ") _ As String 'Created by Duane Hookom, 2003 'this code may be included in any application/mdb providing ' this statement is left intact 'example 'tblFamily with FamID as numeric primary key 'tblFamMem with FamID, FirstName, DOB,... 'return a comma separated list of FirstNames 'for a FamID ' John, Mary, Susan 'in a Query 'SELECT FamID, 'Concatenate("SELECT FirstName FROM tblFamMem ' WHERE FamID =" & [FamID]) as FirstNames 'FROM tblFamily '
'======For DAO uncomment next 4 lines======= '====== comment out ADO below ======= 'Dim db As DAO.Database 'Dim rs As DAO.Recordset 'Set db = CurrentDb 'Set rs = db.OpenRecordset(pstrSQL)
'======For ADO uncomment next two lines===== '====== comment out DAO above ====== Dim rs As New ADODB.Recordset rs.Open pstrSQL, CurrentProject.Connection, _ adOpenKeyset, adLockOptimistic Dim strConcat As String 'build return string With rs If Not .EOF Then .MoveFirst Do While Not .EOF strConcat = strConcat & _ .Fields(0) & pstrDelim .MoveNext Loop End If .Close End With Set rs = Nothing '====== uncomment next line for DAO ======== 'Set db = Nothing If Len(strConcat) > 0 Then strConcat = Left(strConcat, _ Len(strConcat) - Len(pstrDelim)) End If Concatenate = strConcat End Function
> Ok > [quoted text clipped - 42 lines] > >> >> your > >> >> database and save the module with a name other than concatenate? John Spencer - 26 Feb 2008 12:28 GMT That was one of the lines that needed to be commented out. You are using DAO, so the lines that Duane has for ADO need to be commented out and the lines for DAO need to be uncommented. Near the top of the code, it should look like:
'======For DAO uncomment next 4 lines======== '====== comment out ADO below ======== Dim db As DAO.Database Dim rs As DAO.Recordset Set db = CurrentDb() Set rs = db.OpenRecordset(pstrSQL)
'======For ADO uncomment next two lines======== '====== comment out DAO above ======== 'Dim rs As New ADODB.Recordset 'rs.Open pstrSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
And near the bottom of the function '====== comment out next line for ADO =========== Set db = Nothing
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
>I copied the following into my new module (although I have no clue what it > means). When I ran Compile, I got the error message: "User defined Type [quoted text clipped - 119 lines] >> >> >> your >> >> >> database and save the module with a name other than concatenate? FJ Questioner - 26 Feb 2008 13:59 GMT OK got it. It seemed to compile ok but when I ran the query I got an error message:
Runtime error 3075. Syntax error (missing operator) in query expression.
It then highlighted this Module line in Yellow: Set rs = db.OpenRecordset(pstrSQL)
BTW, the actual query I wrote was as follows: LastNames: Concatenate("SELECT [LastName] FROM [List] WHERE [X] =" & [X])
List=table with names and address X=a column within the same query in which I concatenated the street name and number into a single string.
I'm zeroing in on it !
Thanks once again.
FJ
> That was one of the lines that needed to be commented out. You are using > DAO, so the lines that Duane has for ADO need to be commented out and the [quoted text clipped - 141 lines] > >> >> >> your > >> >> >> database and save the module with a name other than concatenate? John Spencer - 26 Feb 2008 16:23 GMT Your query string needs some work.
First: X is a string and as such the query must identify it as a string by using quote marks around X. Second: X is unknown in the table List so you must rebuild X in the where clause.
I assumed that you concatenated in a space between Street Name and Street Number and I assumed that you built X in the same order and manner.
Something like the following is what I would expect to see.
LastNames: Concatenate("SELECT [LastName] FROM [List] WHERE [Street name ] & "" "" & [Street Number] =""" & [X] & """")
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> OK got it. It seemed to compile ok but when I ran the query I got an > error [quoted text clipped - 173 lines] >> >> >> >> your >> >> >> >> database and save the module with a name other than concatenate? FJ Questioner - 26 Feb 2008 20:01 GMT Sorry John, Me again!
Here's the query I wrote.
LastNames: Concatenate("SELECT [LastName] FROM [List] WHERE [Unit No]&""&[Street Number ] &"" & [Street Name] =""" & [X] & """")
Here's the error message I get.
"Too few Parameters. Expected 1."
Not sure if this is of interest but here's the address concatenation column.
X: ([Unit No] & "" & [Street Number] & "" & [Street Name])
I must be getting close ??
Thanks. FJ
> Your query string needs some work. > [quoted text clipped - 188 lines] > >> >> >> >> your > >> >> >> >> database and save the module with a name other than concatenate? John Spencer - 27 Feb 2008 12:19 GMT Ok, there are a couple possibilities. One - is the field LastName or is it Last Space Name. Or one of the other field names is misspelled. Second - X is not being recognized as the concatenation and you will have to build that into the query string.
Since you are separating the fields with a zero-length string, you should be able to use Concatenate("SELECT [LastName] FROM [List] WHERE [Unit No] & [Street Number ] & [Street Name] ='" & [Unit No] & [Street Number] & [Street Name] & "'")
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> Sorry John, Me again! > [quoted text clipped - 226 lines] >> >> >> >> >> database and save the module with a name other than >> >> >> >> >> concatenate? FJ Questioner - 27 Feb 2008 17:20 GMT I think this is going to work because the error message I'm getting only tells me I have invalid sytax and points to the phrase below. I suspect it has to do with the sequence of single and double quotaion marks. Do I have these right? (I left spaces between them in this example to show you what I did:
=" ' & [Unit No] & [Street Number] & [Street Name] & "" '
Thanks. Hopefully this'll be the last iteration!
> Ok, there are a couple possibilities. > One - is the field LastName or is it Last Space Name. Or one of the other [quoted text clipped - 228 lines] > >> >> >> >> > No I just wrote it in a regular query (as per Hookum's > >> >> >> >> > example). John Spencer - 27 Feb 2008 19:53 GMT Try the following (take out the spaces)
= ' "& [Unit No] & [Street Number] & [Street Name] & " ' "
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
>I think this is going to work because the error message I'm getting only > tells me I have invalid sytax and points to the phrase below. I suspect it [quoted text clipped - 260 lines] >> >> >> >> >> > No I just wrote it in a regular query (as per Hookum's >> >> >> >> >> > example). FJ Questioner - 27 Feb 2008 21:23 GMT OK so here's what I've got (spaces are added just for clarity)
LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE [Unit No]&[Street Number ] & [Street Name] ="' & [Unit No] & [Street Number] & [Street Name] & ""')
Now I get the same error message: Runtime error 3061. Too few parameters. Expected 1.
Incidentally, I got the same error mesage even when I tried the above with 2 versions of the street concatenation. One with spaces and one without ie.
X: ([Unit No] & "" & [Street Number] & "" & [Street Name])
and
X: ([Unit No] & [Street Number] & [Street Name])
Thanks again, FJ
> Try the following (take out the spaces) > [quoted text clipped - 240 lines] > >> >> >> >> the line(s) to be commented out. Comment in - remove the > >> >> >> >> apostrophe. John Spencer - 28 Feb 2008 12:28 GMT LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE [Unit No]&[Street Number ] & [Street Name] ="' & [Unit No] & [Street Number] & [Street Name] & ""')
It still appears to me as if you have the apostrophes and quotes switched. Let's try a slight differenct approach. LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE [Unit No]&[Street Number ] & [Street Name] =" & Chr(34) & [Unit No] & [Street Number] & [Street Name] & Chr(34))
If that fails, then obviously I am not giving you the correct advice.
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> OK so here's what I've got (spaces are added just for clarity) > [quoted text clipped - 17 lines] > Thanks again, > FJ FJ Questioner - 28 Feb 2008 15:35 GMT Hi John,
I checked Duane's formula again and the following simpler version seems to be working for the latter half of the formula (ie after the= sign):
LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE[Unit No]&[Street Number ]&[Street Name] =" & [Unit No] & [Street Number] & [Street Name])
However, when I run this I'm now getting the message:
Syntax error (Missing Operator) in query expression '[Unit No]&[Street Number]&[Street Name]= 123 Winterbourne Dr.'
And when I click Debug it takes me to the same old line: Set rs = db.OpenRecordset(pstrSQL)
So given that its properly picking up the address info after the = sign I presume the problem is with the first half of the formula. I tried puttiing the [Unit No]&[Street Number]&[Street Name] in single quotes, double quotes and () but to no avail. I also tried it with additional "&" signs at the beginning and end of the phrase but it makes no difference. When I look at Duane's formula it seems to be constructed the same as mine is.
Have you any idea what this elusive "Missing Operator" might be?
Sorry for dragging this out but it seems we're so darned close!
Thanks,
FJ
> LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE [Unit > No]&[Street Number ] & [Street Name] ="' & [Unit No] & [Street Number] & [quoted text clipped - 28 lines] > > Thanks again, > > FJ John Spencer - 28 Feb 2008 16:42 GMT NO, the problem is that 123 WinterBourne Dr should be surrounded with Quote marks to tell the query that you are looking at a string. Without surrounding quotes the query thinks you want to do something with the number 123 and two fields named Winterbourne and Dr.
Did you try the suggestion below?
LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE[Unit No]&[Street Number ]&[Street Name] =" & Chr(34) & [Unit No] & [Street Number] & [Street Name] & Chr(34))
I'm not sure how you ended up with spaces in 123 Winterbourne Dr since there is nothing in your posted SQL string that would add those in.
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> Hi John, > [quoted text clipped - 68 lines] >> > Thanks again, >> > FJ FJ Questioner - 28 Feb 2008 17:19 GMT EUREKA ! I tried it with the Chr(34) and this time it worked (although I tried that the first time you mentioned it and it didn't).
However, it still needs to be tweaked because in the numerous instances where there are 2 or more people at a residence with the same last name, the query is returning a string that includes one last name for each resident. Here's what the query produces:
Address Last Names
123Winterbourne Dr. Smith, Smith, Smith
I can't produce address labels to "The Smith & Smith & Smith residence". Is there a way I filter it so that I end up only with one of each discrete name at a given address?
Hope this isn't too tricky!
Incidentally, here's the version that I've now got:
LastNames: Concatenate("SELECT [Last Name] FROM [List] WHERE[Unit No]&[Street Number ]&[Street Name] =" & Chr(34) & [Unit No] & [Street Number] & [Street Name] & Chr(34))
Thanks again. FJ
> NO, the problem is that 123 WinterBourne Dr should be surrounded with Quote > marks to tell the query that you are looking at a string. Without [quoted text clipped - 82 lines] > >> > Thanks again, > >> > FJ John Spencer - 28 Feb 2008 19:53 GMT Add DISTINCT to the Select clause.
LastNames: Concatenate("SELECT DISTINCT [Last Name] FROM [List] WHERE[Unit No]&[Street Number ]&[Street Name] =" & Chr(34) & [Unit No] & [Street Number] & [Street Name] & Chr(34))
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> EUREKA ! I tried it with the Chr(34) and this time it worked (although I > tried that the first time you mentioned it and it didn't). [quoted text clipped - 124 lines] >> >> > Thanks again, >> >> > FJ cttechsupport@heritagephc.com - 23 Feb 2008 00:32 GMT Well - very similar actually to the text of your example.....
In your query you can define field (column) names yourself - you're not stuck with what you can drag down from the table. So............... For a combined name field enter in the box where you'd normally drag down a table field.......... (assuming you have two Last Name fields called Lname1 and Lname2)
NameField:[Lname1] & " & " & [Lname2] & "Residence"
That simple !
Good luck
Charlie
On Feb 22, 5:36 pm, FJ Questioner <FJQuestio...@discussions.microsoft.com> wrote:
> My mailing list often has two last names for a given address. Each is a > separate row in my database. I want to be able to make an address label that [quoted text clipped - 4 lines] > > FJquestioner FJ Questioner - 25 Feb 2008 15:38 GMT Thanks but I don't think I explained properly. My database looks like this:
ID Last Name St# Street 1 Smith 11 Apple St. 2 Jone 11 Apple St.
Ultimately I want to be able to produce an address label like this:
The Smith & Jones Residence 11 Apple St.
I tried the following:
LastNames: Concatenate("SELECT [LastName] FROM [List] WHERE [Address] =" & [Address])
However, I've no idea if this will work because I keep getting error message "Undefined function"Concatenate" in expression"
Am I way off track?
Thanks again.
FJ
> Well - very similar actually to the text of your example..... > [quoted text clipped - 23 lines] > > > > FJquestioner
|
|
|