gpt4 book ai didi

excel - 用于从 Excel 更新/创建新记录到 Access 的 VBA 代码

转载 作者:行者123 更新时间:2023-12-01 19:04:37 25 4
gpt4 key购买 nike

我一直在努力到处寻找答案,但我的 VBA 基础技能很低,实际上并不能帮助我弄清楚我要编码的内容。

到目前为止我有这个代码:

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=\\GSS_Model_2.4.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Forecast_T", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
For i = 4 To 16
x = 0
Do While Len(Range("E" & i).Offset(0, x).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
.Fields("Products") = Range("C" & i).Value
.Fields("Mapping") = Range("A1").Value
.Fields("Region") = Range("B2").Value
.Fields("ARPU") = Range("D" & i).Value
.Fields("Quarter_F") = Range("E3").Offset(0, x).Value
.Fields("Year_F") = Range("E2").Offset(0, x).Value
.Fields("Units_F") = Range("E" & i).Offset(0, x).Value
.Update
' stores the new record
End With
x = x + 1
Loop
Next i
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

到目前为止,这段代码正是我想要的。我知道想要添加一个片段,根据 4 个规则检查记录是否存在:Products、Region、Quarter_F 和 Year_F如果与这些匹配,则应更新其他字段(Units_F、ARPU)。如果没有,它应该正确运行代码并创建一个新记录。

非常感谢您的帮助,我被困在这里,不知道如何出去。

谢谢

最佳答案

我有一个 Excel 电子表格,其中包含从单元格 A1 开始的以下数据

product  variety  price
bacon regular 3.79
bacon premium 4.89
bacon deluxe 5.99

我的 Access 数据库中有一个名为“PriceList”的表,其中包含以下数据

product  variety  price
------- ------- -----
bacon premium 4.99
bacon regular 3.99

以下 Excel VBA 将使用“普通”和“高级”的新价格更新现有的 Access 记录,并在表中添加“豪华”的新行:

Public Sub UpdatePriceList()
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Dim sProduct As String, sVariety As String, cPrice As Variant
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=C:\Users\Gord\Desktop\Database1.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "PriceList", cn, adOpenKeyset, adLockOptimistic, adCmdTable

Range("A2").Activate ' row 1 contains column headings
Do While Not IsEmpty(ActiveCell)
sProduct = ActiveCell.Value
sVariety = ActiveCell.Offset(0, 1).Value
cPrice = ActiveCell.Offset(0, 2).Value

rs.Filter = "product='" & sProduct & "' AND variety='" & sVariety & "'"
If rs.EOF Then
Debug.Print "No existing record - adding new..."
rs.Filter = ""
rs.AddNew
rs("product").Value = sProduct
rs("variety").Value = sVariety
Else
Debug.Print "Existing record found..."
End If
rs("price").Value = cPrice
rs.Update
Debug.Print "...record update complete."

ActiveCell.Offset(1, 0).Activate ' next cell down
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

关于excel - 用于从 Excel 更新/创建新记录到 Access 的 VBA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15709156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com