473,568 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA Data Types

MMcCarthy
14,534 Recognized Expert Moderator MVP
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,47 7.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,54 3,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.922816251426 433759354395033 5 to 7.9228162514264 337593543950335 .

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,8 54,775,808 to 9,223,372,036,8 54,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.797693134862 32E308 to –4.940656458412 47E-324 or 1.7976931348623 2E308 to 4.9406564584124 7E-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 193966
matthewslyman
20 New Member
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
Thank you for that Matthew. I've edited the original article to add a link to your post.
Nov 25 '10 #6
NeoPa
32,564 Recognized Expert Moderator MVP
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 New Member
Good one
Noted!
Nov 3 '21 #8
NeoPa
32,564 Recognized Expert Moderator MVP
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
4629
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
3665
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
1901
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, special ed.'. (Sorry, I'm reading a Chinese edition of the book, and can't get a English edition handy currently. Perhaps these words are not same as...
4
2262
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 performed on the different types of data. Currently I have a collection of functions that do exactly the same - the only difference between them is the...
11
3172
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 accomplish it. For example: int main void() { while there are more data types { print next data type; }
8
3262
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 convert the vc++ datatypes into c data types or i have to use eqvivalent data types. Can someone give me some idea how to convert or about the...
11
3419
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 difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
4
4483
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 more. Would help if we could get some reference link. Thanks and Regards, M Shetty
15
8418
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 a={int,float,char,int*..............................}, so that a should return me int and a should return me
7
3380
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 python developers. II want to add some more KEYWORDS and DATATYPES into the python script apart from the existing ones. It would be really great if...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.