Common.inc Sample Script

This script contains subroutines used in both Response.asp and Clarify.asp.

<!-------------------------------------------------------------------

Microsoft English Query

Copyright 1998 Microsoft Corporation. All Rights Reserved.

Component: Sample Scripts

File: Common.inc

Common subroutines

-------------------------------------------------------------------->

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Function GetEQSession(strDomainFile)

'----- Get the current or initialize a new English Query Session-----

    if IsObject(Session("EQSession")) Then

        '----- Remember the existing session -----

        Set objEQSession = Session("EQSession")

    else

        '----- Initialize a new session -----

        Set objEQSession = Server.CreateObject("MSEQ.Session")

        objEQSession.InitDomain(Server.MapPath(strDomainFile))

        objEQSession.ClarifySpellingErrors = False

        Set Session("EQSession") = objEQSession

    End If

    set GetEQSession = objEQSession

End Function

Sub ProcessResponse(objEQResponse, strQuestion)

'----- Handle a response from English Query -----

    '----- Response type constants -----

    nlCommandResponse            = 0

    nlErrorResponse            = 2

    nlUserClarifyResponse         = 3

    Set Session("EQResponse") = Nothing

    Response.Write "<FORM ACTION=request.asp>" & _

                "<INPUT TYPE=SUBMIT VALUE='New Question'>" & _

                "</FORM>"

    Response.Write "<B>Question:  </B>" & strQuestion & "<BR>"

    Select Case objEQResponse.Type

        Case nlCommandResponse

            DoCommands objEQResponse

        Case nlUserClarifyResponse

            DoClarification objEQResponse, strQuestion

        Case nlErrorResponse

            Response.Write objEQResponse.Description & "<BR>"

            Response.End

        Case Else

            Response.Write "Error: Unhandled response type<BR>"

            Response.End

    End Select

End Sub

Sub DoCommands(objEQResponse)

'----- Handle a Command Response -----

    '----- Command type constants -----

    nlQueryCmd            = 1

    nlAnswerCmd            = 2

    Response.Write "<B>Restatement:  </B>" & objEQResponse.Restatement & "<BR>"

    '----- Handle each command -----

    Set objCommands = objEQResponse.Commands

    For intCommand = 0 To objCommands.Count - 1

        Set objCommand = objCommands(intCommand)

        Select Case objCommand.CmdID

            Case nlQueryCmd

                DoSQLCommand objCommand

            Case nlAnswerCmd

                Response.Write objCommand.Answer

            Case Else

                Response.Write "Error: Unhandled command type<BR>"

        End Select

    Next

End Sub

Sub DoClarification(objEQResponse, strQuestion)

'----- Handle a UserClarification Response -----

    '----- UserInput type constants -----

    nlListInput        = 0

    nlTextInput        = 1

    nlStaticInput    = 2

    Response.Write "<BR><B>Please Clarify:</B><BR>"

    Response.Write "<FORM ACTION=""clarify.asp"" METHOD=POST>"

    '----- Handle each required user input -----

    Set objInputs = objEQResponse.UserInputs

    For intInput = 0 to objInputs.Count - 1

        Set objInput = objInputs(intInput)

        strInputName = "Input" & intInput

        Response.Write objInput.Caption

        Select Case objInput.Type

            Case nlStaticInput

            Case nlTextInput

                Response.Write "<BR><INPUT TYPE=TEXT NAME=" & strInputName & " SIZE=40 MAXLENGTH=50>"

            Case nlListInput

                aItems = objInput.Items

                intSelection = objInput.Selection ' default selection

                If intSelection < 0 Then

                    intSelection = 0

                End If

                '----- Write each choice as a radio button -----

                For intItem = 0 To UBound(aItems, 1)

                    If intItem = intSelection Then

                        strChecked = " CHECKED"

                    Else

                        strChecked = ""

                    End If

    

                    Response.Write "<BR><INPUT TYPE=RADIO NAME=" & _

                        strInputName & " VALUE=" & intItem & _

                        strChecked & ">" & aItems(intItem)

                Next

            Case Else

                Response.Write "Error: Unexpected input type<BR>"

        End Select

    Next

    Response.Write "<INPUT TYPE=HIDDEN NAME=reqRequestTyped VALUE=""" & strQuestion & """>"

    Response.Write "<BR><BR>"

    Response.Write "<INPUT TYPE=SUBMIT VALUE=Submit>" & _

                "<INPUT TYPE=RESET VALUE=Reset>"

    

    '----- Save the question and response for the clarification page -----

    Session("Question") = strQuestion

    Set Session("EQResponse") = objEQResponse

End Sub

Sub DoSQLCommand(objCommand)

'----- Handle an SQL command -----

    '----- ADO constants -----

    adOpenForwardOnly            = 0

    adOpenKeyset            = 1

    adOpenDynamic            = 2

    adOpenStatic            = 3

    adConcurReadonly            = 1

    '----- Connect to SQL Server via ADO -----

    Set objDataConnection = Server.CreateObject("ADODB.Connection")

    objDataConnection.Open "Pubs", "guest", "guest"

    objDataConnection.CommandTimeout = 60

    On Error Resume Next

    Err.Clear

    If Not objCommand.DisplayToUser Then

        '----- Silently execute the SQL -----

        objDataConnection.Execute objCommand.SQL

        CheckADOError objDataConnection, objCommand.IgnoreError

    Else

        '----- Execute the SQL and display the result set -----

        intMaxRows = objCommand.DisplayRows

        If intMaxRows = 0 Then

            intMaxRows = 25

        End If

        Set objResultSet = Server.CreateObject("ADODB.RecordSet")

        objResultSet.MaxRecords = intMaxRows

        objResultSet.Open objCommand.SQL, objDataConnection, adOpenForwardOnly, adConcurReadOnly

        CheckADOError objDataConnection, objCommand.IgnoreError

        DisplayTable objResultSet, intMaxRows, objCommand.TableCaption, objCommand.TrueFalseAnswer

        objResultSet.Close

    End If

    objDataConnection.Close

End Sub

Sub CheckADOError(objDataConnection, fIgnoreError)

'----- Handle errors in executing the SQL -----

    If Not fIgnoreError and Err <> 0 Then

        Set objError = objDataConnection.Errors(0)

        Response.Write objError.Source & objError.Description

        Response.End

    End If

    Err.Clear

End Sub

Sub DisplayTable(objResultSet, intMaxRows, strTitle, fTrueFalse)

'----- Display a result table -----

    '----- Handle true/false questions -----

    If fTrueFalse Then

        If objResultSet.EOF Then

            strAnswer = "FALSE"

        Else

            strAnswer = "TRUE"

        End If

        Response.Write("<BR>The answer is " & strAnswer & ".<BR>")

        Exit Sub

    End If

    '----- Handle empty result tables -----

    if objResultSet.EOF Then

        Response.Write("No rows were found")

        Response.End

    End If

    Response.Write("<BR>" & strTitle & "<BR><BR>")

    Response.Write("<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>")

    '----- Display the column captions -----

    Dim intCaptionCol

    For intCaptionCol = 0 To objResultSet.Fields.Count - 1

        Response.Write "<TD BGCOLOR=#800000> <FONT COLOR=WHITE>" & _

                objResultSet(intCaptionCol).Name & "</FONT></TD>"

    Next

    '----- Fill the table body -----

    intRows = 0

    While Not objResultSet.EOF

        intRows = intRows + 1

        Response.Write("<TR>")

        For intCol = 0 To objResultSet.Fields.Count - 1

            value = objResultSet.Fields(intCol).Value

            If IsNull(value) Then

                value = " "

            End If

            Response.Write "<TD BGCOLOR=f7efde>" & value & "</TD>"

        Next

        objResultSet.MoveNext

        Response.Write "</TR>"

    Wend

    Response.Write "</TABLE>"

End Sub

</SCRIPT>


(c) 1996-1998 Microsoft Corporation. All Rights Reserved.