473,223 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,223 developers and data experts.

VBA Data Types

MMcCarthy
14,534 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Type        MemSize RetVal of VarType() Declaration Char   Conversion
  2. Boolean     2b      vbBoolean(11)                          CBool()
  3. Byte        1b      vbByte(17)                             CByte()
  4. Currency    8b      vbCurrency(6)       @                  CCur()
  5. Date        8b      vbDate(7)                              CDate()
  6. Decimal     14b     vbDecimal(14)                          CDec()
  7. Integer     2b      vbInteger(2)        %                  CInt()
  8. Long        4b      vbLong(3)           &                  CLng()
  9. LongLong    8b      vbLongLong(20)      ^                  CLngLng()
  10. Single      4b      vbSingle(4)         !                  CSng()
  11. Double      8b      vbDouble(5)         #                  CDbl()
  12. String      10b+    vbString(8)         $                  CStr()
  13. Object      4b      vbObject(9)
  14. Variant     16b     vbVariant(12)                          CVar()

The Boolean Data Type (True or False)
Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and false, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values –1 and 0, respectively.

The Byte Data Type (0 to 255)
Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't support negative values. If you attempt to assign one, VBA returns an error.

The Currency Data Type
Use the Currency numeric data type to store monetary values from –922,337,203,477.5808 to 922,337,203,685,477.5807.

The Date Data Type
The Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

The Decimal Data Type
The Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from –79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places. The data type maintains precision up to 28 decimal places with values from –7.9228162514264337593543950335 to 7.9228162514264337593543950335.

The Integer Data Type
This is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from –32,768 to 32,767.

The Long Data Type
The Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648 to 2,147,483,647.

The LongLong Data Type (64-bit only)
The LongLong data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer & Long data types. Use the LongLong data type to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

The Single Data Type
The Single data type stores precision numbers—numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

The Double Data Type
Use the Double data type to store precision floating point numbers from –1.79769313486232E308 to –4.94065645841247E-324 or 1.79769313486232E308 to 4.94065645841247E-324.

The String Data Types (Fixed & Variable lengths)
String is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. The Dim for a Fixed String is of the form:
Dim variablename As String * stringlength

In contrast, the Variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form
Dim variablename As String

The Object Data Type
An Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module.

The Variant Data Type
The Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications.

The Variant data type is VBA's default, so the following code interprets varValue as a Variant:
Dim varValue

Although the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit.

The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't easily determine the appropriate data type by viewing the code, and that can be a problem.

Note:
Please see Matthew's post for information on updates in VBA7 for 64-bit arithmetic and pointer compatibility.

NB:
The LongLong data type has now been added in as well as a table showing some further helpful information.
Feb 13 '07 #1
8 193799
The Long Data Type
2,147,486,647 should read 2,147,483,647
(Open Windows Calculator, select scientific view, and try 2^31 - 1, to get this result. It's a 32-bit signed integer.)
Aug 2 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
Thanks, I'll edit this in the original :) Probably just a type originally.

Mary
Aug 4 '10 #3
Good notes, thanks
Nov 5 '10 #4
Updates in VBA7 for 64-bit arithmetic and pointer compatibility:

http://msdn.microsoft.com/en-us/libr...ice.14%29.aspx

http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx

Note, for example, the new 64-bit LongLong type and various Ptr types.
Nov 23 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
Thank you for that Matthew. I've edited the original article to add a link to your post.
Nov 25 '10 #6
NeoPa
32,554 Expert Mod 16PB
I've just taken the liberty (Sorry Mary) of updating the OP of this, still very popular, thread to bring it up-to-date with the main new 64 bit Long Long data type mentioned previously in the discussion. I also took the opportunity, while I was about it, to add in a table at the top showing some often useful information about the data types included here.
Oct 23 '21 #7
svtsolutionllc
2 2Bits
Good one
Noted!
Nov 3 '21 #8
NeoPa
32,554 Expert Mod 16PB
I wanted to add a link here (Type Characters) to an article on this subject from Microsoft for further reading if required.
Dec 4 '21 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
3
by: lovecreatesbeauty | last post by:
Prof. Bjarne Stroustrup said "built-in data types have default constructor" at §10.4.2 in `TC++PL, special ed.'. He also said that "built-in data types are not classes" at §11.4 in `TC++PL,...
4
by: No One | last post by:
Here is my problem: I have a certain set of well-defined manipulations that I have to apply to different types of data. In all cases the manipulations are exactly the same, and are to be...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
8
by: ramu | last post by:
Hi, I want to call a vc++ function from a c program on linux. So when I do this dosen't the VC++ datatypes differ with c datatypes. Because we don't have some vc++ data types in c. I have to...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
4
by: mshetty | last post by:
Hi, We need a list of all VC++ Data Types. Searched through MSDN but could not find a single list. Since we are new to VCC+ we really don't know if what we are getting is sufficient or there is...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.