- 帖子
- 6
- 主題
- 3
- 精華
- 0
- 積分
- 10
- 點名
- 0
- 作業系統
- Windows 7
- 軟體版本
- 1
- 閱讀權限
- 10
- 註冊時間
- 2013-5-2
- 最後登錄
- 2019-11-21

|
[發問] [發問] 有關於SQL不存在則insert存在則update
請問各位大大有關於SQL不存在則insert存在則update的問題
我要將Excel的Table Update到Access資料庫,當資料存在時即Update不存在時即insert
因為我非專業,故上網查詢一般人的寫法,以下三段語法分別(Select, Update, Insert)都能執行,但我該如何整合,才能達到呢?- Private baglan As Object, rs As Object
- Sub baglanti()
- Set baglan = CreateObject("adodb.connection")
- #If VBA7 And Win64 Then
- baglan.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
- #Else
- baglan.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
- #End If
- End Sub
- Sub FP_Database_Acess()
- Sheets("Menu-FP").Visible = True
- Sheets("Menu-FP").Select
- Dim shFC As Worksheet
- 'Dim aArticle As String
- Set shFC = Sheets("Menu-FP")
- EndR = shFC.Range("A65536").End(xlUp).Row
-
- For R = 7 To EndR
- CustomerID = shFC.Cells(R, 1)
- Customer = "'" & shFC.Cells(R, 2) & "'"
- SPECID = shFC.Cells(R, 3)
- aArticle = "'" & shFC.Cells(R, 4) & "'"
- MD = "'" & shFC.Cells(R, 5) & "'"
- Qty = shFC.Cells(R, 6)
- Area = "'" & shFC.Cells(R, 7) & "'"
- Remark = "'" & shFC.Cells(R, 8) & "'"
- PNL = "'" & shFC.Cells(R, 9) & "'"
- Grade = "'" & shFC.Cells(R, 10) & "'"
- Package = "'" & shFC.Cells(R, 11) & "'"
- ICVID = shFC.Cells(R, 12)
- Path = "'" & shFC.Cells(R, 13) & "'"
- File = "'" & shFC.Cells(R, 14) & "'"
-
- Call baglanti
-
- Set rs1 = baglan.Execute("(SELECT CustomerID=" & CustomerID & ",Customer= " & Customer & ", SPECID=" & SPECID & ", MaterialDescription=" & MD & ",Qty=" & Qty & ",Area=" & Area & ",Remark=" & Remark & ", ProductNameLIMS=" & PNL & ",Grade=" & Grade & ", Package=" & Package & ",ICVID=" & ICVID & ",Path=" & Path & ",File=" & File & " FROM [FC] WHERE Article= " & aArticle & ")")
- Set rs2 = baglan.Execute("(UPDATE [FC] SET CustomerID=" & CustomerID & ",Customer= " & Customer & ", SPECID=" & SPECID & ", MaterialDescription=" & MD & ",Qty=" & Qty & ",Area=" & Area & ",Remark=" & Remark & ", ProductNameLIMS=" & PNL & ",Grade=" & Grade & ", Package=" & Package & ",ICVID=" & ICVID & ",Path=" & Path & ",File=" & File & " WHERE Article= " & aArticle & ")")
- Set rs3 = baglan.Execute("INSERT INTO [FC] (CustomerID,Customer,SPECID,Article,MaterialDescription,Qty,Area,Remark,ProductNameLIMS,Grade,Package,ICVID,Path,File) Values (" & CustomerID & "," & Customer & "," & SPECID & "," & aArticle & "," & MD & "," & Qty & "," & Area & "," & Remark & "," & PNL & "," & Grade & "," & Package & "," & ICVID & "," & Path & "," & File & ")")
-
- Set baglan = Nothing: Set rs1 = Nothing: Set rs2 = Nothing: Set rs3 = Nothing:
-
- Next
- MsgBox ("DONE")
- End Sub
複製代碼 |
|