gpt4 book ai didi

vba - 在 VBA 中从 SQL Server 获取数据

转载 作者:行者123 更新时间:2023-12-04 22:30:05 25 4
gpt4 key购买 nike

嗨下面是我的代码,我无法从我的 SQL 服务器获取数据,
它的抛出错误为

Compiler error :  object required.

连接没有问题,连接成功。

请更正我的代码,帮我解决这个问题
Private Sub CommandButton1_Click()
Set SQLConn = CreateObject("ADODB.Connection")

SQLConn.Open "provider =sqloledb; Data Source = xxxx; Initial Catalog = jjjj; User Id = yyyy; Password = zzzz"

MsgBox "Connection Succesful"

Set SQLData = CreateObject("ADODB.Recordset")
With SQLData

' Assign the Connection object.
.ActiveConnection = SQLConn

' Extract the required records.
.Open "select invoice_num, invoice_date, invoice_amount from im_invoice where billing_account = 'HS0076A' and invoice_date ='01-apr-2011'"

' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset SQLData

' Tidy up
.Close

End With

SQLConn.Close
Set SQLData = Nothing
Set SQLConn = Nothing

End Sub

谢谢

谢谢它的工作...... :)

最佳答案

缺少“设置”...

' Assign the Connection object. 
Set .ActiveConnection = SQLConn

关于vba - 在 VBA 中从 SQL Server 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5674187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com