Need to extract data from SQL database

Hello,

I am using a query_string_decoded text box and webquerysave in my form and “get” in order to send values to my agent. I have the two values

(t_orig and t_dest) parsed out, but I can’t get any output from the external database. (I know that it is connected) Below is my code that I have tried to send queries to the database with:

Could someone troubleshoot or point me in the right direction? Thanking you in advance!

'—Code for sql database AFTER parsing values–

Dim con As ODBCConnection

Dim qry As ODBCQuery

Dim result As ODBCResultSet

Set con = New ODBCConnection

Set qry = New ODBCQuery

Set result = New ODBCResultSet

Set qry.Connection = con

Set result.Query = qry

con.ConnectTo(“myodbc3-trans”)

'retrieive parsed variables from user

torig2 = s.DocumentContext.GetItemValue(“t_orig”)

tdest2 = s.DocumentContext.GetItemValue(“t_dest”)

'Send query to SQL database

qry.SQL = "SELECT DISTINCT torig, tdest, tldest FROM Trans WHERE

torig=‘torig2%’ AND tdest=‘tdest2%’"

'Retrieve variables from SQL Query

Torig = result.GetValue(“torig”, torig)

Tdest = result.GetValue(“tdest”, tdest)

'Call Display

Call DisplayResultSetProperties

result.Close(DB_CLOSE)

con.Disconnect

'-close connection

End Sub

Subject: Need to extract data from SQL database

i didn’t see resultset.execute

Subject: As well as not not executing the query…

Your SQL statement is wrong: qry.SQL = “SELECT DISTINCT torig, tdest, tldest FROM Trans WHERE torig='” + torig2(0) + “’ AND tdest='” + tdest2(0) + “'”

Subject: RE: As well as not not executing the query…

I got an error stating that my torig2 variables cannot take on a (0) parameter? Is there a solution to this or is the syntax incorrect? Thanks

Subject: That is strange…

The following lines should return a variant array… torig2 = s.DocumentContext.GetItemValue(“t_orig”)

tdest2 = s.DocumentContext.GetItemValue(“t_dest”)

This line is just a string i.e. the “WHERE torig=‘torig2%’” is the actual string being sent as the query. Which I assume is not exactly what you wanted. What I thing your query wanted to be was using the value in torig2 that you got above. Something like “WHERE torig=‘100’ AND tdest=‘200’”. The question is did you Dim torig2 as Integer? Are the values in the NotesDocument Number or Text?

My suggestion is to Dim torig2 As Variant, tdest2 As Variant and then try my previous suggestion. If the values are not text use &'s to concatenate the strings…

qry.SQL = “SELECT DISTINCT torig, tdest, tldest FROM Trans WHERE torig='” & torig2(0) & “’ AND tdest='” & tdest2(0) & “'”

Subject: Need to extract data from SQL database

You never actually query the database :wink:

qry.SQL = "SELECT DISTINCT torig, tdest, tldest FROM Trans WHERE _

torig=‘torig2%’ AND tdest=‘tdest2%’" 'Retrieve variables from SQL Query

’ *** add something like…

If Not result.Execute Then

      Messagebox _

      result.GetExtendedErrorMessage,, result.GetErrorMessage

      Exit Sub

 End If

Subject: RE: Need to extract data from SQL database

I definitly need the Error clause, The messagebox function would not work on the web thought right? Thank you for the tips though. Could you or any else see the other problems with my code?

Subject: Need to extract data from SQL database

Ok, I finally got it to work a few days ago, thank you all for your help!!! Here is the complete code (including parsing) if anyone needs it for the future…—AGENT CODE—

Option Public

Uselsx “*LSXODBC”

Dim db As NotesDatabase

Dim doc As NotesDocument

Sub Initialize

Dim s As New NotesSession

Set db = s.CurrentDatabase

Set doc = s.DocumentContext

Dim AgentArgs List As String

Dim torig As String

Dim tdest As String

Dim tldest As String

Dim tlorig As String

Dim torig2 As String

Dim tdest2 As String

Dim transoption As String

Dim tlduration As String



Call ExplodeQueryString (doc.Query_String_Decoded(0), AgentArgs)



'Check to see if values exist

If Iselement( AgentArgs ("t_orig") ) Then

	Print "<br>"

	Print " <H3>Directions from " + AgentArgs ("t_orig")

Else

	Print "There is an error in the URL that was used."

	Print "<br>"

End If



If Iselement( AgentArgs ("t_dest") ) Then

	Print "<H3> To " + AgentArgs ("t_dest")

	Print "<br>"	

Else

	Print "There is an error in the URL that was used."

	Print "<br>"

End If



'---Code for sql query after parsing values---

Dim con As ODBCConnection

Dim qry As ODBCQuery

Dim result As ODBCResultSet



Set con = New ODBCConnection

Set qry = New ODBCQuery

Set result = New ODBCResultSet

Set qry.Connection = con

Set result.Query = qry

con.ConnectTo("myodbc3-trans")



'retrieve parsed variables from user



torig2 = AgentArgs ("t_orig")

tdest2 =  AgentArgs ("t_dest")



'Send query to SQL database

qry.SQL = "SELECT DISTINCT torig, tdest, tldest, tlorig, transoption, tlduration FROM Trans WHERE opt_num ='1' AND torig='" + torig2 + "' AND tdest='" + tdest2 + "'"



result.Execute	

If Not result.Execute Then

	result.GetErrorMessage

	Print "No chance"

End If



'Retrieve variables from SQL Query



Do

	result.NextRow 

	'torig = result.GetValue("TORIG", torig)

	'tdest = result.GetValue("TDEST", tdest)

	tldest = result.GetValue("TLDEST", tldest)

	tlorig = result.GetValue("TLORIG", tlorig)

	transoption = result.GetValue("TRANSOPTION",transoption)

	tlduration = result.GetValue("TLDURATION", tlduration)	

	Print"<table border = 1cellpadding = 10 cellspacing = 3>"	

	'Print"<tr>"

	'Print"<td col span = 2>"

	'Print torig	" to "

	'Print tdest

	Print"<tr>"

	Print"<td>"

	Print tlorig " to "

	Print"<td>"

	Print tldest

	Print"<td>"

	Print "Using " + transoption

	Print"<td>"

	Print  tlduration + " Mins"

	Print "<tr>"	

	Print"</table>"

Loop Until result.IsEndOfData



result.Close(DB_CLOSE)

con.Disconnect

'-close connection

End Sub

—Called Functions—

Private Sub ExplodeQueryString (QueryString As String, AgentArgs List As String)

Dim Args As String 

Args = RightStr(QueryString, "OpenAgent&")



Dim ArgsList As Variant 

ArgsList = Evaluate ({@Explode("} & Args & {"; "&")})



Dim ArgKey As String

Dim ArgValue As String



Forall Arg In ArgsList

	ArgKey = LeftStr(Arg, "=")

	ArgValue = RightStr(Arg, "=")

	AgentArgs(ArgKey) = ArgValue

End Forall

End Sub

Function LeftStr(OrigStr, LeftOf ) As String

Dim Pos As Integer

Dim OrigStrLen As Integer

Pos = Instr( Lcase(OrigStr), Lcase(LeftOf) )

OrigStrLen = Len(OrigStr)

If pos>0 Then

	LeftStr = Left( OrigStr, (Pos-1))

Else

	LeftStr = OrigStr

End If

End Function

Function RightStr(OrigStr, RightOf ) As String

Dim Pos As Integer

Dim OrigStrLen As Integer

Dim RightOfLen As Integer

Pos = Instr( Lcase(OrigStr), Lcase(RightOf) )

OrigStrLen = Len(OrigStr)

RightOfLen = Len(RightOf)

If Pos>0 Then

	RightStr = Right( OrigStr, OrigStrLen - (RightOfLen+Pos-1))

Else

	RightStr = OrigStr

End If

End Function