1

I'm clearly lacking some knowledge on excel queries.. I need to print out some custom labels with the ordered articles of each worker.

In my example, i have 2 tables:

      [tabWorkers]
      ID        Name
      1         John
      2         Patrick


      [tabOrders]
      ID        Article   Amount
      1         Shoe      2
      1         T-Shirt   5
      2         T-Shirt   3

I'm looping through tabWorker and for each working i would like to query my tabOrders table: SELECT * FROM tabOrders WHERE ID=1

Syntax example of what i want:

    Dim row As Range 
    For Each row In [tabWorkers].Rows 
      myID = row.Columns(row.ListObject.ListColumns("ID").Index).Value

      Write( row.Columns(row.ListObject.ListColumns("Name").Index).Value & " has ordered...<p>" )

      For Each (Article, Amount) In "SELECT * FROM tabOrders WHERE ID ='" & myID & "'")
        Write( Amount & " x " & Article & "<br>" )
      Next

    Next 
4

1 回答 1

0

我之前在大型数据集上使用过这个解决方案,只要选项卡上的数据定义明确,它就可以很好地工作

基本上设置对 ADO 的引用,然后定义到工作表的连接,记住每个工作表名称末尾的 $ :)

Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
Dim strSQL as String

Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name

cn.Open

strSQL = "Select Distinct [Name] From [tabWorkers$] Order by [Name]"

Set rs = New ADODB.Recordset
rs.Open cn, strSQL, adOpenStatic, adLockReadOnly

'// then use the recordset
for each .......

rs.Close
于 2013-01-08T11:07:47.173 回答