I am playing with a new function in an AccessDB app to return Lat/Lon info from Hospital names. The following function provides what I need when I provide a Name & Address. I noticed (unexpected) the function returns a formatted address even if I provide JUST a valid hospital name. I think I can exploit this to backfill address info into my database.
It appears that Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text is "mostly" consistent and easilly parsed to grab Address/City/State/ZIP info using "," as a delimiter. My complication is the rare occasion where a "Floor Number" is included in the formatted address string. My parsing routine fails.
I found this routine (not mine):
Option Explicit
Option Compare Database
'Public Type containing the geocoding of the postal address
Public Type tGeocodeResult
dLatitude As Double
dLongitude As Double
sRetAddress As String
sAccuracy As String
sStatus As String
End Type
'---------------------------------------------------------------------------------------
' Procedure : Geocode with Google Geocoding API v3
' Version : 1.01
' DateTime : 03/03/2011
' Author : Philben
' Purpose : converting addresses into geographic coordinates
' Parameter : No mandatory. string format or NULL
' Reference : http://code.google.com/intl/fr-FR/apis/maps/documentation/geocoding/index.html
' Remark : Query limit of 2,500 geolocation requests per day
' : A good accuracy is different of a good geocoding !!!
' : Minimum delay between two queries : >= 200 ms
'---------------------------------------------------------------------------------------
Public Function Geocode(Optional ByVal vAddress As Variant = Null, _
Optional ByVal vTown As Variant = Null, _
Optional ByVal vPostCode As Variant = Null, _
Optional ByVal vRegion As Variant = Null, _
Optional ByVal sCountry As String = "UNITED STATES+") As tGeocodeResult
On Error GoTo catch
Dim oXmlDoc As Object
Dim sUrl As String, sFormatAddress As String
If Not IsNull(vAddress) Then vAddress = Replace(vAddress, ",", " ")
sFormatAddress = (vAddress + ",") & _
(vTown + ",") & _
(vRegion + ",") & _
(vPostCode + ",") & _
sCountry
'To create the URL
sUrl = "http://maps.googleapis.com/maps/api/geocode/xml?address=" & sFormatAddress & "&sensor=false"
''XMLDOM to get the XML response
Set oXmlDoc = CreateObject("Microsoft.XMLDOM")
With oXmlDoc
.Async = False
If .Load(sUrl) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then
'Status code
Geocode.sStatus = .selectSingleNode("GeocodeResponse/status").Text
'If a result is returned
If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then
'formatted_address
Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text
'Accuracy
Geocode.sAccuracy = .selectSingleNode("//location_type").Text
'Latitude and longitude
Geocode.dLatitude = Val(.selectSingleNode("//location/lat").Text)
Geocode.dLongitude = Val(.selectSingleNode("//location/lng").Text)
End If
End If
End With
Set oXmlDoc = Nothing
Exit Function
catch:
Set oXmlDoc = Nothing
Err.Raise Err.Number, , Err.Description
End Function
Example Results (Geocode.sRetAddress - formatted address):
good: 100 S Raymond Ave, Alhambra, CA 91801, USA
good: 3040 Salt Creek Ln, Arlington Heights, IL 60005, USA
bad: 4th floor, 2450 Ashby Ave, Berkeley, CA 94705, USA
Question
Any clue if the "Floor" component of the Formatted Address can be excluded, or alternatively explicitly return JUST the desired components?
Thanks,
Mark Pelletier
PS> I am currently counting the number of "," in the string and conditionally handling the parsing task. But As a general purpose approach, there will likely be other exceptions I have not encountered yet.
I'm a bit of a beginner in XPath, but I think I can solve this:
Instead of:
'formatted_address
Geocode.sRetAddress = .selectSingleNode("//formatted_address").Text
Use:
'Build an address:
Geocode.sRetAddress = oXMLDoc.selectSingleNode("descendant::address_component[type='street_number']/short_name").text
Geocode.sRetAddress = Geocode.sRetAddress & " " oXMLDoc.selectSingleNode("descendant::address_component[type='route']/short_name").text
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='locality']/short_name").text
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='administrative_area_level_1']/short_name").text
Geocode.sRetAddress = Geocode.sRetAddress & " " oXMLDoc.selectSingleNode("descendant::address_component[type='postal_code']/short_name").text
Geocode.sRetAddress = Geocode.sRetAddress & ", " oXMLDoc.selectSingleNode("descendant::address_component[type='country']/short_name").text
to manually build up an adress based on components provided by the Google Maps geocode API.
Note that if you're parsing things like cities and states out of this, that's a rather silly thing to do, since they're just available in the XML document. You're better of reading them directly from the XML.
Just re-read and it looks like your specific situation is geared to just hospitals so you will not need to consider all of the issues listed here. I'll leave this up, though, in case someone else is looking to parse addresses containing more than just "floor". And still--you could consider the algorithm for finding just the "root".
I worked on a similar project where I needed to identify the "root" physical address and it can be way more complicated than meets the eye. There are SO many pitfalls to watch out for. I ended up having to build a full-on rules engine. Anticipate every possible combination and account for it.
-2 Main St 4th floor
-2 Main St 3rd floor
-2 Main St Unit 3
-4th floor 2 Main St
-Apt 3 2 Main street
-Apt 3 22 Rte 7
-2 Main st 1st floor
...many more
As a general rule you are usually trying to identify a section of the address formatted as "2 Main Street" where you have a number, a street name, and the suffix describing the street/road/drive, etc. Here is a general algorithm which is only the base. You'll need to expand.
If there are any commas, split the string into separate elements to be evaluated individually
Remove all punctuation in the address elements
Find index of the "Street" You'll have to have a fairly extensive list, but here are some:
Road, Rd, Street, St, Boulevard, Blvd, Blv, Way, Avenue, Ave, Kill, Drive, Dr, Lane, Ln, Path, Highway, Hwy, BiWay, Bwy, Expressway. Circle, Cir, Crossing, Xing, Route, Rte, Rural Route, RR
I'm sure you can think of more.
Find the right-most instance of one of those and work backwards from that index until you find a numeric value (or, more accurately, the index of the beginning of a contiguous set of numerical values).
- Make sure that numeric value is not part of the street name (i.e. "3rd Street), which means make sure the numeric value is not followed by a "rd" or "th" or "nd" or make sure it has a space following it. If it is, keep looking back until you find the numeric part of the street address.
- Once you find the numeric value you will likely have what you need. Grab everything between the numeric value and the "Street".
Other things to be careful of:
- Abbreviation for "Street" and for "Saint" are the same. as in "2 St Francis St"
- Abbreviation for "Doctor" and for "Drive" are the same. "3 Dr Jones Dr"
- "Route"s and "Highways" can have numeric values following them as in "2 Route 5"
- Abbreviations for the many incarnations of "Street/Drive" are very often buried in the street name. "3 Caveman Arrival St" contains "ave" and "rr" an "st"
- Numeric portions may also be written as a word as in "Three Main Street",
If you choose to try and identify unwanted sections of address instead of the desired section, you will likewise need to account for a plethora of potential situations:
Apartment, Apt, Suite, Ste, Floor, Fl, Unit, #, Flat, Box, POBox, PO, Building,, Bldg, Bld, Dorm, Room, Rm
Ultimately, you'll likely end up with many scenarios/exceptions you'd need to account for and many "Cases". You might also consider using regular expressions to identify them. Good luck!
Related
I'm trying to figure out a solution on how to concatenate strings from about 15 different options. Each result comes from a checkbox that is selected based on the state a person has lived in within a certain area.
I know how to turn the checkbox option into a text result. What I'm looking for is how to take these text results, combine them, then ignore null results so there isn't any weird spacing or formatting.
In short, if someone select 3 of the 15 results it would combine the 3 results cleanly and ignore the rest. Example would be: FL, CA, NY
There are, of course, multiple ways that this can be achieved, and since you didn't provide any code or examples of how you are attempting to do this, I will provide two options.
1 - You can concatenate the values using a combination of the & and + operators.
For example, let's say you have 15 checkboxes, all named similarly like chkState01, chkState02 ... through chkState15. And for the simplicity of my sample code, let's assume that when referencing the checkbox control directly in code as chkState01 that it will return either the 2 letter string abbreviation of the State it represents (i.e. NY) if the checkbox was checked, or it will return Null if the checkbox was not checked. With that, you could get your results in 2 ways:
Option A
StateList = (chkState01 + ",") & (chkState02 + ",") & (chkState03 + ",") ....
If those 3 check boxes returned the following values
chkState01 = "NY"
chkState02 = Null
chkState03 = "FL"
Then the result of that concatenation code would be:
NY,FL,
Notice that the string ends with an extra comma (,) and always would since you can't know ahead of time how many of the checkboxes will be checked. You would then need to trim that comma from your list before using it most likely.
Option B
'Create the list with a trailing comma that will need removed
Dim x as Integer
For x = 1 to 15
StateList = StateList & (Me("chkState" & Format(x, "00")) + ",")
Next x
or, you could do:
'Create the list without a trailing comma
Dim x as Integer
For x = 1 to 15
If Not IsNull(Me("chkState" & Format(x, "00"))) Then
If Len(StateList) > 0 Then
StateList = StateList & "," & Me("chkState" & Format(x, "00"))
Else
StateList = Me("chkState" & Format(x, "00"))
End If
End If
Next x
Notice that you can reference a control on a form by "generating" the name of that control as a string and referencing it in the Me("yourcontrolname") format. This is one advantage to naming controls that are similar in a fashion that lends itself to a looping structure like this. The Format command formats the number returned by x as a 2 digit with leading zeros i.e. 1 becomes 01
Further, using & to concatenate two items, where at least one of them is a string, will always result in a string output (Null & "S" = "S"). However, using the + to concatenate two items, where at least one of them is a Null, will always result in Null output (Null + "S" = Null). Hence the checkboxes where the value returns Null does not cause additional commas to be included in the result.
2 - You can write more complicated code to dynamically loop through the checkboxes and build the output list.
More likely, you are going to need to use additional code to determine which checkbox is which state abbreviation and to return the correct string value. Maybe you made the state abbreviation part of the checkbox name i.e. chkState_NY, chkState_FL or maybe you have put the abbreviation in the Tag property of each checkbox.
Let's say you used special control naming chkState_NY, chkState_FL. You could do the following:
Dim ctl as Access.Control
For Each ctl in Me.Controls
If ctl.Name Like "chkState_??" Then
If ctl.Value = True Then
If Len(StateList) > 0 Then
StateList = StateList & "," & Right(ctl.Name,2)
Else
StateList = Right(ctl.Name,2)
End If
End If
End If
Next ctl
I am trying to do a Regular Expression search on string assigned to the HTML content of web search. The pattern I am trying to match has the following format HQ 12345 the second fragment could also start with a letter so HQ A12345 is also a possibility. As shown in the code below the regex pattern I am using is "HQ .*[0-9]".
Problem is when i run the regex search the pattern matched is not just HQ 959693 but also includes the rest of the html file content as shown in the snapshot of the message box below.
Sub Test()
Dim mystring As String
mystring = getHTMLData("loratadine")
Dim rx As New RegExp
rx.IgnoreCase = True
rx.MultiLine = False
rx.Global = True
rx.Pattern = "HQ .*[0-9]"
Dim mtch As Variant
For Each mtch In rx.Execute(mystring)
Debug.Print mtch
MsgBox(mtch)
Next
End Sub
Public Function getHTMLData (ByVal name As String) As String
Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
XMLhttp.setTimeouts 2000, 2000, 2000, 2000
XMLhttp.Open "GET", "http://rulings.cbp.gov/results.asp?qu=" & name & "&p=1", False
XMLhttp.send
If XMLhttp.Status = 200 Then
getHTMLData = XMLhttp.responsetext
Else
getHTMLData = ""
End If
End Function
Use ? to specify non-greedy, otherwise the match will consume up until the last digit of the entire string. Also, you are only matching one digit occurrence. Add a + to specify "one or more" so it will match your goal:
HQ .*?[0-9]+
Alternatively, you can try to use a negated character class like so:
HQ [^0-9]*[0-9]+
Or you can even simplify it further:
HQ [^\d]*\d+
Regex matching is by default greedy. Unfortunately I didn't manage to reproduce precisely your issue, but I am pretty sure it is because you a long string which is being matched by '.*' to a number at the end.
I find this link useful, see the explaination near the bottom about the greediness of *
http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
I suggest changing your Regex to:
HQ .*?[0-9]+
That will match the "HQ " and any number of characters, followed by any number of numeric characters. It will also consume the minimal amount in the ".*", because of the "?".
Please respond if this does not work and I will getting your Regex running in Excel.
Sorry, another question about MsAccess.
I have data set:
Phone Number
444-514-9864
555-722-2273
333-553- 4535
000-000- 0000
550-322-6888
444-896-5371
322-533-1448
222.449.2931
222.314.5208
222.745.6001
I need it to look like (222) 896-5371.
How do I do it in Ms Access or MsExcel?
You can use the Instr, mid, Left and Right functions to make this work. I have made 1 example, with msdn you should be able to figure out the rest
Dim OldPhoneNumber As String
Dim NewPhoneNumber As String
Dim PreFix As String
Dim PreFix2 As String
' You can replace this line in Access, just make sure the full phone number is stored in "OldPhoneNumber"
OldPhoneNumber = Worksheets(<worksheet name>).Range(<cell name>).Value
PreFix = Left(OldPhoneNumber, InStr(1, OldPhoneNumber, "-", 1))
PreFix2 = Left(OldPhoneNumber, InStr(1, OldPhoneNumber, "-", 1) - 1)
NewPhoneNumber = Replace(OldPhoneNumber, PreFix, "(" & PreFix2 & ") ")
Debug.Print (NewPhoneNumber)
Seeing as not all your phone numbers are formatted the same way, you would have to make a different rule for every different formatted phone number (you need 1 that checks for "-" and one that checks for "." You also might want to filter out the spaces
In Access you set the "Input mask" to : "("000") "000"-"0000;1;_
All the references http://office.microsoft.com/en-ca/access-help/input-mask-syntax-and-examples-HP005187550.aspx
Input mask will only work for new data. You will need to create a macro or function to update your existing data to be consistent with your desired format
Referencing this https://developers.google.com/maps/documentation/places/#PlaceDetails
I was expecting the JSON result to always hold all of the address_components details. However, one of my searches ended up missing street_number, postal_code and one of the others. Looking in intellisense revealed one of the address_components returned held an administrative_area_level_2.
This has thrown me into a loop because I was always expecting to have those original values returned even if they were maybe empty. I was also hardcoding the expected return like so:
array.result.address_components(0).long_name
Since I can't expect the components to return all the results, I need a new way to appropriately access them and figure out what to do when I'm missing some.
Here's the case statement I used to handle this problem. It worked, but not sure if it's the most elegant solution.
Dim street As String = ""
Dim city As String = ""
Dim state As String = ""
Dim country As String = ""
Dim zip As String = ""
Dim phone As String = ""
Dim website As String = ""
' No guareentee to the order of even if the data will be there so we check each type. '
For j = 0 To array.result.address_components.Length - 1
Select Case array.result.address_components(j).types(0)
Case "street_number"
street += array.result.address_components(j).long_name()
Case "route"
street += " " & array.result.address_components(j).long_name()
Case "locality"
city = array.result.address_components(j).long_name()
Case "administrative_area_level_1"
state = array.result.address_components(j).long_name()
Case "country"
country = array.result.address_components(j).long_name()
Case "postal_code"
zip = array.result.address_components(j).long_name()
End Select
Next
I basically cycle through the address_components and check their types value. If the value is one I'm looking for I assign it. I later call Trim() on street to remove that white space I add if there's no street number.
I've tried googling, but with limited luck - what I want to do is use VBA to read in coordinates from a serial GPS dongle (or bluetooth Android phone mimicking the former), log a "visit" record, and find the nearest matching record on a table of our clients. Has anyone seen an opensource script that will allow this?
Thanks in advance.
PG
For the serial port data acquisition see the Serial Port Communications page at my web site. Then you can write records to a table using a SQL Insert query or a DAO Recordset with an AddNew. However finding the nearest matching record will likely require using of geometry to figure out the reading through the table looking for the closest location I'd have to refresh my memory as to the exact equations required.
I'm also thinking that to speed up the search you might want to index the latitude and longitude and start your search at nearby lat/longs. That is limit the initial record set to plus/minus, for example, 0.1 of a lat/long which, just guessing would be about 10 kms square.
Here's a rough rundown of what you will need to do.
Lookup Longitude & Latitude For Your Clients' Addresses:
I posted a question a while back on SO asking how to get GPS Coordinates for an address. You can see that question here. There's actually two functions there for you, one to use Google Maps API and another that uses rpc.geocoder.us. Take your pick. Just be aware that each of them having limitations. Google has licensing restrictions as well as maximum queries per day. Geocoder.us has a limit of one query every 15 seconds and I don't remember what their maximum queries per day is, if they even have a limit.
Obviously, you will need to retrieve the Longitude and Latitude for your addresses beforehand, and store this information along with the address. You could possibly try to come up with a zip code for the area and lookup the addresses by that but it could be horribly inaccurate in sprawling urban areas, especially if you have a lot of customers concentrated in a single zip code. You'll need to take Tony's advice here and query your GPS coordinate data using a between statement to get addresses in the near vicinity.
Get GPS Data from the GPS Device
As Tony has already pointed out, you'll need to use something like an ActiveX control, DLL, or API call to perform serial communications with your GPS Device. I have used MS's communication ActiveX control in the past for getting GPS data and it did work satisfactory.
I don't know where my code is to retrieve the incoming data from the GPS device. It's not exactly trivial if you have never programmed serial communications before. You usually have an OnComm event that fires on incoming data. If I remember correctly you loop until an EOF (End of File) code or bit is found which indicates the end of a data stream. If you use MS's Communications Control you can see some sample code here: http://support.microsoft.com/kb/194922
I think I had to use 4800 in my settings instead of 9600 but your requirements may be different depending what type of device you are using.
Extract The Right Data From Your Incoming GPS Data
Step two is to extract the data you need from the incoming GPS Data. You may even find that you have to change it to match the GPS Data you have stored (see below).
In my sample NMEA sentences (above), the Longitude and Latitude are both in the following format:
ddmm.mmmm
For some programs or API's you may have to convert it into Degrees.Degrees, or to word it another way, dd.dddd. The formula to convert it is:
dd.dddd = ddd + mm.mmmm/60
So, for example, if we want to convert my sample data above to exact Longitude and Latitude in degrees, here's what it would look like:
3731.9404 ----> 37 + 31.9404/60 = 37.53234 degrees
10601.6986 ----> 106 + 1.6986/60 = 106.02831 degrees
Here are some functions I wrote back in 2007 to extract certain parts of the data from the NMEA sentence:
Public Function ExtractLatitude(strNMEAString As String, Optional strNMEAStringType As String = "GPRMC") As String
'This function extracts the latitude from an NMEA string and converts it to Decimal Degrees (as a string).
'To use this function you must specify what string type you are passing in, either GPRMC or GPGGA
Dim aryNMEAString() As String
aryNMEAString() = Split(strNMEAString, ",")
Dim dblMinutes As Single, dblLatitude As Single
Select Case strNMEAStringType
Case "GPRMC"
'Latitude is the Number 3 place in the array (4th place in the string)
If aryNMEAString(2) = "A" Then 'A represents a valid string
dblMinutes = (CDbl(Mid(aryNMEAString(3), 3, 7)) / 60)
dblLatitude = CDbl(Left(aryNMEAString(3), 2)) + dblMinutes
ExtractLatitude = CStr(dblLatitude)
End If
Case "GPGGA"
'Latitude is the Number 2 place in the array (3rd place in the string)
If CDbl(aryNMEAString(2)) <> 0 Then 'If string is invalid it will be 0
dblMinutes = (CDbl(Mid(aryNMEAString(2), 3, 7)) / 60)
dblLatitude = CDbl(Left(aryNMEAString(2), 2)) + dblMinutes
ExtractLatitude = CStr(dblLatitude)
End If
End Select
End Function
Public Function ExtractLongitude(strNMEAString As String, Optional strNMEAStringType As String = "GPRMC") As String
'This function extracts the longitude from an NMEA string and converts it to Decimal Degrees (as a string).
'To use this function you must specify what string type you are passing in, either GPRMC or GPGGA
Dim aryNMEAString() As String
aryNMEAString() = Split(strNMEAString, ",")
Dim dblMinutes As Single, dblLongitude As Single
Select Case strNMEAStringType
Case "GPRMC"
'Latitude is the Number 3 place in the array (4th place in the string)
If aryNMEAString(2) = "A" Then
dblMinutes = (CDbl(Mid(aryNMEAString(5), 4, 7)) / 60)
dblLongitude = CDbl(Left(aryNMEAString(5), 3)) + dblMinutes
ExtractLongitude = CStr(dblLongitude)
End If
Case "GPGGA"
'Latitude is the Number 2 place in the array (3rd place in the string)
If CDbl(aryNMEAString(4)) <> 0 Then
dblMinutes = (CDbl(Mid(aryNMEAString(4), 4, 7)) / 60)
dblLongitude = CDbl(Left(aryNMEAString(4), 3)) + dblMinutes
ExtractLongitude = CStr(dblLongitude)
End If
End Select
End Function
Public Function ExtractSpeed(strGPRMC As String) As Integer
'Expects a GPRMC NMEA Sentence
Dim aryGPRMC() As String, dblSpeed As Double
aryGPRMC() = Split(strGPRMC, ",")
If aryGPRMC(7) <> "" Then dblSpeed = CDbl(aryGPRMC(7))
'Convert knots to MPH
ExtractSpeed = CInt(dblSpeed * 1.15077945)
End Function
Public Function ExtractHeading(strGPRMC As String) As Double
'Expects a GPRMC NMEA Sentence
Dim aryGPRMC() As String
aryGPRMC() = Split(strGPRMC, ",")
If aryGPRMC(8) <> "" Then ExtractHeading = CDbl(aryGPRMC(8))
End Function
Public Function ExtractSatelliteCount(strGPGGA As String) As Integer
'Expects a GPGGA NMEA Sentence
Dim aryGPGGA() As String
aryGPGGA() = Split(strGPGGA, ",")
ExtractSatelliteCount = CInt(aryGPGGA(7))
End Function