WebSearch within VBA

ianclegg

Registered User.
Local time
Today, 15:42
Joined
Jul 14, 2001
Messages
58
Can anyone help, I am wanting to lookup place locations from a field held in a table. eg 'barnby dun', I need to know which county it is in.

Can anyone suggest any method or code that can help?

Thanks in anticipation

Regards

Ian Clegg
 
Can anyone help, I am wanting to lookup place locations from a field held in a table. eg 'barnby dun', I need to know which county it is in.

Can anyone suggest any method or code that can help?

Thanks in anticipation

Regards

Ian Clegg
you can easily perform a google search through vba and loop through your table and record you findings. As a matter of fact, if you can find a website that gives you a country location for each place that you enter in, say, a seach box on a page, all you would need to do is loop your table, use the search box for the place to search, click the search button, and use the "getElementById()" method in HTML to populate another table filed with the control value where your search result is stored on the post-search URL page. It could really be that easy...
 
Thanks for that, however I was looking for help with the code to perform the websearch through VBA

Regards
 
ian,

you'll have to learn some HTML to do this. here's a cool little sample that will log into this forum site and do a little searching for ya...
Code:
Option Compare Database

Function test()

On Error Resume Next

Dim ie As Object, i As Integer, z As Integer, counter As Integer
Dim Names(5) As String
Set ie = CreateObject("internetexplorer.application")

Names(0) = "ajetrumpet"
Names(1) = "ianclegg"
Names(2) = "boblarson"
Names(3) = "marianne"
Names(4) = "rainlover"
Names(5) = "smiler44"

ie.Visible = True

      ie.navigate "http://www.access-programmers.co.uk/forums/index.php"

         While ie.busy
            DoEvents
         Wend

   ie.document.all("navbar_username").Value = "[COLOR="Red"]YOUR LOGIN NAME[/COLOR]"
   ie.document.all("navbar_password").Value = "[COLOR="Red"][COLOR="Red"]YOUR PASSWORD[/COLOR][/COLOR]"
   ie.document.Forms(0).submit

         While ie.busy
            DoEvents
         Wend

For z = 0 To 5

ie.navigate "http://www.access-programmers.co.uk/forums/search.php"

         While ie.busy
            DoEvents
         Wend

   ie.document.all("userfield_txt").Value = Names(z)
   ie.document.all("starteronly").Value = "1"
   ie.document.Forms(2).submit

         While ie.busy
            DoEvents
         Wend

Debug.Print "Threads Started By " & Names(z) & ":"

counter = 0
For i = 0 To 1000
   If ie.document.all(i).id Like "thread_title*" Then
      counter = counter + 1
         Debug.Print counter & ") " & ie.document.all(i).innerHTML
   End If
Next i
   If counter = 0 Then
      Debug.Print "NO THREADS FOUND FOR THIS USER."
   End If

Debug.Print vbCr

Next z

Set ie = Nothing

End Function
 
Thanks for that,

Are there some references that need adding?, my list contains the following at the moment

Visual basic for applications
access 11 obect library
ole automation
dao 3.6 object library
activex data objects 2.1 library
common dialog control 6.0 sp3
scripting runtime

I am running office 2003

Regards
 
you don't need any additional refs for that example i gave you. put the code in an access module and put in your UN and PASSWORD and give it a try. see what it does...
 
Hi

After creating a module & a macro to run the module

I get the message apiShowWindow sub or function not defined



Ian
 
Hi

After creating a module & a macro to run the module

I get the message apiShowWindow sub or function not defined



Ian
ian,

i took that portion out of that function i posted. that was an api call that i had in another database of mine. you need to take those two lines out in order for it to work for you...

just take out the apiSHOWINDOW line and ul be fine
 
That's exactly what I have been looking for, Thanks very much


Ian
 
I hope that it is ok to post a supplementary question

I have compressed the code into the following

The parameter is passed to the function by the code behind the form as in

SearchArea = recsetip("area")................read record
FindCountyName (SearchArea)...............extract areacode

Function FindCountyName(area).....From the module
On Error Resume Next
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "http://www.wikipedia.org/wiki/" + area
End Function

If you search for Dewsbury in wikipedia eg

www.wikipedia.org/wiki/dewsbury

the table at the bottom righthand corner shows the detail that I require

Metropolitan Bourough - Kirklees
Metropolitan County - West Yorkshire

How do I retreive the data Kirklees & West Yorkshire from Wikipedia into my dataareas in access

Regards

Ian
 
ian,

the values you are looking for are actual links inside of a table. they are buried inside table cell objects that have the <td>(table data) tag. i am not quite sure how to go about getting those kind of values, because they are not actual controls that you can pull from. however, i do know this:

you CAN get the number of elements in a tag collection. for example, to get the number of <td>'s there are in a webpage code, you write this in VBA:
Code:
debug.print document.getElementsByTagName("td").length
now, seeing that every page you are going to be searching is going to have that same table in it, and most likely will have the same number of cells and rows in it, i would bet my last dollar that you can reference the actual INDEX of the <td> collection in HTML, and simply pull the "innerHTML" property from that specific element in the page. what i do not know how to do is get an element NUMBER (index value) in VBA. i know for a fact that the syntax for getting the first element in a <td> collection in javascript code or HTML is this:
Code:
document.getElementsByTagName("td")[0]
however, in VBA, doing this automatically parses out the [0] and preceeds it with a ";". i have no idea why. probably because this syntax is not compat. in VBA.

at any rate, if you can figure out how to get the index number syntax to work for the <td> collection in VBA, as i have outlined above, most likely you will be able to retrieve those values like this:
Code:
document.GetElementsByTagName("td")[INDEX VALUE of your element].innerHTML
here is the HTML from the page that you gave in your last post that you would need to pull from:
Code:
<th><a href="/wiki/Districts_of_England" title="Districts of England">Metropolitan borough</a></th>
<td><a href="/wiki/Kirklees" title="Kirklees">Kirklees</a></td>
</tr>
<tr class="mergedrow">

<th><a href="/wiki/Metropolitan_and_non-metropolitan_counties_of_England" title="Metropolitan and non-metropolitan counties of England">Metropolitan county</a></th>
<td><a href="/wiki/West_Yorkshire" title="West Yorkshire">West Yorkshire</a></td>
 

Users who are viewing this thread

Back
Top Bottom