在MS Access中使用多个DAO记录集

时间:2012-09-04 15:44:07

标签: vba dao

第一篇文章尽可能温柔! :)

我正在Access中创建一个新数据库来更新我们的电子商务软件(也基于Access)。

我们从供应商处收到3个Feed,所有CSV格式的格式略有不同。我已经使用链接表成功导入了Feed,并且可以(我认为)以编程方式从文件中刷新数据。

我创建了一个'CurrentProducts'表,其中包含了我们网站上现场的所有产品。

我想依次从CurrentProducts表中获取每个产品代码,在每个供应商Feed中查找,根据我们的购买价计算我们的销售价格,确定哪个供应商提供最便宜的价格然后更新CurrentProducts相应的表格。

我之前在Excel中经常使用VBA用于宏目的,但我从来没有真正触及Access中的DAO Recordsets,所以我承认我真的不知道我在做什么!

到目前为止我已经得到了代码。 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

2 个答案:

答案 0 :(得分:0)

考虑这个查询,只需将其剪切并粘贴到查询设计窗口的SQL视图中即可。

SELECT c.productcode,
       c.price,
       s.supfile,
       s.calcprice
FROM   currentproducts c
       LEFT JOIN (SELECT "varlink"        AS supfile,
                         productcode,
                         ( price * 1.25 ) AS CalcPrice
                  FROM   varlink
                  UNION ALL
                  SELECT "ingram"         AS supfile,
                         productcode,
                         ( price * 1.25 ) AS CalcPrice
                  FROM   ingram
                  UNION ALL
                  SELECT "scansource"     AS supfile,
                         productcode,
                         ( price * 1.25 ) AS CalcPrice
                  FROM   scansource) AS s
              ON c.productcode = s.productcode 

如果查询显示您感兴趣的数据,则只需从union表中获取每个产品代码的最小值。这在SQL中并不太难。

例如,您可以将联合查询保存为查询,然后操纵数据。

SELECT u.supfile,
       u.productcode,
       u.calcprice
FROM   MyUnion u
WHERE  (( ( u.calcprice ) IN (SELECT TOP 1 calcprice
                                 FROM   MyUnion b
                                 WHERE  b.productcode = u.productcode
                                 ORDER  BY calcprice) )); 

此查询也可以作为派生表包含,与上面的查询相同,因此您可以将各种低价与当前价格进行比较,但是,当两个供应商具有相同价格时,此查询将给出重复项

决策过程可能不如更新价格最低的那么简单,但是,如果没有别的,使用sql和vba将大大减少处理时间和记录集的数量。

答案 1 :(得分:0)

我最终使用临时表(temp_prod)并将所有3个供应商的所有产品转储到表中。然后我使用了这个查询:

SELECT a.productcode, a.buyprice, a.listprice, a.source FROM temp_prod AS a WHERE a.buyprice in (Select top 1 buyprice from temp_prod b where a.productcode = b.productcode order by buyprice)

打开包含每个产品代码最便宜价格的记录集。然后我循环记录集,使用新的销售价格(从buyprice计算)更新产品导出表并瞧。非常感谢你帮助Remou!