简体   繁体   English

通过循环记录集访问 vba 数组

[英]access vba array by looping through recordset

I have a loop which I want to create an array then use the array to insert into another table.我有一个循环,我想创建一个数组,然后使用该数组插入到另一个表中。

Band     Country 
AIR       FR 
Bon Jovi  US 
Oasis     UK 
Blur      UK 
Green Day US 
Metalica  US

I want to loop through this recordset, so I want to create arrays, for example, arrayFR = "AIR";我想遍历这个记录集,所以我想创建数组,例如,arrayFR = "AIR"; arrayUK = "Blur vbCrLf Oasis" and arrayUS = "Bon Jovi vbCrLf Green Day vbCrLf Metalica". arrayUK = "Blur vbCrLf Oasis" 和 arrayUS = "Bon Jovi vbCrLf Green Day vbCrLf Metalica"。

At the same time, based on this recordset, I have created a temp table with columns FR, UK & US.同时,基于此记录集,我创建了一个包含 FR、UK 和 US 列的临时表。 I hope to use the arrays created, then insert into the temp table like the view below.我希望使用创建的数组,然后像下面的视图一样插入到临时表中。

FR        UK        US 
AIR       Blur      Bon Jovi 
          Oasis     Green Day 
                    Metalica

I don't know how to start with as I have searched a lot of arrays related pages but doesn't help, please help me gurus!我不知道如何开始,因为我已经搜索了很多与数组相关的页面但没有帮助,请帮助我大师! Thanks in advance!提前致谢!

Since your end result (per country) is a single field with bands delimited by CRLF, you can make it simple.由于您的最终结果(每个国家/地区)是由 CRLF 分隔带的单个字段,因此您可以将其简化。 Add the following Dims;添加以下 Dims;

Dim aArray(10, 2) As String
Dim iC      As Integer
Dim i       As Integer
IC = 0

Then add this inside your loop (change field names as required):然后将其添加到您的循环中(根据需要更改字段名称):

Debug.Print rs1!country & vbTab & rs1!band
If aArray(iC, 1) <> rs1!country Then
    iC = iC + 1
    If iC > 10 Then MsgBox "There are more than 10 countries! Change the code...", vbOKOnly, "Too many countries"
    aArray(iC, 1) = rs1!country
    aArray(iC, 2) = rs1!band
Else
    aArray(iC, 2) = aArray(iC, 2) & vbCrLf & rs1!band
End If

To see results:查看结果:

For i = 1 To iC
    MsgBox aArray(i, 1) & vbCrLf & aArray(i, 2)
Next i

Why not creating your temp table by using a simple CrossTab query ?为什么不使用简单的 CrossTab 查询创建临时表? If that suits your needs, it will be much easier.如果这适合您的需求,那会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM