|
本帖最后由 Yolanda_L 于 2012-7-17 13:05 编辑
用VBA编写代码通过Excel来实现SQL数据库里的数据表的更新等操作时,因为有些表的主键是自增型,不需要在excel里维护,但有的表的主键用户是可以自己增加和修改值的。打算用一个标准的过程来实现几个基础表的维护,但因为主键的问题,在判断记录是否存在等方面遇到了麻烦,请问大侠如何解决这一问题。谢谢[code=vb]For i = 1 To iRowCount - 1
myStr = myArray(i, 0)
SQL = "SELECT * FROM " & mytable & " WHERE " & myArray(0, 0) & "=" & myStr '问题就出在这里
Set rs = cnn.Execute(SQL)
If rs.EOF And rs.BOF Then '<if the keywords ID does not exist,, then execute the insert sql>
For j = 1 To iColCount - 1
myStr = myStr & "','" & myArray(i, j)
Next j
SQL = "Insert into " & mytable & myColumn & " Values('" & myStr & "')"
Set rs = cnn.Execute(SQL)
myStr = ""
Else '<if the keywords ID does not exist, then execute the update sql>
myStr1 = myArray(0, 1) & "='" & myArray(i, 1) & "'"
myStr2 = myArray(0, 0) & "='" & myArray(i, 0) & "'"
For j = 2 To iColCount - 1
myStr1 = myStr1 & "," & myArray(0, j) & "='" & myArray(i, j) & "'"
Next j
SQL = "UPDATE " & mytable & " SET " & myStr1 & " where " & myStr2
Set rs = cnn.Execute(SQL)
myStr = ""
myStr1 = ""
End If
Next i[/code]
|
|