Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While Loop?

Status
Not open for further replies.

gabrielbatistuta

Technical User
Jun 21, 2007
20
0
0
US
Hi there,

I have a column called Product in my report. And I would like to add a data that will tell you the number of product changeovers. Could someone please provide me with some sample code.

It is probably a while loop that will go through all instances of product and will increase the counter when the two products being compared are different.

Thanks.
 
How are ya gabrielbatistuta . . .

Perhaps this:
Code:
[blue]Function ChgOvrs() As Long
   Dim rst As DAO.Recordset, OldTxt As String, cnt As Long
   
   Set rst = Me.RecordsetClone
   
   Do Until rst.EOF
      If OldTxt <> rst("[purple][b][i]FieldName[/i][/b][/purple]") Then
         cnt = cnt + 1
         OldTxt = rst("[purple][b][i]FieldName[/i][/b][/purple]")
      End If
         
      rst.MoveNext
   Loop
   
   ChgOvrs = cnt
   
End Function[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Woops! . . .

Corrected code:
Code:
[blue]Function ChgOvrs() As Long
   Dim rst As DAO.Recordset, OldTxt As String, cnt As Long
   
   Set rst = Me.RecordsetClone
   
   Do Until rst.EOF
      If OldTxt <> rst("[purple][b][i]FieldName[/i][/b][/purple]") Then cnt = cnt + 1
      OldTxt = rst("[purple][b][i]FieldName[/i][/b][/purple]")
      rst.MoveNext
   Loop
   
   ChgOvrs = cnt
   
End Function[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Thank you Aceman.

So you would paste this code in the report vba and set a textbox called "product changovers" to = ChgOvrs()
 
gabrielbatistuta said:
[blue]So you would paste this code in the report vba and set a textbox called "product changovers" to = ChgOvrs()[/blue]
[blue]Exactly![/blue] However testing of the code revealed a few more changes. Here's the final code (The code requires [purple]Microsoft DAO 3.6 Object Library[/purple] to run. To [blue]check/install[/blue] the library, in any code window click [blue]Tools[/blue] - [blue]References...[/blue] In the listing find the library and [blue]make sure its checked.[/blue] Then using the up arrow, [purple]push it up as high in priority as it will go[/purple]. Click OK):
Code:
[blue]Function ChgOvrs() As Long
   Dim db As DAO.Database, rst As DAO.Recordset, OldTxt As String, cnt As Long
   
   Set db = CurrentDb
   Set rst = db.OpenRecordset(Me.RecordSource, dbReadOnly)
   
   Do Until rst.EOF
      If OldTxt <> rst("[purple][b][i]FieldName[/i][/b][/purple]") Then cnt = cnt + 1
      OldTxt = Nz(rst("[purple][b][i]FieldName[/i][/b][/purple]"), "")
      rst.MoveNext
   Loop
   
   ChgOvrs = cnt
   
   Set rst = Nothing
   Set db = Nothing
   
End Function[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Are you sure this is correct? Because in the report for Product ChangeOver = #ERROR

field name = Product

 
I'm not 100% sure of what you're doing, but it sounds like all you need is a count of UNIQUE products?

If that's correct, you could try a variation of TheAceMan1's suggestion, using a DISTINCT query.

If you haven't already, save the report's query, let's call it qryMyReport for this example. Presumably, the query is something like 'select Product, field2 ... fieldn from myTable'.

Code:
Function ChgOvrs() As Long
   Dim db As DAO.Database, rst As DAO.Recordset, cnt&, sSQL$
   
   Set db = CurrentDb
   sSQL = "SELECT [b]DISTINCT[/b] Product from qryMyReport"

   Set rst = db.OpenRecordset(Me.RecordSource, dbReadOnly)
      If not rst.EOF then rst.MoveLast: cnt=rst.RecordCount
   Set rst = Nothing
   Set db = Nothing
  
   ChgOvrs = cnt

End Function

HTH

Max Hugen
Australia
 
gabrielbatistuta . . .

Did you change [purple]FieldName[/purple] to the name of the control you want to track?

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
How are ya maxhugen . . .
gabrielbatistuta said:
[blue]I would like to add a data that will tell you the number of [purple]product changeovers[/purple].[/blue]
The [blue]Distinct[/blue] predicate will cover this as long as group changes occur only once. My read is they wont (groups occuring more that once), hence the recordset in my code.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Yes I did. As I mentioned earlier, I am looking for changeover and NOT unique products. How many times does a product change from one to another?

 
Oh, right. In that case I'd look at using the report detail section's OnFormat event:

Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Static cnt&
Static LastProduct$

    if Product <> LastProduct then cnt=cnt+1
    LastProduct=Product
    ProductChangovers=cnt

End Sub

The Static declaration will preserve values between calls to the sub.

Cheers

Max Hugen
Australia
 
Oops, need to check the FormatCount though, as the report may need to loop through formatting more than once...
Code:
    if [b]FormatCount=1[/b] and Product <> LastProduct then cnt=cnt+1


Max Hugen
Australia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top