|
标准成本系统.rar
(30.12 KB, 下载次数: 20)
- Sub 添加物料()
- Dim myData As String, myTable As String
- Dim arr As Variant
- Dim cnn As ADODB.Connection
- Dim rs As ADODB.Recordset
- arr = [a1].CurrentRegion
- myData = ThisWorkbook.Path & "\系统.accdb"
- myTable = "物料清单"
- Set cnn = New ADODB.Connection
- With cnn
- .Provider = "microsoft.ACE.oledb.12.0"
- .Open myData
- End With
- For i = 2 To UBound(arr)
- Sql = "select * from " & myTable
- Set rs = New ADODB.Recordset
- rs.Open Sql, cnn, adOpenKeyset, adLockOptimistic
- If rs.RecordCount = 0 Then rs.AddNew
- For j = 1 To rs.Fields.Count
- rs.Fields(j - 1) = arr(i, j - 1)
- Next j
- rs.Update
- Next
- MsgBox "数据保存完毕!", vbInformation + vbOKOnly
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 我的代码提示下标越界,ACCESS不熟,VBA也不熟,只是常用ADO查询做报表,现在想着手弄个系统,都不知道怎么做,好像又没什么比较好的ACCESS方面的书适合我这种菜鸟看。
另外上面的例子,怎么实现添加的时候,先判断需要添加的记录的U8编码在ACCESS数据是否存在,存在的话则整条记录不导入,麻烦大师们给予指点,谢谢。
|
|