|
你access表欄位都是簡體字, 我不好寫代碼, 只能在這說明
方法是, 先用查出access[數據]表中的[學生編號]最大值, 之後加1
現之後pass給變數mxVal, 關閉rs, 接下來mxVal就是表中最大值
- Option Base 1
- Sub 蠶講奻換()
- Dim cn As New ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim MxKey As Long '最大鍵值
- Dim mp As String
- Dim sql As String
- Dim 杅擂, SJ As String
- Dim M, N, I, J As Double
- mp = ThisWorkbook.Path & "\aa.accdb"
- cn.Open "provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & mp
- Set rs = New ADODB.Recordset
- rs.Open "SELECT MAX([學生編號]) AS mxVal FROM [數據]", cn, adOpenKeyset, adLockOptimistic
- MxKey = rs.Fields("mxVal")
- '至此已抓到最大鍵值, 如有新值INSERT 其ID就是 MxKey + 1
- Set rs = Nothing
- AB = Selection.Resize(, 10)
- M = UBound(AB)
- N = UBound(AB, 2)
- For I = 1 To M
- SJ = "'" & AB(I, 1) & "'"
- For J = 2 To N
- sjn = TypeName(AB(I, J))
- Select Case sjn
- Case Is = "String"
- SJ = SJ & ",'" & AB(I, J) & "'"
- Case Is = "Date"
- SJ = SJ & ",#" & AB(I, J) & "#"
- Case Is = "Double"
- SJ = SJ & "," & AB(I, J)
- Case Is = "Empty"
- SJ = SJ & "," & "null"
- End Select
- Next
- 杅擂 = SJ & ",#" & Time & "#"
- sql = "insert into 杅擂 VALUES(" & 杅擂 & ")"
- cn.Execute (sql)
- Next
- cn.Close
- Set cn = Nothing
- MsgBox "OK"
- End Sub
复制代码
|
|