2014-12-21 23 views
0

我偶然發現了一個編譯錯誤,但沒有得到什麼問題。當試圖將符號添加到輸入變量(TickerID)時,出現錯誤,在輸入例如「yhoo」作爲雅虎代碼名稱時完美無缺。編譯錯誤:需要的常量表達式

代碼

Private Sub CmdBtn_Add_Click() 
'---------------------------------------------------------------------------------------' 
' Checks that inputted ticker name is correct and calls import class after confirmation 
'---------------------------------------------------------------------------------------' 

' General Variables---------' 
    Dim TickerID As String: TickerID = UCase(Add_Instrument.TxtBox_Instrument.Value) 
'--------------------------' 

    'Check if input field is not empty 
    If TickerID = "" Or Application.WorksheetFunction.IsText(TickerID) = False Then 
     MsgBox "Please provide a valid ticker ID" 
     Exit Sub 
    End If 

    Debug.Print TickerID 

    'Check Ticker name exists through YQLBuilder class 
    Dim YQLBuilder As YQLBuilder: Set YQLBuilder = New YQLBuilder 
    Call YQLBuilder.TickerCheck(TickerID) 


'  Call ImportData(TickerID) 

'  MsgBox "Please check the ticker name. It is in the wrong format" 

End Sub 
Public Sub TickerCheck(TickerID As String) 
'---------------------------------------------------------------------------------------' 
' Built 2014-11-05 Allows parsing of XML data through YAHOO API YQL 
' 2014-12-21: Not fully built yet, see where it can be of use 
'---------------------------------------------------------------------------------------' 

' General Variables---------' 
Const ConnStringStart As String = "http://query.yahooapis.com/v1/public/yql?q=" 
Const ConnStringLast As String = "&diagnostics=true&env=store://datatables.org/alltableswithkeys" 
'---------------------------' 


Const ConnStringInput As String = "select * from yahoo.finance.stocks where symbol='" _ 
& TickerID & "'" **<----- Error here!** 

    Debug.Print ConnStringStart & ConnStringInput & ConnStringLast 

    Dim YQLNodes As MSXML2.IXMLDOMNodeList 
    Dim YQLReq As MSXML2.DOMDocument60 

    Set YQLReq = New MSXML2.DOMDocument60 

     YQLReq.async = False 
     YQLReq.Load ConnStringStart & ConnStringInput & ConnStringLast 

    YQLReq.setProperty "SelectionNamespaces", "xmlns:f='http://www.yahooapis.com/v1/base.rng'" 
    Set YQLNodes = YQLReq.SelectNodes("//CompanyName") 

    Dim xNode As MSXML2.IXMLDOMNode 

    For Each xNode In YQLNodes 

     Debug.Print xNode.Text 

    Next xNode 

    Debug.Print YQLNodes.Length 

End Sub 

回答

3

信息是明確的。當你聲明一個常量時,你給它的值也必須是恆定的。在這種情況下,它的一部分是參數TickerId,它是可變的。你不能用變量值聲明一個常量。

爲了解決這個問題,我認爲你可以使用Dim而不是Const而不是使ConnStringInput成爲常數。