Dim a Field (1 Viewer)

mike60smart

Registered User.
Local time
Today, 15:57
Joined
Aug 6, 2017
Messages
1,905
I have 2 Controls whose datatypes are set as Double in the table and display values like 2.1 and 0.35

When I create an OnClick Event and need to Dim these 2 fields how would I correctly Dim these Controls?

I currently have :-

Dim varSquareMeters As Long
Dim varTotalSquareMeters As Long

When I run the OnClick Event the Immediate Window displays the values as 2 & 0 respectively.

What would be the correct way to Dim these 2 Controls?

Any help appreciated.
 

tvanstiphout

Active member
Local time
Today, 07:57
Joined
Jan 22, 2016
Messages
222
Not "controls", but "variables". They may contain the value of a control, as in:
Dim dblSquareMeters as Double
dblSquareMeters = Me.txtSM
So the correct data type would be Double.
Long is a 32-bit integer, which means no decimal values.
Decimal is available, but should not be used.
 

XPS35

Active member
Local time
Today, 16:57
Joined
Jul 19, 2022
Messages
159
Long does not have decimals. Use Dim XXX as Single.
 

tvanstiphout

Active member
Local time
Today, 07:57
Joined
Jan 22, 2016
Messages
222
@XPS35
Although a Double can often be coerced into a Single, that is not a best practice, with the Double data type being available.
Single storage is 4 bytes, Double is 8 bytes, so it can store larger numbers, and with more decimals.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:57
Joined
Feb 28, 2001
Messages
27,187
What would be the correct way to Dim these 2 Controls?

Controls are not DIM'd unless you are creating a control OBJECT as a variable that isn't actually displayed, but rather is used analogously to the way that a recordset or an application object is used. Controls are created on a form or report in a section thereof. A variable is a VBA and programming concept. A control is a visual concept (with the exception of objects used in programming formality as noted above.)

The value associated with a text box control is usually a Variant, which can be anything including numbers with fractional parts. If the control is a Radio Button or Toggle Button or List Box or Combo Box, the associated value is a special case and is those cases an integer.

Your problem is that if you have an event routine interacting with the controls and use a LONG in the computation, there is where you constrained your display. At any time that an INTEGER-class variable sneaks into a computation, that is where you lose the decimal places.
 

cheekybuddha

AWF VIP
Local time
Today, 15:57
Joined
Jul 21, 2014
Messages
2,280
@Mike, did you read Tom's suggestion?

Since you are dealing with doubles, why not Dim ... As Double ?

If you will never need accuracy beyond 4 decimal places, then I would advise using Currency datatype instead, both in the table and also in the VBA code. It is less prone to rounding errors.
 

mike60smart

Registered User.
Local time
Today, 15:57
Joined
Aug 6, 2017
Messages
1,905
@Mike, did you read Tom's suggestion?

Since you are dealing with doubles, why not Dim ... As Double ?

If you will never need accuracy beyond 4 decimal places, then I would advise using Currency datatype instead, both in the table and also in the VBA code. It is less prone to rounding errors.
Hi David

I did note his suggestion and will test when I get some time.

Many thanks.
 

Users who are viewing this thread

Top Bottom