2014-08-27 261 views
0

我有點卡住了一段時間在一個小項目 生成結果從幾個 SQL查詢在幾個Excel表格,我想使用SQL Server 2008,這是我第一次編碼VBA 我想這個代碼(對於SQL單個查詢),但我仍然有編譯問題執行SQL查詢從excel

Sub New_Feuil1() 
    ThisWorkbook.Activate 

    'First clear the contents from the query 
    Worksheets("Feuil1").Select 
    Range("A2").Select 
    Do Until ActiveCell = "" 
     ActiveCell.Offset(1).Select 
    Loop 
    Range("A4", ActiveCell.Offset(-1, 3)).ClearContents 

    'Get reporting date 
    ReportingDate = ThisWorkbook.Sheets("Parameters").Range("D1") 

    'Format the value for use in the SQL query 
    ReportingDateFor = Format(ReportingDate, "yyyy-mm-dd") 

    Worksheets("Feuil1").Select 
    Range("A1").Select 

    Dim cnn As New ADODB.Connection 
    Dim rst As New ADODB.Recordset 
    Dim StrQuery1 As String 
    Dim ConnectionString As String 

    ConnectionString ="ODBC;" & _ 
    "Driver={SQL Server Native Client 10.0};" & _ 
    "Server=187.125.254.231;" & _ 
    "Database=database;" & _ 
    "UID=sa; PWD=pwd" 
    cnn.Open ConnectionString 
    cnn.CommandTimeout = 900 

    'Queries to be executed 
    StrQuery1 = StrQuery1 & "Select Id from Users" 

    rst.Open StrQuery1, cnn, adOpenForwardOnly, adLockReadOnly 
    rst.Close 

    Debug.Print "StrQuery1:"; StrQuery1 
    cnn.Close 

    ThisWorkbook.Sheets("Central Dashboard").Select 
    Sheets("Feuil1").Range("A2").CopyFromRecordset rst 

End Sub 

還有沒有其他的解決辦法?

+1

能否請你告訴我們具體的** **的問題(S)你有。閱讀[如何編寫一個簡短的,自包含的,正確的示例](http://sscce.org/)並查看此處的[help center](http://stackoverflow.com/help)。然後[編輯](http://stackoverflow.com/posts/25524210/edit)你的問題,並提供所有必要的細節。 – RossC 2014-08-27 10:06:28

+0

每當我運行宏時,它會一直告訴我,我有一個compulation問題 – user3708197 2014-08-27 10:11:16

+1

好的...什麼是編譯問題?我看不到您的電腦。請閱讀我提供的鏈接並編輯您的問題,向我們解釋發生了什麼事情。 「它給出了一個編譯器錯誤」告訴​​我什麼都沒有。 – RossC 2014-08-27 10:13:08

回答

1

看來你是編程新手:)在你使用任何變量之前,請先聲明它們,這將有助於你快速理解它們。

喜歡:

Dim ReportingDate as Date 
ReportingDate = ThisWorkbook.Sheets("Parameters").Range("D1") 

Dim ReportingDateFor As String 
ReportingDateFor = Format$(ReportingDate, "yyyy-mm-dd") 

檢查您的連接字符串。試試這個連接字符串。

ConnectionString = "Driver={SQL Server Native Client 10.0};Server=187.125.254.231;Database=database;UID=sa; PWD=pwd" 

除此之外,看着你的代碼要連接到服務器,打開記錄,關閉記錄,最後關閉連接,然後試圖檢索結果。邏輯上,這行不通:) :)

試試這個:

Dim cnn As New ADODB.Connection 
Dim rst As New ADODB.Recordset 
Dim cmd As ADODB.Command 

Dim ConnectionString As String 
ConnectionString = "Driver={SQL Server Native Client 10.0};Server=187.125.254.231;Database=database;UID=sa; PWD=pwd" 

cnn.Open ConnectionString 

'Queries to be executed 
Dim StrQuery1 As String 
StrQuery1 = StrQuery1 & "Select Id from Users" 

'Prepare SQL execution 
cmd.Name = "SelectUsers" 
cmd.ActiveConnection = conn 
cmd.CommandText = StrQuery1 

Set rst = cmd.Execute 
If Not rst.EOF Then 
    With Sheets(1).Cells ' Enter your sheet name and range here 
     .ClearContents ' clears the entire sheet 
     .CopyFromRecordset rst ' copy the result 
    End With 
Else 
    MsgBox "no records found.." 
End If 

'After work done close connection 
On Error Resume Next 
rst.Close 
cnn.Close 
Set rst = Nothing 
Set cnn = Nothing