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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what causes records to repeat endlessly?

Status
Not open for further replies.

humvie

Technical User
Aug 14, 2002
61
CA
I've written some code which basically takes information from one table and then compares it against two other tables and then CREATES a new table from the result.

I used the INNER JOIN .... ON ....
Then to compare I used INSERT ......

The codes works fine until I add the WHERE .....

Now without copying the code (it is fairly long-winded) would anyone know why records would be repeated endlessly to the point ACCESS errors out by maxing out at .99MB ?

Has anyone run into this problem before and what was done to rectify the problem?

thanks
 
I would suggest that your code is obviously not breaking out of a processing loop. Without seeing the code, its impossible to help further. Why not post the code into this thread, so someone can check it out.
Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
OK, here you go. What I'm trying to achieve is to give the user the obtion to select the carrier (cboTruck). Having a difficult time placing it in the code. Disregard some of the comments - I use them to keep track of my progress.
See text in red. Maybe it doesnt even belong there !!!

strSQL1 = "SELECT tblRatesHeader.PrimaryCarrier, tblRatesHeader.CarrierCode, " & _
"tblRatesHeader.CarrierName, tblRatesHeader.Item, tblRatesHeader.Quote, " & _
"tblRatesHeader.Origin, tblRatesHeader.Destination, tblRatesHeader.ServType, " & _
"tblRatesHeader.ServLevel, tblRateDetail.Currency, tblRateDetail.Rate, tblRateDetail.RateType, " & _
"tblRateDetail.HidWgt, tblRateDetail.Weight, tblRateDetail.WgtBrk, " & _
"tblRatesHeader.Factor, tblRateDetail.Qty, " & _
"IIf([QTY]='MC' Or [RateType] = 'Flat' Or CINT([Qty]) Between 1 AND 35, " & _
&quot;[RATE], IIf(&quot; & Weight_in_lbs & &quot;<=[Weight],[Weight]/100*[Rate],&quot; & Weight_in_lbs & &quot;/100*[Rate])) AS Net &quot; & _
&quot;FROM tblRatesHeader INNER JOIN tblRateDetail &quot; & _
&quot;ON tblRatesHeader.Quote = tblRateDetail.Quote WHERE tblRatesHeader.CarrierName = '&quot; & Me!cboTruck & &quot;'&quot;

' adding the AND tblRatesHeader.CarrierName = 'DAILY MOTOR FREIGHT' causes the system to create duplicates
' until Access reaches .99MB or full capacity

strSQL2 = &quot;SELECT Freight.Carrier, Freight.[Province Destination], &quot; & _
&quot;Freight.[Origin Zip Code/PC], Freight.[Weight in lbs], &quot; & _
&quot;Freight.Total, Freight.TTLApproved, Freight.Currency, &quot; & _
&quot;Freight.BegDate, Freight.Skids, Freight.[Invoice Number], &quot; & _
&quot;Freight.FSC, Freight.OtherCharges, Freight.Mode, True AS Sel, Freight.TTLApproved &quot; & _
&quot;FROM Freight WHERE 1 = 1&quot;

If Not (Me!cboCarrierHIST.Value = vbNullString) Then
strSQL2a = strSQL2a & &quot; AND Carrier = '&quot; & Me!cboCarrierHIST.Value & &quot;'&quot;
End If

If Not (Me!cboDestHIST.Value = vbNullString) Then
strSQL2a = strSQL2a & &quot; AND [Province Destination] = '&quot; & Me!cboDestHIST.Value & &quot;'&quot;
End If

If Not (Me!cboOriginHIST.Value = vbNullString) Then
strSQL2a = strSQL2a & &quot; AND [Origin Zip Code/PC] = '&quot; & Me!cboOriginHIST.Value & &quot;'&quot;
End If

If Not (Me!cboTerrHIST.Value = vbNullString) Then
strSQL2a = strSQL2a & &quot; AND Territory = '&quot; & Me!cboTerrHIST.Value & &quot;'&quot;
End If

If (Not (IsNull(Me!txtWgtHIST))) And (Not (IsNull(Me!txtWgtHIST2))) Then
strSQL2a = strSQL2a & &quot; AND [Weight in lbs] BETWEEN &quot; & Me!txtWgtHIST & &quot; AND &quot; & Me!txtWgtHIST2 & &quot;&quot;
End If

If (Not IsNull(Me!Text133.Value)) And (Not IsNull(Me!Text135.Value)) Then
strSQL2a = strSQL2a & &quot; AND BegDate BETWEEN #&quot; & Me!Text133.Value & &quot;# AND #&quot; & Me!Text135.Value & &quot;#&quot;
End If


strSQL2 = strSQL2 & strSQL2a

strSQL3 = &quot;DELETE * FROM FreightCost_Temp&quot; ' Used for clearing the temp table

strSQL4 = &quot;INSERT INTO FreightCost_Temp &quot; & _
&quot;( Carrier, [Province Destination], [Origin Zip Code/PC], &quot; & _
&quot; [Weight in lbs], Total, TTLApproved, [Currency], BegDate, &quot; & _
&quot; Skids, [Invoice Number], FSC, OtherCharges, Mode, Selected, Net) &quot; & _
&quot;SELECT Freight.Carrier, Freight.[Province Destination], &quot; & _
&quot;Freight.[Origin Zip Code/PC], Freight.[Weight in lbs], &quot; & _
&quot;Freight.Total, Freight.TTLApproved, Freight.Currency, &quot; & _
&quot;Freight.BegDate, Freight.Skids, Freight.[Invoice Number], &quot; & _
&quot;Freight.FSC, Freight.OtherCharges, Freight.Mode, True AS Sel, Freight.TTLApproved &quot; & _
&quot;FROM Freight WHERE 1 = 1&quot;

strSQL4 = strSQL4 & strSQL2a

' Used for populating the temp table and anything changed here must also be
'be changed in strSQL2
' Clear the temp table


DoCmd.SetWarnings False
DoCmd.RunSQL strSQL3
DoCmd.SetWarnings True

' Get the existing data and push it into the temp table
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL4
DoCmd.SetWarnings True

' Process the comparison data
' We need to do this one record at a time.
Set db = CurrentDb()
Set rs2 = db.OpenRecordset(strSQL2)

With rs2 ' The Freight Table
While Not .EOF ' Cycle through all the records
' Build the filter
strFilter = &quot;(&quot;
' Origin
If Not IsNull(![Origin Zip Code/PC]) Then
strFilter = strFilter & &quot; Origin = '&quot; & ![Origin Zip Code/PC] & &quot;' and&quot;

End If

' Destination
If Not IsNull(![Province Destination]) Then
strFilter = strFilter & &quot; Destination = '&quot; & ![Province Destination] & &quot;' and&quot;

End If

' Qty
If Not IsNull(!Skids) Then
strFilter = strFilter & &quot; Qty = '&quot; & !Skids & &quot;') or (&quot;

End If

'Origin
If Not IsNull(![Origin Zip Code/PC]) Then
strFilter = strFilter & &quot; Origin = '&quot; & ![Origin Zip Code/PC] & &quot;' and&quot;

End If

' Destination
If Not IsNull(![Province Destination]) Then
strFilter = strFilter & &quot; Destination = '&quot; & ![Province Destination] & &quot;' and&quot;

End If

On Error Resume Next
' Weight
If Not IsNull(![Weight in lbs]) Then
strFilter = strFilter & &quot; HidWgt <= &quot; & ![Weight in lbs] & &quot; and WgtBrk >= &quot; & ![Weight in lbs] & &quot;)&quot;
'MsgBox ![Weight In Lbs]
Weight_in_lbs = ![Weight in lbs]

Else
Weight_in_lbs = 0

End If


' Trim the string
If Right(strFilter, 3) = &quot;and&quot; Then
strFilter = Trim(Left(strFilter, Len(strFilter) - 3))

End If

' Set the filter
If Len(strFilter) > 0 Then
strFilter = &quot; WHERE &quot; & strFilter
strSQL5 = strSQL1 & strFilter
End If

' Run the query

Set rs1 = db.OpenRecordset(strSQL5)


While Not rs1.EOF

Option1 = False

If rs1!Qty = &quot;MC&quot; Or rs1!RateType = &quot;FLAT&quot; Or rs1!RateType = &quot;SKID&quot; Or rs1!Qty = &quot;1&quot; Or rs1!Qty = &quot;2&quot; Or rs1!Qty = &quot;3&quot; Or rs1!Qty = &quot;4&quot; Or rs1!Qty = &quot;5&quot; Or rs1!Qty = &quot;6&quot; Or rs1!Qty = &quot;7&quot; Or rs1!Qty = &quot;8&quot; Or rs1!Qty = &quot;9&quot; Or rs1!Qty = &quot;10&quot; Or rs1!Qty = &quot;11&quot; Or rs1!Qty = &quot;12&quot; Or rs1!Qty = &quot;13&quot; Or rs1!Qty = &quot;14&quot; Or rs1!Qty = &quot;15&quot; Or rs1!Qty = &quot;16&quot; Or rs1!Qty = &quot;17&quot; Or rs1!Qty = &quot;18&quot; Or rs1!Qty = &quot;19&quot; Or rs1!Qty = &quot;20&quot; Or rs1!Qty = &quot;21&quot; Or rs1!Qty = &quot;22&quot; Or rs1!Qty = &quot;23&quot; Or rs1!Qty = &quot;24&quot; Or rs1!Qty = &quot;25&quot; Or rs1!Qty = &quot;26&quot; Or rs1!Qty = &quot;27&quot; Or rs1!Qty = &quot;28&quot; Or rs1!Qty = &quot;29&quot; Or rs1!Qty = &quot;30&quot; Or rs1!Qty = &quot;31&quot; Or rs1!Qty = &quot;32&quot; Or rs1!Qty = &quot;33&quot; Or rs1!Qty = &quot;34&quot; Or rs1!Qty = &quot;35&quot; Then Option1 = True
If Option1 Then
Net = rs1!Rate + rs2!FSC + rs2!OtherCharges
Total = Net - rs2!FSC - rs2!OtherCharges
End If


If rs1!RateType = &quot;CWT&quot; Then
If [Weight_in_lbs] <= rs1!Weight Then
Net = (rs1!Weight / 100 * rs1!Rate) + rs2!FSC + rs2!OtherCharges
Total = Net - rs2!FSC - rs2!OtherCharges
Else

Net = ([Weight_in_lbs] / 100 * rs1!Rate) + rs2!FSC + rs2!OtherCharges
Total = Net - rs2!FSC - rs2!OtherCharges
End If


End If
'stop

strSQL4 = &quot;INSERT INTO FreightCost_Temp &quot; & _
&quot;( [Invoice Number], Carrier, [Province Destination], [Origin Zip Code/PC], &quot; & _
&quot; [Weight in lbs], [Currency], Skids, Net, Total, OtherCharges, FSC) &quot; & _
&quot;VALUES ( '&quot; & rs2![Invoice Number] & &quot;', ' &quot; & rs1!CarrierName & &quot;', '&quot; & rs1!Destination & &quot;', '&quot; & _
&quot;&quot; & rs1!Origin & &quot;', &quot; & rs1!Weight & &quot;, '&quot; & _
&quot;&quot; & rs1!Currency & &quot;', '&quot; & rs2!Skids & &quot;', '&quot; & Net & &quot;','&quot; & Total & &quot;','&quot; & rs2!OtherCharges & &quot;','&quot; & rs2!FSC & &quot;')&quot;


DoCmd.SetWarnings False
DoCmd.RunSQL strSQL4
DoCmd.SetWarnings True

rs1.MoveNext

Wend
strSQL5 = &quot;&quot;
strFilter = &quot;&quot;
.MoveNext

Wend

End With
 
Humvie,

I've had a quick look at the code, and nothing obvious leaps out that would cause an infinate loop. Possibly whats happening is:

(a) the looping is generating more data than anticipated, probably because of underconstrained queries.

(b) There's no evidence of error handling in the code snippet you provided, but is it possible that you ignore errors, which causes the program to resume on the next line, and as a consequence, never complete - unlikely, but I need to ask this.

At this point, what I would do in your position, is start making extensive use of the debugger, stopping the code at various breakpoints (I note that you do have a commented out Stop statement in your code), and displaying key variable values. One of my favourite debugging aids, is to display an sql based variable in the immediate window, then cut it and past it into an SQL query definition. Then run the query, and make sure that it delivers the rows that you expect.

Before doing the extensive debugging, you should be able to view the contents of the freightcost_temp table to see what data is being generated into it. Preferably, try to view this data in the sequence it is generated. Duplicate (unwanted) entries should then expose themselves, providing you clues as to where to look for the reason in the code.

I'm afraid that from the look of things, you have a little detective work in front of you.

I assume also that the code that you submitted is behind a form, as opposed to being in a module. If its in a module, then the me! prefix will not be recognised, resulting in an error situation, or worse, if error handling is suppressed.

One other point; your one if statement has code to the effect of rs1!Qty = '1' or rs1!Qty = 2 or ... or rs1!Qty = '35'. This for example could be better coded as:

Val(rs!Qty) > =1 and Val(rs!Qty) <= 35

I know I havnt solved your problem, but hopefully I've provided a bit of a strategy for you to follow to solve it,

Good luck,

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
To test:
First I'd get rid of 'On Error Resume Next'
I'd run the code step by step and check each variable, especially until the line:

strSQL5 = strSQL1 & strFilter

Then, before
While Not rs1.EOF

I'd check the recordcount:

rs1.movelast
rs1.movefirst
Debug.print rs1.RecordCount 'Or MsgBox rs1.RecordCount & &quot; records&quot;


To simplify the code:
I'd change:

If rs1!Qty = &quot;MC&quot; Or rs1!RateType = &quot;FLAT&quot; Or rs1!RateType = &quot;SKID&quot; Or rs1!Qty = &quot;1&quot; Or rs1!Qty = &quot;2&quot; Or rs1!Qty = &quot;3&quot; Or rs1!Qty = &quot;4&quot; Or rs1!Qty = &quot;5&quot; Or rs1!Qty = &quot;6&quot; Or rs1!Qty = &quot;7&quot; Or rs1!Qty = &quot;8&quot; Or rs1!Qty = &quot;9&quot; Or rs1!Qty = &quot;10&quot; Or rs1!Qty = &quot;11&quot; Or rs1!Qty = &quot;12&quot; Or rs1!Qty = &quot;13&quot; Or rs1!Qty = &quot;14&quot; Or rs1!Qty = &quot;15&quot; Or rs1!Qty = &quot;16&quot; Or rs1!Qty = &quot;17&quot; Or rs1!Qty = &quot;18&quot; Or rs1!Qty = &quot;19&quot; Or rs1!Qty = &quot;20&quot; Or rs1!Qty = &quot;21&quot; Or rs1!Qty = &quot;22&quot; Or rs1!Qty = &quot;23&quot; Or rs1!Qty = &quot;24&quot; Or rs1!Qty = &quot;25&quot; Or rs1!Qty = &quot;26&quot; Or rs1!Qty = &quot;27&quot; Or rs1!Qty = &quot;28&quot; Or rs1!Qty = &quot;29&quot; Or rs1!Qty = &quot;30&quot; Or rs1!Qty = &quot;31&quot; Or rs1!Qty = &quot;32&quot; Or rs1!Qty = &quot;33&quot; Or rs1!Qty = &quot;34&quot; Or rs1!Qty = &quot;35&quot; Then Option1 = True

to:
Select Case rs1!qty
Case &quot;MC&quot;, &quot;1&quot;, &quot;2&quot;.... and so on
Select Case rs1!RateType
Case &quot;FLAT&quot;, &quot;SKID&quot;
Option1 = True
Case Else
MsgBox &quot;RateType = &quot; & rs1!RateType 'an unhandled rate type
End Select
Case Else
MsgBox &quot;qty = &quot; & rs1!qty 'unhandled qty
End select

I'd delete &quot;Where 1 = 1&quot; in the SQL statements, as it is obviously always true and does nothing but unnecessarily complicates things. (I would understand &quot;Where 1=0&quot; as it's great when an empty recordset is needed, but it's not the case here).

And in the end I'd close the recordsets and set them to Nothing...

Just a few thoughts...

Dan
[pipe]
 
thanks guys, but I warned you it was long winded. I appreciate your input.
Thanks
Ray
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top