第一篇文章,所以尽可能温柔!:)
我正在 Access 中创建一个新数据库来更新我们的电子商务软件(也基于 Access)。
我们收到来自供应商的 3 个提要,所有 CSV 文件的格式略有不同。我已经使用链接表成功导入了提要,并且可以(我认为)根据需要以编程方式刷新文件中的数据。
我创建了一个“CurrentProducts”表,其中包括目前在我们网站上的所有产品。
我想依次从 CurrentProducts 表中获取每个产品代码,在每个供应商提要中查找它,根据我们的购买价格计算我们的售价,找出哪个供应商提供最便宜的价格,然后相应地更新 CurrentProducts 表。
我以前经常在 Excel 中将 VBA 用于宏目的,但我从未真正接触过 Access 中的 DAO 记录集,所以我承认我真的不知道自己在做什么!
到目前为止,我已经得到了下面的代码。CurrentProducts 表中有大约 17,900 条记录,“Ingram”表中有近 51,000 条记录,ScanSource 表中有近 15,000 条记录,Varlink 表中有大约 3,000 条记录。
我已经让代码运行了 5 到 10 分钟,虽然代码看起来确实在工作,但它的运行速度非常慢。我只能假设必须有一种比我目前正在做的更快/更简单的方法来访问记录集中的数据。
所以交给你们,我应该放弃所有这些并重新开始还是可以从这里调整?
谢谢你。
Private Sub Command0_Click()
Dim var As DAO.Recordset
Dim ing As DAO.Recordset
Dim scan As DAO.Recordset
Dim curr As DAO.Recordset
Dim filtvar As DAO.Recordset
Dim filtscan As DAO.Recordset
Dim filting As DAO.Recordset
Dim db As Database
Dim varSQL As String, ingSQL As String, scanSQL As String, currSQL As String
Dim prodcode As String
Dim varPrice As Double, ingPrice As Double, scanPrice As Double, currPrice As Double
DoCmd.Hourglass True
Set db = CurrentDb
currSQL = "select ProductCode, Price from CurrentProducts"
varSQL = "select ProductCode, (Price*1.25) as CalcPrice from Varlink"
ingSQL = "select ProductCode, (Price*1.25) as CalcPrice from Ingram"
scanSQL = "select ProductCode, (Price*1.25) as CalcPrice from ScanSource"
Set curr = db.OpenRecordset(currSQL)
Set var = db.OpenRecordset(varSQL)
Set ing = db.OpenRecordset(ingSQL)
Set scan = db.OpenRecordset(scanSQL)
curr.MoveLast 'Needed to get the accurate number of records
'Show the progress bar
SysCmd acSysCmdInitMeter, "Working...", curr.RecordCount
curr.MoveFirst
Do While Not curr.EOF
prodcode = curr!ProductCode
var.Filter = "[ProductCode] = " & "'" & prodcode & "'"
Set filtvar = var.OpenRecordset
ing.Filter = "[ProductCode] = " & "'" & prodcode & "'"
Set filting = ing.OpenRecordset
scan.Filter = "[ProductCode] = " & "'" & prodcode & "'"
Set filtscan = scan.OpenRecordset
usevarprice = 0
useingprice = 0
usescanprice = 0
If filtvar.EOF And filtvar.BOF Then
Else
varPrice = filtvar!CalcPrice
varPrice = Round(varPrice, 0)
usevarprice = 1
End If
If filting.EOF And filting.BOF Then
Else
ingPrice = filting!CalcPrice
ingPrice = Round(ingPrice, 0)
useingprice = 1
End If
If filtscan.EOF And filtscan.BOF Then
Else
scanPrice = filtscan!CalcPrice
scanPrice = Round(scanPrice, 0)
usescanprice = 1
End If
If usevarprice = 1 And useingprice = 1 And usescanprice = 1 Then
If varPrice < ingPrice And varPrice < scanPrice Then
newPrice = varPrice
ElseIf ingPrice < varPrice And ingPrice < scanPrice Then
newPrice = ingPrice
Else
newPrice = scanPrice
End If
ElseIf usevarprice = 1 And useingprice = 1 And usescanprice = 0 Then
If varPrice < ingPrice Then
newPrice = varPrice
Else
newPrice = ingPrice
End If
ElseIf usevarprice = 1 And useingprice = 0 And usescanprice = 1 Then
If varPrice < scanPrice Then
newPrice = varPrice
Else
newPrice = scanPrice
End If
ElseIf usevarprice = 0 And useingprice = 1 And usescanprice = 1 Then
If scanPrice < ingPrice Then
newPrice = scanPrice
Else
newPrice = ingPrice
End If
Else
If usevarprice = 1 Then
newPrice = varPrice
ElseIf useingprice = 1 Then
newPrice = ingPrice
ElseIf usescanprice = 1 Then
newPrice = scanPrice
End If
End If
curr.Edit
curr!Price = newPrice
curr.Update
curr.MoveNext
n = n + 1
'Update the progress bar
SysCmd acSysCmdUpdateMeter, n
'Keep the application responding (optional)
DoEvents
Loop
curr.Close: Set curr = Nothing
var.Close: Set var = Nothing
ing.Close: Set ing = Nothing
scann.Close: Set scan = Nothing
'Remove the progress bar
SysCmd acSysCmdRemoveMeter
'Show the normal cursor again
DoCmd.Hourglass False
End Sub