2013-03-15 48 views
0

我有一個庫存表,其中填充了我們庫存中信息的ID。還有其他表具有這些類別的名稱和ID。 我希望能夠使用for循環來根據我們的庫存填充帶有ID​​的正確庫存數據。如何獲取嵌套for循環的當前值和Access 2010中的直接for循環

我一直希望使用for循環來填充數據到InventoryNow表。 我正在考慮做這樣的事情。

For location.location_ID <= EOF 
{ 
    For years.years_ID <= EOF 
    { 
     insert into inventoryNow (wine_ID, years_ID, type_ID,language_ID, Amount) 
     values (2,currentvalue(years.years_ID), 1,1,currentvalue(locatio.location_ID,2)) 
    } 
} 

Access 2010中是否有這樣的功能?

回答

1

沒有看到你的數據,我想這可能更適合追加查詢,如果你打算做的位置和年表中的所有記錄。

Dim dbs As DAO.Database 
Dim rstLoc as DAO.Recordset 
Dim rstYears as DAO.Recordset 

Set dbs = CurrentDb 
Set rstLoc = dbs.OpenRecordset("SELECT location_ID FROM location") 
Set rstYears = dbs.OpenRecordset("SELECT years_ID FROM years") 

If rstLoc.RecordCount > 0 And rstYears.RecordCount > 0 Then 
    rstLoc.MoveFirst 
    Do Until rstLoc.EOF 
     rstYears.MoveFirst 
     Do Until rstYears.EOF 
      DoCmd.RunSQL "INSERT INTO InventoryNow (wine_ID, years_ID, type_ID, language_ID, localtion_ID, Amount) VALUES (2, " & rstYears!years_ID & ", 1, 1, " & rstLoc.location_ID & ", 2)" 
      rstYears.MoveNext 
     Loop 
     rstLoc.MoveNext 
    Loop 
End If 
rstLoc.Close 
Set rstLoc = Nothing 

rstYears.Close 
Set rstYears = Nothing 

dbs.Close 
Set dbs = Nothing 
0

您可以使用SQL與MS Access和採取Cross join的優勢創造:但它可以在代碼通過循環周圍2點的記錄類似於你所描述的東西,使用類似下面的代碼來完成項目每年和位置:

SELECT wines.wine_ID, wines.type_ID, 
    wines.language_ID, wines.Amount, 
    years.years_ID, location.location_id 
INTO AllWines 
FROM wines, years, location 

例如,2種葡萄酒(2),2年(2010年和2011年)和2個地點(1和2),你會得到

wine_ID type_ID language_ID Amount years_ID location_id 
1  1  1   10  2010  1 
2  2  2   20  2010  1 
1  1  1   10  2011  1 
2  2  2   20  2011  1 
1  1  1   10  2010  2 
2  2  2   20  2010  2 
1  1  1   10  2011  2 
2  2  2   20  2011  2