PDA

View Full Version : Explain this code



nepotist
11-03-2008, 08:33 AM
Hello Can some one explain me the following code.
I found this code in my search to find a way to relate access and google earth.
I want to add google earth feature to my existing database. I would create two more filed in my table one being latitude and the other longitude. as each record would be having its own values and I want the user to be able to view the aerial just by clicking a button.

Google earth uses a script called KML(Key Hole Markup language), which can be written in a notepad and save it a kml file, double clicking the file would take you the location directly. the actual code that for this is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>

so Once I click the button I need to open a note pad, type in the above code(whose latitude and logitude vary with each record), and my search lead to me the following code. Can some one explain to me what each step does.


Option Compare Database
Private Sub Command33_Click()
DoCmd.OpenQuery "ENTERYOURQRYORTABLEHERE"
'Export data to text file

Dim MyDB As Database
Dim MyRS As Recordset
Dim fld As Field
Dim strText As String
Dim MyTableName As String
Dim QryOrTblDef As String
QryOrTblDef = "ENTERYOURQRYORTABLEHERE"
Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset(QryOrTblDef)

Open "ENTERKMLFILENAME" For Output Shared As #1

Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #1, "<kml xmlns=""http://earth.google.com/kml/2.1"">"
Print #1, "<Document>"
Print #1, " <name>tools.kml</name>"
Print #1, " <Folder>"
Print #1, " <name>Tools</name>"
Print #1, " <open>1</open>"
Print #1, " <Folder>"
Print #1, " <name>Tools</name>"
Print #1, " <open>1</open>"
Print #1, " <Snippet maxLines=""2"">Tools</Snippet>"
Print #1, " <description><![CDATA[]]></description>"
With MyRS
Do Until .EOF
Print #1, " <Placemark>"
'Print #1, " <Point>"
strText = " <description><![CDATA[" & MyRS.Fields(3) & "<br><br>" & MyRS.Fields(4) &
"]]></description>"
Print #1, strText
strText = " <address> " & MyRS.Fields(0) & "</address>"
Print #1, strText
strText = " <name>" & MyRS.Fields(1) & "</name>"
Print #1, strText
'Print #1, " <Snippet maxLines="; 2; "></Snippet>"
'Print #1, " <description><![CDATA[]]></description>"
'Print #1, " <LookAt>"

'Print #1, " </LookAt>"
'Print #1, " </Point>"
Print #1, " </Placemark>"
.MoveNext
Loop
End With
Print #1, " </Folder>"
Print #1, " </Folder>"
Print #1, " </Document>"
Print #1, "</kml>"
Close #1
MyRS.Close
Set MyRS = Nothing
Set MyDB = Nothing
'Shell "ENTERKMLFILENAMEHERE"
End Sub

One more question:

Does anyone know if there is a way in which I could embed Google earth with in the form(like using plugin or some other way)

Thank you for you help

nepotist
11-03-2008, 11:09 AM
Private Sub cmdaerial_Click()
DoCmd.OpenQuery "qryIntEncumberedTrips"

Dim db As Database
Dim rstAerial As Recordset
Dim fld As Field
Dim qry As String
Dim strtext As String
Set db = CurrentDb()
qry = "qryIntEncumberedTrips"
strtext = "<coordinates>" & rstAerial.Fields(15) & "," & rstAerial.Fields(16) & ",0</coordinates>" 'xxxxxxx
Set rstAerial = db.OpenRecordset(qry)
Open "KMLFile" For Output Shared As #1
Print #1, " <?xml version=""1.0""encoding=""UTF-8""?> "
Print #1, "<kml xmlns=""http://www.opengis.net/kml/2.2"" > "
Print #1, " <Placemark>"
Print #1, "<name>Simple placemark</name>"
Print #1, "<description>Attached To the ground. Intelligently places itself"
Print #1, "the height of the underlying terrain.</description>"
Print #1, "<Point>"
Print #1, strtext
Print #1, "</Point>"
Print #1, "</Placemark>"
Print #1, "</kml>"
Close #1
rstAerial.Close
Set rstAerial = Nothing
Set db = Nothing
Shell "KMLFile"
End Sub

Well I guess most of you are lost with the KML part ... I have tried the following with my database.The code is posted above.
When I said if some one could explain me the code I meant this line and the rest of the part i believe is printing the code.
Open "KMLFile" For Output Shared As #1


In the above posted code I am getting a error
[
Runtime erro 91
object variable or with block variable not set
I am getting the error at the line where I COMMENTED xxxxxxx.
As we come to the end of code we have a syntax to close the rst.. but there is no syntac that say to save it in kml format or open it ??:dunno :banghead: :think:

Any clues

CreganTur
11-03-2008, 12:16 PM
Can you post a link to where you found this code?

nepotist
11-03-2008, 12:20 PM
@ randy I guess , i need to google it agian.
here is the code that I modified as per my requirement
Private Sub cmdaerial_Click()
'DoCmd.OpenQuery "qryIntEncumberedTrips"

Dim db As Database
Dim rstAerial As Recordset
Dim fld As Field
Dim qry As String
Dim strtext As String
Dim id

id = Form_TMC_Dairy.Intersection_ID.Value
Set db = CurrentDb()
'qry = "SELECT tblcmsintersection.globalID, tblcmsintersection.Latitude, tblcmsintersection.Longitude FROM tblCMSIntersections WHERE (tblCMSIntersections.GlobalID = " & id & " );"


'Set rstproTrips = db.OpenRecordset("SELECT tbltripdiary.CONCURRENCYID, tbltripdiary.linknumber,tbltripdiary.type from tbltripdiary where tbltripdiary.concurrencyid = " & projectID & ";")

Set rstAerial = db.OpenRecordset("SELECT tblcmsintersections.globalID,tblcmsintersections.Latitude,tblcmsintersectio ns.Longitude FROM tblCMSIntersections WHERE (tblCMSIntersections.GlobalID = " & id & " );")
strtext = "<coordinates>" & rstAerial.Fields(1) & "," & rstAerial.Fields(2) & ",0</coordinates>"

Open "KMLFile" For Output Shared As #1
Print #1, " <?xml version=""1.0""encoding=""UTF-8""?> "
Print #1, "<kml xmlns=""http://www.opengis.net/kml/2.2"" > "
Print #1, " <Placemark>"
Print #1, "<name>Simple placemark</name>"
Print #1, "<description>Attached To the ground. Intelligently places itself"
Print #1, "the height of the underlying terrain.</description>"
Print #1, "<Point>"
Print #1, strtext
Print #1, "</Point>"
Print #1, "</Placemark>"
Print #1, "</kml>"
'Close #1
'rstAerial.Close
'Set rstAerial = Nothing
'Set db = Nothing
Shell "KMLFile"
End Sub

the error that get is file not found, guess that makes sense as i havent saved the file. i tried to use the object browser to find the syntax for it but wasnt able to do so.

nepotist
11-03-2008, 12:25 PM
http://www.access-programmers.co.uk/forums/showthread.php?t=151797 hereis the link