简体   繁体   English

Excel 2003 VBA写入SQL Server 2000 ADO

[英]Excel 2003 VBA Write to SQL Server 2000 ADO

We are working on changing an existing excel form to deliver a record to our SLQ server. 我们正在努力更改现有的excel表单,以将记录发送到我们的SLQ服务器。

We have been scuccessful with code that scrubs captures and writes the data to the server using a DSN and ODBC. 我们已经成功完成了使用DSN和ODBC清理捕获数据并将数据写入服务器的代码。

scn = dsn=ODBC123

Our target users (of wich there are many) are using excel 2003 and an ODBC connection on each terminal is not practical. 我们的目标用户(其中​​有很多)正在使用excel 2003,并且每个终端上的ODBC连接都是不实际的。

This is my first venture into using ADODB. 这是我第一次使用ADODB。

I was tripping my badSQL message but do not get an error from the server, until I added the UID and Pass. 在添加UID和通行证之前,我曾绊倒我的badSQL消息,但没有从服务器收到错误消息。

Now I am getting Run-time error '-2147467559(80004005)': Automation error Unspecified Error 现在,我得到运行时错误“ -2147467559(80004005)”:自动化错误未指定错误

Would one of you fine ladies and/or gentlemen take a look and provide some guidence? 你们中的一位女士和/或先生们会不会看一下并提供一些指导?

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim ssql As String

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

Const scn As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
    "Persist Security Info=False;" & _
    "Initial Catalog="DB on Server";" & _
    "Data Source="Server Name" & _ 
    "User ID=Form;Password=nope;"

ssql = "sql statment that writes a record to a table using info in the excel form," & _
" writen in vb that has worked through an odbc"

With cn
    .CursorLocation = adUseClient
    .Open scn
    .CommandTimeout = 0
    Set rst = .Execute(ssql)
End With
    On Error GoTo badsql:

rs.Open ssql, cn, adOpenStatic, adLockOptimistic
On Error GoTo badsql:

rs.Close
cn.Close

Set rs = Nothing
Set cn = Nothing


MsgBox ("This record was updated in the database and documented for" & _
" processing. Please close this workbook.")


badsql:
    MsgBox ("This record was not processed please resubmit.")
    Exit Sub'

We seem to have both rst and rs as Recordset objects. 我们似乎同时将rstrs作为Recordset对象。 I don't see a declaration for rst in the supplied code. 我没有看到一个声明rst在提供的代码。

In any case, the query string in ssql is run twice. 无论如何, ssql的查询字符串将运行两次。 Once when it gets executed and the results (if any) returned into rst . 一旦执行,结果(如果有的话)返回rst The other time when rs is opened. rs打开的另一时间。

You probably only want to run the query string once and, as it seems to be an INSERT -type query, you probably want to use cn.Execute rather than rs.Open 您可能只想运行一次查询字符串,并且由于它似乎是INSERT类型的查询,因此您可能想使用cn.Execute而不是rs.Open

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM