Open a report with several IDs

1

I have a Recordset that offers data with between 1 and 10 records.

On the other hand, to call a report that shows on each page the data of its associated table given some Id's:

DoCmd.OpenReport "ordentallerSobre", acViewPreview, , "ID IN (20346, 20347)"

The problem is getting N references in those Id's, but I do not know how I could .... So you could load two references using variables, of course. But what would happen when they were necessary 3,4,5,6 ... up to 10.

Can anyone give a clue?

    
asked by AccOUCH 27.09.2018 в 18:08
source

1 answer

1

Welcome to SO in Spanish. What happens to you right now is very common in programming. You have N references, where N is an indeterminate number, and you would need N variables. Sometimes 2, other times, 8, other times 10 ... etc.

To do this, you need to use matrices , also called arrays, arrays . To simplify the explanation a lot, I will tell you that matrices is a variable that allows you to save a set N of values. But I strongly recommend that you read the following link in Spanish if you want to delve into matrices ( highly recommended ):

  

Matrices in Visual Basic

Well, knowing this, you say you have a Recordset that returns an N number of records (between 1 and 10). I suppose that in that Recordset you will have an ID field. The steps to follow would be:

  • Create the matrix
  • Resize the matrix according to the number of records returned from the Recordset
  • Put all the IDs in the matrix
  • Open report
  • I do not know where your data is, so you'll have to adapt it. Let's suppose that you have a form that returns the Recordset according to what interests you, and from there you click on a button to take that data to a report. Then:

    Dim RST As Recordset
    Set RST = Me.Form.RecordSet.Clone
    
    RST.MoveLast
    RST.MoveFirst
    
    Dim MiMatriz() As Long
    ReDim MiMatriz(RST.RecordCount)
    
    Dim ZZ As Long
    ZZ = 0
    Do Until RST.EOF = True
        MiMatriz(ZZ) = RST.Fields("Id").Value
        ZZ = ZZ + 1
        RST.MoveNext
    Loop
    
    Set RST = Nothing
    
    Dim EstosInformes As String
    
    
    For ZZ = 0 To UBound(MiMatriz) Step 1
        EstosInformes = EstosInformes & MiMatriz(ZZ) & ", "
    Next ZZ
    
    EstosInformes = Left(EstosInformes, Len(EstosInformes) - 2) 'para quitar el último ", "
    
    Erase MiMatriz
    
    DoCmd.OpenReport "ordentallerSobre", acViewPreview, , "[ID] IN (" & EstosInformes & ")"
    

    I hope you can adapt this code to your needs. And really, I strongly recommend you master matrices, because they are very useful.

        
    answered by 28.09.2018 / 11:35
    source