Results 1 to 8 of 8

20041022, 15:54 #1
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 30 Times in 30 Posts
Re: Digit counting function for a field value (2000)
You might use the base 10 logarithm of the number:
Function Log10(X)
Log10 = Log(X) / Log(10#)
End Function
For 1 <= x < 10, this returns 0.*
For 10 <= x < 100, this returns 1.*
For 100 <= x < 1000, this returns 2.*
etc.
So you could use Int(Log10(x)) + 1 to calculate the number of digits.

20041022, 17:55 #2
 Join Date
 Jan 2001
 Location
 Alzano Lombardo, Italy
 Posts
 1,483
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: Digit counting function for a field value (2000)
Thank you Hans, what is the purpose of the # symbol in the expression?

20041022, 18:01 #3
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 30 Times in 30 Posts
Re: Digit counting function for a field value (2000)
# forces the number before it to be interpreted as a Double precision floating point number. See Double in the online help for VBA.

20041022, 18:09 #4
 Join Date
 Jan 2001
 Location
 Alzano Lombardo, Italy
 Posts
 1,483
 Thanks
 0
 Thanked 0 Times in 0 Posts
Digit counting function for a field value (2000)
Is there a function that allows counting the digits of a numerical field value? I work around the problem by using the TOPUP function below but it's limited to when the argument value digits is 3 or 4.
Function EnerOm2(tab1, tab2, gio, orem, lett, lett1)
Dim a, b, c, d, e
d = DLookup("LETTUR", tab1, "Giorno=#" & Format(gio  1, "mm/dd/yyyy") & "#")
e = DLookup("LETTUR1", tab1, "Giorno=#" & Format(gio  1, "mm/dd/yyyy") & "#")
If IsNull(lett) Then
EnerOm2 = 0
Else
If lett < d Then
If MsgBox("Has the counter reset?", vbOKCancel) = vbOK Then
<font color=red>a = lett + topup(d)  d</font color=red>
Else
Exit Function
End If
Else
a = lett  Nz(DLookup("inLetT", tab2, "startdate = #" & Format(gio, "mm/dd/yyyy") & "#"), _
DLookup("LETTUR", tab1, "Giorno=#" & Format(gio  1, "mm/dd/yyyy") & "#"))
End If
If lett1 < e Then
If MsgBox("Has the counter reset?", vbOKCancel) = vbOK Then
<font color=red>c = lett1 + topup(e)  e</font color=red>
Else
Exit Function
End If
Else
c = lett1  Nz(DLookup("inLetA", tab2, "startdate = #" & Format(gio, "mm/dd/yyyy") & "#"), _
DLookup("LETTUR1", tab1, "Giorno=#" & Format(gio  1, "mm/dd/yyyy") & "#"))
End If
b = DFirst("K_CONT", tab2, "startdate <= #" & Format(gio, "mm/dd/yyyy") & "#")
If IsNull([img]/forums/images/smilies/cool.gif[/img] Then
MsgBox "Selected a day for which there's no constant", vbOKOnly, tab2
End If
EnerOm2 = (a + Nz(c, 0)) * b
If EnerOm2 = 0 And orem > 0 Then
MsgBox "Warning! There are work hours with no energy.", vbOKOnly, tab1
End If
End If
End Function
Function topup(reading)
If reading < 1000 Then
topup = 1000
ElseIf reading < 10000 Then
topup = 10000
End If
End Function

20041022, 18:16 #5
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 30 Times in 30 Posts
Re: Digit counting function for a field value (2000)
Basically, because I copied the function straight out of the VBA help file <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
I don't think the # is absolutely necessary here; the Log function will return a Double value anyway.

20041022, 18:28 #6
 Join Date
 Jan 2001
 Location
 Alzano Lombardo, Italy
 Posts
 1,483
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: Digit counting function for a field value (2000)
Thanks again, what is the advantage of having 10 be interpreted as a Double precision floating point number in this case?

20041022, 18:46 #7
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 30 Times in 30 Posts
Re: Digit counting function for a field value (2000)
I think I have already told you a) that I just copied the code from help and [img]/forums/images/smilies/cool.gif[/img] that I don't think that the # is essential. There are situations, however, in which you must coerce a constant into a certain data type to get correct results. Here is a very simple example
Dim x As Long
x = 32000 + 32000
will cause an overflow error, since 32000 is seen as a (short) integer, and 32000 + 32000 is above the maximum value 32767 for a short integer. You can force the constants to be seen as long integers by adding &:
x = 32000& + 32000&
or even
x = 32000& + 32000
% = short integer
& = long integer
! = single
# = double
@ = currency

20041022, 19:55 #8
 Join Date
 Jan 2001
 Location
 Alzano Lombardo, Italy
 Posts
 1,483
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: Digit counting function for a field value (2000)
Sorry Hans, <img src=/S/blush.gif border=0 alt=blush width=15 height=15> I deleted the message your 419595 was a reply to because in it I was asking you how come you hadn't followed X with # as well in the expression but that's a variable so I realized it wasn't feasible and then I posted my 419603 because your 419595 was yet to be displayed. <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>