code count line until values changes

2
You would be best off if you used a function. In your query, create a  column with something like  LineCount : GetLineCount([ida], [idb], .8)  I'm passing .8. If this number changes, you can change the value in the  query and the function will calc correctly.  Then in the modules create a function called GetCount(). Aircode is:  Public FUnction GetCount(strIDA As string, strIDA2 As String, _  dblDec As Double) As Integer  Dim strSQL As STring  Dim rst As Recordset  Dim dblVal As Double  'select records for the ids  strSQL = "Select number from ... " & _  "Where ida = strIDA and idb = strIDB"  "order by number decending"  set rst = currentdb.openrecordset(strSQL...)  rst.movefirst  Do while not rst.EOF  'add the number from the select  dblVal = dblVal + Number  'increment line count  GetCount = GetCount + 1  'if greater/equal, .8 line count reached  If dblVal >= dblDec Then  exit do  endif  rst.MoveNext  loop  End Function xxxxxxxxxxxxxxxxxxxxxxxxxx tblContractItems  ID AutoNumber PK  Contract_Number Text  Box Text  1 1111111 Box 1 of 3  2 1111111 Box 2 of 3  3 1111111 Box 3 of 3  4 1111111 Skid 1 of 1  5 2222222 Box 1 of 2  6 2222222 Box 2 of 2  7 2222222 Skid 1 of 1  qryGetNumberToAssign  SELECT Box, Contract_Number, (SELECT Count(A.ID) FROM tblContractItems  AS A WHERE A.Contract_Number = tblContractItems.Contract_Number And  A.ID < tblContractItems.ID) + 1 AS NumberToAssign FROM  tblContractItems; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Increment a field by 1, based on value of previous record. --------------------------------------------------------------------------------

Upload: bengalsac

Post on 06-Jul-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Code Count Line Until Values Changes

8/17/2019 Code Count Line Until Values Changes

http://slidepdf.com/reader/full/code-count-line-until-values-changes 1/2

You would be best off if you used a function. In your query, create a column with something like LineCount : GetLineCount([ida], [idb], .8)

 I'm passing .8. If this number changes, you can change the value in the query and the function will calc correctly.

 Then in the modules create a function called GetCount(). Aircode is: Public FUnction GetCount(strIDA As string, strIDA2 As String, _  dblDec As Double) As Integer Dim strSQL As STring Dim rst As Recordset Dim dblVal As Double

 'select records for the ids strSQL = "Select number from ... " & _  "Where ida = strIDA and idb = strIDB" "order by number decending"

 set rst = currentdb.openrecordset(strSQL...) rst.movefirst Do while not rst.EOF 'add the number from the select dblVal = dblVal + Number

 'increment line count GetCount = GetCount + 1 'if greater/equal, .8 line count reached If dblVal >= dblDec Then exit do endif rst.MoveNext loop End Function

xxxxxxxxxxxxxxxxxxxxxxxxxx

tblContractItems ID AutoNumber PK Contract_Number Text Box Text

 1 1111111 Box 1 of 3 2 1111111 Box 2 of 3 3 1111111 Box 3 of 3 4 1111111 Skid 1 of 1 5 2222222 Box 1 of 2 6 2222222 Box 2 of 2 7 2222222 Skid 1 of 1

 qryGetNumberToAssign SELECT Box, Contract_Number, (SELECT Count(A.ID) FROM tblContractItems AS A WHERE A.Contract_Number = tblContractItems.Contract_Number And A.ID < tblContractItems.ID) + 1 AS NumberToAssign FROM tblContractItems;

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxIncrement a field by 1, based on value of previous record.

--------------------------------------------------------------------------------

Page 2: Code Count Line Until Values Changes

8/17/2019 Code Count Line Until Values Changes

http://slidepdf.com/reader/full/code-count-line-until-values-changes 2/2

I addressed the same thing but put it in the code behind a button. I set up a field named last number. I needed to add a number of records at once so set up a loop. I added 1 to last number each iteration. Not sure if its the same thing aswhat you want to do but here's some code:

Dim Number1 As IntegerDim Number2 As IntegerDim IncNum As Integer

Number1 = 1Number2 = Forms!formname!Txtfield

IncNum = IIF(IsNull(DLookup("MaxofLastNumber", "LastNumLookUpQRY")), 0, DLookup("MaxofLastNumber", "LastNumLookUpQRY")) ' this is important only for the first record.

For i = Number1 To Number2 'sets up the loop and tells it how many times to runbased on data entry in the form text box

Set db = CurrentDbSet rs = db.OpenRecordset("Tablewhereyouwanttochangeinfo")rs.AddNew 'opens a new recordrs("LastNumber") = IncNum + i 'adds the number (ie if it is running the 5th loop

 it adds 5) to the incnum value and places it in the last number field.