Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Data Type Question (VBA for Access2002)

    I'm a little confused about the differences between Integer, Long, Single and Double data types. Can someone help me with an explanation and a short example of when it's appropriate to use each type?

    Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Data Type Question (VBA for Access2002)

    The major division is between Integer and Long on one side, and Single and Double on the other side.

    Integers and Longs (or Long Integers) are both whole numbers; there are no decimals involved.

    Integers take up 2 bytes (16 bits) of space each and range from -32,768 to + 32,767.
    Long Integers take up 4 bytes (32 bits) of space each and range from -2,147,483,648 to +2,147,483,647.

    If you want to work with whole numbers, in general Long Integer is the best choice. You're less likely to run into limitations, and Pentium processors are optimized to work with Long Integers. Only if you have to store a huge amount of whole numbers, and you are sure that they will all fit in the range of -32,768 to +32,767, Integers are better because they occupy only half the space.

    Single and Double are floating point types; they can handle numbers with decimals such as 3.141592 and numbers with exponents such as 2.71828 * 10^23.

    Single (Precision) numbers take up 4 bytes (32 bits) each; they have an accuracy of about 8 digits.
    Double (Precision) numbers take up 8 bytes (64 bits) each; they have an accuracy of about 15 digits.

    If you want to work with numbers that may have decimals, Double is the safest choice. Calculations with Double Precision numbers are more accurate than calculations with Single Precision numbers. Only if you have to store a huge amount of numbers with decimals, Single Precision numbers are better because they only take up half the space.

  3. #3
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    Ahhh, excellent! So, most times, if I'm going to have, say, currency fields, I should use Doubles due to the decimals?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Data Type Question (VBA for Access2002)

    Uhhh...

    There is a dedicated data type Currency. It takes up 8 bytes (64 bits) and ranges from

  5. #5
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    Here's the code I'm working with:

    Option Compare Database
    Option Explicit

    Dim dRunningTotal As Double
    Const TAXRATE = 0.07
    Const dPricePerApple = 0.1
    Const dPricePerOrange = 0.2
    Const dPricePerBanana = 0.3

    Private Sub cmdCalculateTotals_Click()
    Dim dSubtotal As Double
    Dim dTotal As Double
    Dim dTax As Double

    dSubtotal = (dPricePerApple * txtApples.Value) + _
    (dPricePerOrange * txtOranges.Value) + _
    (dPricePerBanana * txtBananas.Value)

    lblSubtotal.Caption = "$" & dSubtotal
    dTax = (TAXRATE * dSubtotal)
    dTotal = dTax + dSubtotal
    lblTotal.Caption = "$" & dTotal

    dRunningTotal = dRunningTotal + dTotal
    lblRunningTotal.Caption = "$" & dRunningTotal

    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub

    Private Sub cmdResetFields_Click()
    Me.txtApples.Value = "0"
    Me.txtOranges.Value = "0"
    Me.txtBananas.Value = "0"

    Me.lblSubtotal.Caption = "$0.00"
    Me.lblTax.Caption = "$0.00"
    Me.lblTotal.Caption = "$0.00"
    End Sub

    Private Sub cmdResetRunningTotal_Click()
    dRunningTotal = 0
    Me.lblRunningTotal.Caption = "$0.00"
    End Sub

    Private Sub Form_Load()
    txtApples.SetFocus
    End Sub

    I'm working through a book for "Absolute Beginners". This is an exercise in the 2nd chapter. It all makes sense to me except for the Caption Properties of the labels in the latter half of the above code. These labels (subtotal, total, running total) are next to text boxes where, in the screen shot in the book, the results appear. In the code, and my program I built using this code, the results show in the LABELS. Isn't this wrong??? Shouldn't the results be in the text boxes???? <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Data Type Question (VBA for Access2002)

    My guess (without knowing the book) would be that there are two labels side by side: a label with caption "Subtotal" (I don't know the name of this label) and a label named lblSubtotal whose caption initially is "$0.00", which will be replaced by the amount of the subtotal in cmdCalculateTotals_Click. The second label has been made to look like a text box by giving it a sunken effect and making its background white (again, this is a guess)
    Similarly for Tax and Total.

  7. #7
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    Well, that would make sense. You'd think they'd tell me this though.....I'll change the controls to labels and see if it works. I'm sure it will as you are always right!!

    Thanks Hans

  8. #8
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    Yep, that was it. This is a good way to learn tho.....and I don't have to bug you so much.
    <img src=/S/bullseye.gif border=0 alt=bullseye width=45 height=15>

  9. #9
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    I'm faced with a ?simple? exercise and have a question or 2. My are instructions are as follows:

    Build a simple calculator program with an Access form that allows a user to enter numbers in two separate text boxes. The access form should have four separate command buttons for adding, subtracting, multiplying, and dividing. Write code in each command button's Click Event to output the result in a label control.

    Assuming what I've accomplished so far, I'm thinking that I must also write code for the AfterUpdate event of the first side of the equation, and set the Caption Property of the lblAnswer to represent txtVar1.Value + txtVar2.Value.

    Does that make sense that way it was asked?? Am I on the right track?? I don't really want specific answers, just to know if heading in the right direction.

    TIA

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Data Type Question (VBA for Access2002)

    Hi Gabi,

    If I understand the exercise correctly, the authors don't expect the caption of the answer label to change whenever the user changes something in the input text boxes, only when the user clicks one of the command buttons. So you don't need After Update events here, just the On Click events of the four command buttons. I'll leave the rest to you.

  11. #11
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Data Type Question (VBA for Access2002)

    I guess I was making it more difficult than it had to be! Now I understand- I shall enter, say, the number "5" in each text box. I will write code in the OnClick event of the cmdAdd button to make the output in lblAnswer equate to "10" (written in code, of course), and the cmdSubtract button to make the output in lblAnswer equate to "0", and so on.

    Thanks for the hint-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •