adding numbers in order quickly

  • Thread starter Thread starter macleod409
  • Start date Start date
M

macleod409

Guest
I have to add from 1 - 1999 faster then typing each number in a field. I know how to do it in excel, cant remeber how to do it in access.
 
If I understand your question, this function will do that...

Public Function CountIt()
Dim i, c As Long

c = 0

For i = 1 To 1999
c = c + i
Debug.Print c
Next i

End Function
 
Or assuming that the numbers are consecutive and integers


Dim intStartNumber, intEndNumber, intLastNumber, intResult as integer

intStartNumber = some input say 1

intEndNumber = some input say 1999

intLastNumber = Right("intEndNumber",1)

if intLastNumber Mod 2 = 1 then ' checks last digit odd or even

' odd
intResult = ( (intStartNumber + intEndNumber) * ( ( intEndNumber -1 )/2 ) ) + ( ( intEndNumber + 1 )/2 )

Else
' even
intResult = (intStartNumber + intEndNumber) * (intEndNumber/2)

End if


This will only give the total but would be much quicker than running a loop.
 
jgc31 said:
Or assuming that the numbers are consecutive and integers

Dim intStartNumber, intEndNumber, intLastNumber, intResult as integer

You have dimensioned three Variants and one Integer here. ;)

Code:
Dim intStartNumber As Integer, intEndNumber As Integer
Dim intLastNumber As Integer, intResult As Integer
 
macleod409 said:
I have to add from 1 - 1999 faster then typing each number in a field. I know how to do it in excel, cant remeber how to do it in access.

But why? :confused: You are not using Excel but it sounds as if you are wanting to use Access as if it is Excel - a mistake many people make.
 

Users who are viewing this thread

Back
Top Bottom