|
本帖最后由 ming668 于 2015-8-8 15:16 编辑
参照您的代码,我写了一个更新SQL数据库的代码,执行到cn.Execute sqlins时出错:第1行: ‘a’ 附近有语法错误,请帮忙修改下,谢谢!
-
- Sub updateaddRecords2003()
- ''引用Microsoft ActiveX Data Objects 2.x Library
- Dim cn As New ADODB.Connection
- Dim strCn As String, sqlins As String
- Dim rs As New ADODB.Recordset
- Dim myTable As String
- Dim strTemp As String
- Dim arrFields As Variant
- strCn = "provider=sqloledb;server=192.168.2.8;database=Ttkq;uid=sa;pwd=;"
- cn.Open strCn
- myTable = "kqll"
- On Error GoTo errmsg
- arrFields = Range("A1:G1") '工作表中的字段名写入数组
-
- '生成更新字符串,
- For i = 2 To UBound(arrFields, 2)
- strTemp = strTemp & ",a." & arrFields(1, i) & "=b." & arrFields(1, i)
- Next
-
- '生成更新SQL语句
- sqlins = "update " & myTable & " a,[Excel 8.0;Database=" & ActiveWorkbook.FullName & "].[数据$" _
- & Range("a1").CurrentRegion.Address(0, 0) & "] b set " & Mid(strTemp, 2) & " where a.ID=b.ID"
- cn.Execute sqlins
- Set cn = Nothing
- Exit Sub
- errmsg:
- MsgBox Err.Description, , "错误报告"
- End Sub
-
复制代码
|
|