VBS公共变量

时间:2011-10-24 18:18:43

标签: html vbscript global-variables scope

我在一个html页面中有VBS代码,可以从访问数据库中获取查询。我正在尝试使用

在html中的正确位置打印查询结果

document.write(fDateDispo & " et " & tDateDispo)

其中fDateDispo和tDateDispo是在程序中所有subs之前声明的公共变量。在我的一个过程(声明Public Sub)中,我使用查询结果向公共变量提供它们的值。我使用msgbox()来确认过程运行时是否归因于正确的值。

我的问题是,我在程序执行过程中给公共变量的值在执行后不会停留,它们只是空的,只有“et”被打印出来。

[编辑] 这是代码:

public sub SendQuery(DateOne,DateTwo)
On Error Resume Next
    dim rs, passerelle, nbrAppels, c, stSQLpass
    dim Objet, nbrObjet, stSQLobj, rsObj
    dim Panne, nbrPanne, stSQLpan, rsPan
    dim SQLfromDate, SQLtoDate, stWhere, FromDate, ToDate


SQLfromDate="SELECT Min(Avis.[Date Appel]) AS [MinDeDate Appel] FROM Avis;"
SQLtoDate="SELECT Max(Avis.[Date Appel]) AS [MaxDeDate Appel] FROM Avis;"


ADOConnection1.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=G:\Planification\Planification\Stagiaires\Sébastien Leblanc\EntretienElectro_Backup2.mdb"
Set FromDate = ADOConnection1.Execute(SQLfromDate)
Set ToDate = ADOConnection1.Execute(SQLtoDate)

fDateDispo=FromDate.Fields(0).Value
tDateDispo=ToDate.Fields(0).Value

With rs
    .MoveFirst
    Do While Not .EOF
        passerelle = passerelle & .Fields(1).Value & Chr(9)
        nbrAppels = nbrAppels & .Fields(0).Value & Chr(9)
        .MoveNext
    Loop
    .Close
End With
' Remove the leftover tab character at the end of the strings.
passerelle = Left(passerelle, Len(passerelle) - 1)
nbrAppels = Left(nbrAppels, Len(nbrAppels) - 1)
' Objet cahrtspace1
With ChartSpace1
    .Clear
    .Charts.Add
    Set c = .Constants
    .AllowFiltering = True     
    .Interior.Color = "white"     
    With .Charts(0)
        .Border.Color = "white"
        .Interior.Color = "white"
        .Axes(0).Hastitle = TRUE
        .Axes(0).Title.Caption = "Passerelles"          'titre de l'axes des x
        .Axes(1).Hastitle = TRUE
        .Axes(1).Title.Caption = "Nombre d'Appels"      'titre de l'axe des y
        .SeriesCollection.Add
        .PlotArea.Interior.Color = "white"              du graph
        With .SeriesCollection(0)
            .Interior.Color = RGB(61, 166, 228)      

            .SetData c.chDimCategories, c.chDataLiteral, nbrAppels
            .SetData c.chDimValues, c.chDataLiteral, passerelle
        End With  

        .Type = c.chChartTypeColumnClustered
    End With
End With


    ADOConnection1.Close
End sub

我遗漏了一些东西,因为我怀疑它与它有什么关系。它只是其他OWC对象和东西...... 我尝试通过在子中执行fDateDispo="date"来归因于一个值,但没有运气。

[编辑2]

这是客户端VBscript。 sub位于<head>中,文档写入html的<body>。sub由另一个子集调用为window_load事件。

[编辑3]

这是我放置document.write()的地方:

    <P style="PAGE-BREAK-AFTER: auto"><FONT face=Arial>&nbsp;&nbsp;&nbsp; Les 
      données entre les dates
      <SCRIPT language=vbscript>

Document.write(fDateDispo & " et " & tDateDispo)

      </SCRIPT>
       sont disponibles.</FONT></P>

2 个答案:

答案 0 :(得分:1)

document.write应该是response.write吗?

答案 1 :(得分:1)

希望我正确地遵循这一点,但要说明我的评论:

<html>
    <head>
        <script language="vbscript">
            dim var: var = Empty

            public sub subroutine()
                var = "blah"
            end sub

            public sub loadhandler()
                call subroutine()
                dim d: set d = document.getElementById("thediv")
                d.innerText = var
            end sub
        </script>
    </head>
    <body onload="call loadhandler()">
        <script language="vbscript">
            document.write "document.write('" & var & "')"
        </script>
        <div id="thediv"/>
    </body>
</html>

加载时,输出为

  

文件撰写( '')
  嗒嗒

意味着document.write()loadhandler()之前被称为,这就是为什么var在输出的第一行中是Empty的原因