What variable type to use.

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

What variable type to use.

Post by Steve_in_Kent »

Im reading in a text string from a text file.

example:-

Code: Select all

Dim VarianceString As String
Dim Varian1 As Long

linetxt = the string im reading in.

VarianceString = Mid(linetxt, 108, 14)
Varian1 = CLng(Nz(VarianceString, 0))
VarianceString might be read in as, "3219.9673"

but after the conversion, Varian1, would be 3220.

I'm then storing Varian1, in a table.

I've tried setting the table properties to have 4 decimal places. and i've looked at the different date conversion Cxxx, the only one that seems to have the right decimal places, is CCUR, which is for currencies.

how do i get it , so that i put exactly that number, as read by the string, into my table ?
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: What variable type to use.

Post by Don Wells »

I know that VBA for Access is different in many ways to Excel and Word, but I would expect that you should dimension Varian1 as Double.
Regards
Don

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: What variable type to use.

Post by JohnH »

I agree with Don
A long is a long integer, so it is always a whole number.
Regards

John

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: What variable type to use.

Post by Steve_in_Kent »

ok.. many thanks guys! - i will give it a reply and post back

:clapping:
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: What variable type to use.

Post by Steve_in_Kent »

Well, no luck with this.

In the table properties, the field is set to Double, and the Decimal places set to 4

in the vba, the string is dimensioned as Double.

and the conversion vba, is

Code: Select all

CincomQty = CDbl(Nz(CincomQtyString, 0))
however, when i look at the data its not showing any decimal point, or anything past that point.
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: What variable type to use.

Post by Don Wells »

The following works for me:

Code: Select all

Public Sub TST()
    Dim VarianceString As String
    Dim Varian1 As Double

    VarianceString = "3219.9673"
'    Varian1 = CLng(Nz(VarianceString, 0))
    Varian1 = Val(VarianceString)
    
    Debug.Print Varian1
End Sub
Regards
Don

User avatar
Steve_in_Kent
4StarLounger
Posts: 419
Joined: 04 Feb 2010, 11:46

Re: What variable type to use.

Post by Steve_in_Kent »

thanks don this now works..

looks like i needed the VAL() bit!!!?

steve
----------------------------------------------------------------------------------------
Owing at LEAST 34 beers to other helpful forum members. Especially HansV!