我正在尝试使用MS Access对我无法控制的ODBC服务器进行Passthrough查询更新。我必须使用Passthrough的原因是我访问的记录有超过255个字段(如果可以,我会使用链接表)。
我一直在使用此资源来使用Passthrough(http://www.techonthenet.com/access/tutorials/passthrough/basics09.php)
来获取数据查询只是:SELECT FullName, PointNumber FROM DNP3.CDNP3AnalogIn
ODBC Connect Str是:ODBC;DSN=SCX6_DB;LOCATION=Main;UID=admin;PWD=password;LOCALTIME=False;
现在在Access数据库中我有一个表(SCADA DB Tags),其字段名称相同(FullName,PointNumber),我想使用Update Passthrough查询更新ODBC数据库中的字段,但我不确定这该怎么做。
我将之前的查询保存为DNP3_CDNP3AnalogIn查询,并尝试创建一个新查询:
UPDATE [DNP3_CDNP3AnalogIn Query] INNER JOIN [SCADA DB Tags] ON
[DNP3_CDNP3AnalogInQuery].FullName = [SCADA DB Tags].FullName
SET [DNP3_CDNP3AnalogIn Query].[PointNumber] = [SCADA DB Tags].[PointNumber];
但是我从Access中收到错误:Operation must use an updateable query.
我知道有一些事情可以做到这一点,但我似乎无法找到一个例子(我可能不会用Google搜索正确的短语)。微软页面(http://technet.microsoft.com/en-us/library/bb188204%28v=sql.90%29.aspx)说:There is, however, one important limitation: the results returned by SQL pass-through queries are always read-only. If you want to enable users to perform updates based on the data retrieved, you must write code to handle this.
不幸的是它没有举例说明这一点!
任何人都可以给我一个解决方案,如果需要我可以使用VBA吗?如果需要,我还可以提供更多背景信息。不幸的是,我不是Access的专家,我只想提出一个可以节省一些时间的自动化解决方案。
答案 0 :(得分:2)
当他们说“如果你想让用户根据从传递查询中检索到的数据执行更新时,你必须编写代码来处理这个问题”他们可能意味着这样的事情:
Option Compare Database
Option Explicit
Public Sub UpdateSqlServer()
Dim cdb As DAO.Database, rst As DAO.Recordset
Dim con As Object ' ADODB.Connection
Dim cmd As Object ' ADODB.Command
Const adParamInput = 1
Const adInteger = 3
Const adVarWChar = 202
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset( _
"SELECT " & _
"[SCADA DB Tags].FullName, " & _
"[SCADA DB Tags].PointNumber " & _
"FROM " & _
"[DNP3_CDNP3AnalogIn Query] " & _
"INNER JOIN " & _
"[SCADA DB Tags] " & _
"ON [DNP3_CDNP3AnalogIn Query].FullName = [SCADA DB Tags].FullName", _
dbOpenSnapshot)
Set con = CreateObject("ADODB.Connection")
con.Open "DSN=SCX6_DB;"
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = con
cmd.CommandText = _
"UPDATE DNP3.CDNP3AnalogIn SET " & _
"PointNumber=? " & _
"WHERE FullName=?"
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput) ' PointNumber
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255) ' FullName
cmd.Prepared = True
Do Until rst.EOF
cmd.Parameters(0).Value = rst!PointNumber
cmd.Parameters(1).Value = rst!FullName
cmd.Execute
rst.MoveNext
Loop
Set cmd = Nothing
con.Close
Set con = Nothing
rst.Close
Set rst = Nothing
Set cdb = Nothing
End Sub
注意:
答案 1 :(得分:1)
您是说[DNP3_CDNP3AnalogIn查询]是基于服务器端的,并且该表[SCADA DB标签]是基于本地的吗?在这种情况下,您无法使用传递查询。
然而,由于表格ARE位于不同位置,因此无法同时触摸两者。
但是,您可以在循环中执行“单个”服务器端(pass-though)。如果您设置了pass-though查询并保存它,那么此代码将起作用:
Dim qdfPass As DAO.QueryDef
Dim rstLocal As DAO.Recordset
Dim strSQL As String
Dim strSQL2 As String
Set qdfPass = CurrentDb.QueryDefs("MyPass")
strSQL = "UPDATE [DNP3_CDNP3AnalogIn Query] " & _
"SET [DNP3_CDNP3AnalogIn Query].[PointNumber] = 'xxxx' " & _
"WHERE [DNP3_CDNP3AnalogInQuery].FullName = 'zzzz' "
Set rstLocal = CurrentDb.OpenRecordset("[SCADA DB Tags]")
Do While rstLocal.EOF = False
strSQL2 = Replace(strSQL, "xxxx", rstLocal!PointNumber)
strSQL2 = Replace(strSQL2, "zzzz", rstLocal!FullName)
qdfPass.SQL = strSQL2
qdfPass.Execute
rstLocal.MoveNext
Loop
rstLocal.Close