Problem with GetCityStateZip

No one else has reported any problems with GetCityStateZip, so I guess it must somehow be me. Whenever I run the code below, I get “Bronx, NY 10471” for the City and “US” for the country, but State and Zip are blank. Suggestions?

WindowGlobal wvCityStateZip, wvCity, wvState, wvZip, wvCountry
wvCityStateZip = "Bronx, NY 10471 US"
GetCityStateZip wvCityStateZip, wvCity, wvState, wvZip, wvCountry
message wvCityStateZip
message wvCity
message wvState
message wvZip
message wvCountry

Looking at the code for that statement, it is incorrect. I think it should be:

local CITYSTATEZIP,xCity,xState,xZip,xCountry,temp
CITYSTATEZIP = parameter(1)
xCity="" xState="" xZip="" xCountry=""

if striptonum(CITYSTATEZIP)<>""
   if length(striptonum(CITYSTATEZIP))=5
      xZip=striptonum(CITYSTATEZIP)
      xCity=city(xZip)
      xState=state(xZip)
   else
      xCity=upperword(CITYSTATEZIP[1,","][1,"-A-z"])
      xState=upper(CITYSTATEZIP[",",-1]["A-z",-1][1," "][1,"-A-z"])
      xZip=upper(CITYSTATEZIP["- ",-1][2,-1])
  if length(xZip["A-Z","-A-Z"])>0
     temp=strip(after(CITYSTATEZIP,","))
     xState=upper(firstword(temp))
     xZip=upper(after(temp," "))
     xCountry="CANADA"
  endif
   endif
else
   xCountry=upper(CITYSTATEZIP["- ",-1]["A-z",-1])
   xCity=CITYSTATEZIP[1,"- "][1,"-!-z"]
   xState=""
   xZip=""
endif

setparameter 2,xCity
setparameter 3,xState
setparameter 4,xZip
setparameter 5,xCountry

I have not tested this for Canada. There may be better ways to do this. It does not work for Zip+4. I could probably fix that, but it is late.

This actually was reported a couple of months ago.

Bruce, I don’t understand your proposed fix. It seems to me that if the line ends with US, U.S., USA, U.S.A., United States, or any other variation that means US, that should be removed and the previous part of the line processed as a normal US address. As far as I can see your proposed fix would work if only a 5 digit zip code was supplied, but otherwise would fail for US addresses.

This works for US address if a Zip or Zip+4 code is provided, just replacing the beginning of the procedure:

if striptonum(CITYSTATEZIP)<>""
if length(striptonum(CITYSTATEZIP))=5 or length(striptonum(CITYSTATEZIP))=9
  
  if length(striptonum(CITYSTATEZIP))=9
      xZip=striptonum(CITYSTATEZIP)[1,5]+"-"+striptonum(CITYSTATEZIP)[6,9]
  else
      xZip=striptonum(CITYSTATEZIP)
  endif
  xCity=city(striptonum(CITYSTATEZIP)[1,5])
  xState=state(striptonum(CITYSTATEZIP)[1,5])
  xCountry=upper(CITYSTATEZIP["- ",-1]["A-z",-1])

Everything else is the original code.

The reason the original did not work is that city( and state( arguments were the entire last line, not the Zip code.

Ok, I believe this issue is now fixed. Here is my fix, inserted at the top of the code right after the local variables are set up.

CITYSTATEZIP = onewhitespace(CITYSTATEZIP)
looparray "US,U.S.,U S,USA,U.S.A.,U S A,United States,United States of America,Canada",",",countryName
    if CITYSTATEZIP endswith countryName
        CITYSTATEZIP = strip(trim(CITYSTATEZIP,length(countryName)))
    endif
endloop

This fix works by explicitly checking for the United States or Canada. If either of these countries are found, they are removed, leaving the zip code at the end of the line. Then it continues with the original code which works correctly.

I edited my reply as you were writing. The city( and state( functions were a problem.

By the way, this will only be as accurate as the city( function. This is an issue for me, because my Zip code is used across two cities, and the city( function only returns one. My street spans both cities, so I live in one city, and my neighbors up the hill live in another. Using the city( function makes them the same. It returns my city, but not his. This was a issue for someone else who lived in the same Zip code, but in the other city which is perceived as tonier.

There is no siimple answer to this. As you noted, there are some tonier parts of town that have proclaimed their own name while retaining the original zip code. Zip codes are not designed around city limits and thus, there are many reasons why a particular zip code may be used by multiple named ‘cities’ or areas. But in our db world, when we need a lookup, most often we want an answer. You are wanting a single answer but the USPS has decided the answer you are going to get. Panorama can not easily determine anything more accurate than the primary designator for a single zip code.
In my code, I do the lookup, and then allow the user to ‘correct’ it as they please.

The best way to work around this is to run the complete address through ZipInfoPlus, which will give you the USPS corrected address. This is not completely correct, as is the case with my street, but at least it will assure that whatever you send will get there. The only problem is that ZipInfoPlus, as currently shipped, does not work. You can search around here for my version which does work.

Great! So what is the complete corrected code for GetCityStateZip, and how do I put that new code into Panorama itself?

I live in a city that shares a zip code with another city in another county and it has wreaked havoc with our insurance rates. Lately, I’ve noticed that when I type in my zip code on sites that request an address, the zip code entry prompts a choice for the two cities. You could probably implement code that, if zip code =“X”, prompt for a city choice. The problem would be finding all the cities that share a zip code.