[算表] VBA-SQL 引用問題

看板Office作者 (BBS)時間7年前 (2018/06/09 15:27), 編輯推噓0(008)
留言8則, 2人參與, 7年前最新討論串1/2 (看更多)
軟體: Excel 版本: 2016 初次嘗試ADO SQL,失敗收場.... 目的是想從 Local Excel 抓取資料庫的 sheet1 出現錯誤為物件定義錯誤 (請看貼圖說明) 請問這該怎麼解呢? '************貼圖說明************** https: //ppt.cc/fF8LJx *********************************** '**********Code 如下*************** Sub MySQL_1st() Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adCmdText = &H1 Set objConnection = CreateObject("ADODB.Connection") Set objRecordset = CreateObject("ADODB.Recordset") objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Users\Acer\Desktop\excel\SQL\raw\raw_data.xls;" & _ "Extended Properties=""Excel 8.0;HDR=Yes;"";" objRecordset.Open "Select * FROM [sheet1$]", _ objConnection, adOpenStatic, adLockOptimistic, adCmdText Do Until objRecordset.EOF Wscript.Echo objRecordset.Fields.Item("ID") objRecordset.MoveNext Loop End Sub -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.168.88.191 ※ 文章網址: https://www.ptt.cc/bbs/Office/M.1528529264.A.D21.html

06/09 15:39, 7年前 , 1F
wscript.echo改為msgbox
06/09 15:39, 1F

06/09 15:49, 7年前 , 2F
原本效果是想要讀取並寫入 Active sheet
06/09 15:49, 2F

06/09 15:50, 7年前 , 3F
msgbox 只有顯示資料在對話框
06/09 15:50, 3F

06/09 15:50, 7年前 , 4F
後來發現是Excel 無法使用 Wscript.echo...這...
06/09 15:50, 4F

06/09 15:59, 7年前 , 5F
要讀取寫入所在工作表儲存格的話,可以range = objrecords
06/09 15:59, 5F

06/09 16:00, 7年前 , 6F
et.fields.item(),可於迴圈內設個變數來累加,作為列偏移
06/09 16:00, 6F

06/09 16:00, 7年前 , 7F
06/09 16:00, 7F

06/09 16:03, 7年前 , 8F
原來我自己誤會 wscrip用法 謝謝~ 在研究看看!
06/09 16:03, 8F
文章代碼(AID): #1R6u5mqX (Office)
討論串 (同標題文章)
文章代碼(AID): #1R6u5mqX (Office)