View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jake Marx[_3_] Jake Marx[_3_] is offline
external usenet poster
 
Posts: 860
Default Retrieving Web POST data

Hi Seth,

Here's a bit of code that should give you what you need. In order to run
Demo(), you'll have to set a reference to "Microsoft XML, v5.0" via Tools |
References. Basically, you could call the guGetPollingInfo function for
each row of input data, then return the appropriate values back to the
output columns as appropriate.

Public Type guPollingInfo
AssemblyDistrict As String
ElectionDistrict As String
PollSiteNumber As String
End Type

Public Function guGetPollingInfo(rsStreetNumber As String, _
rsStreetName As String, rsBorough As String) As guPollingInfo
Dim xml As XMLHTTP50
Dim sPost As String
Dim abytPostData() As Byte
Dim sResponse As String
Dim uPI As guPollingInfo
Dim lPos As Long
Dim lStart As Long
Dim lEnd As Long

sPost = "number=" & rsStreetNumber & "&"
sPost = sPost & "street=" & rsStreetName & "&"
sPost = sPost & "borough=" & rsBorough

abytPostData = StrConv(sPost, vbFromUnicode)
Set xml = New XMLHTTP50
With xml
.Open "POST", _
"http://gis.nyc.gov/vote/ps/index.htm"
.setRequestHeader "Content-Type", _
"application/x-www-form-urlencoded"
.send abytPostData
sResponse = .responseText
End With

Set xml = Nothing

'/ get poll site #
lPos = InStr(1, sResponse, "Poll Site Number:", vbTextCompare)
If lPos Then
lStart = InStr(lPos, sResponse, "</strong", vbTextCompare) + 9
If lStart Then
lEnd = InStr(lStart, sResponse, "<br", vbTextCompare) - 1
If lEnd Then
uPI.PollSiteNumber = Trim$(Mid$(sResponse, lStart, lEnd -
lStart + 1))
End If
End If
End If

'/ get assembly district
lPos = InStr(1, sResponse, "Assembly:", vbTextCompare)
If lPos Then
lStart = InStr(lPos, sResponse, "</strong", vbTextCompare) + 9
If lStart Then
lEnd = InStr(lStart, sResponse, "<br", vbTextCompare) - 1
If lEnd Then
uPI.AssemblyDistrict = Trim$(Mid$(sResponse, lStart, lEnd -
lStart + 1))
End If
End If
End If

'/ get election district
lPos = InStr(1, sResponse, "Election:", vbTextCompare)
If lPos Then
lStart = InStr(lPos, sResponse, "</strong", vbTextCompare) + 9
If lStart Then
lEnd = InStr(lStart, sResponse, "<br", vbTextCompare) - 1
If lEnd Then
uPI.ElectionDistrict = Trim$(Mid$(sResponse, lStart, lEnd -
lStart + 1))
End If
End If
End If

guGetPollingInfo = uPI
End Function

Public Sub demo()
Dim uPI As guPollingInfo

uPI = guGetPollingInfo("123", "1st ave", "Manhattan")

Debug.Print "Assembly: " & uPI.AssemblyDistrict
Debug.Print "Election: " & uPI.ElectionDistrict
Debug.Print "Poll Site: " & uPI.PollSiteNumber
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Seth wrote:
Hi. I'm reasonably proficient in excel, but most of my data importing
experience has been using text files, csv's, or occasionally html
tables.

In this particular instance, there's a page on a web site with a post
form, and i'd like to create a query for excel to look up each
parameter in the appropriate column, and deliver retrieved data into a
separate column.

Specifically, I'm trying to use this page:
http://gis.nyc.gov/vote/ps/index.htm

I can set up a column for number, street, and borough. I'd like the
Assembly district, election district, and poll site number and address
each posted into their own columns.

Someone please help if you can. Thank you.

Best,
Seth Samuels