我有在SQL端创建的表和字段。调试器指向cnn.Execute uSQL是问题。我正在尝试将正在访问该工作表的用户和计算机名称写入SQL。使用VBA插入到SQL表中
Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim strText As String
Dim strDate As Date
Dim strUsername As String
Dim strComputerName As String
strUsername = Environ("username")
strComputerName = Environ("Computername")
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=icl-analive; " & _
"Initial Catalog=DW_ALL;" & _
"User ID=dw_all_readonlyuser;" & _
"Trusted_Connection=Yes;"
cnn.Open cnnstr
uSQL = "INSERT INTO Audit (UN,CN) VALUES StrUsername , strComputer"
Debug.Print uSQL
cnn.Execute uSQL
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
的日志记录SQL用户名在你的SQL服务器中是“dw_all_readonlyuser”,所以,在这个用户下你可能没有在数据库中插入特权? – cars10m
FWIW我发现相当震撼的是,你命名'UpdateTable'是一个执行'INSERT'的过程,而不是像读取方法调用所期望的'UPDATE'。 –
在bobby表访问之前,您应该对这类事情使用参数化查询。 http://bobby-tables.com这是一个很好的例子。 http://stackoverflow.com/questions/10352211/vba-ado-connection-and-query-parameters –