How will you choose the appropriate data type you are going to use in declaring variable
This chapter is from the book Show Variable Data TypesThe data type of a variable determines the kind of data the variable can hold. You specify a data type by including the As keyword in a Dim statement. Here is the general syntax: Dim variableName As DataType variableName is the name of the variable and DataType is one of the data types. Here's a rundown of the most useful VBA data types:
Here are a few notes to keep in mind when using data types:
Changing the Default Data TypeI mentioned in the preceding section that VBA assigns the Variant type to a variable if you don't specify a data type. However, VBA supports a number of DefType statements that let you redefine the default data type. These statements all use the following syntax: DefType letter1[-letter2] Here, Type is a three- or four-letter code that specifies the data type, and letter1 and letter2 define a range of letters. Note that this is a module-level statement, so you must place it at the top of a module, before any procedures or functions. The idea is that any variable (or function argument or function result) that begins with one of these letters will be assigned the specified data type by default. For example, the DefInt keyword is used to set the default data type to Integer. If you want VBA to assign, say, the Integer data type to any variables that begin with the letters X through Z, you would add the following statement at the module level: DefInt X-Z Table 3.1 lists the various DefType keywords and the data types they represent. Table 3.1. VBA's DefType keywords.
Creating User-Defined Data TypesVBA's built-in data types cover a lot of ground and should be sufficient to meet most of your needs. However, VBA also lets you set up user-defined data types. These are handy for storing similar types of data in a single structure. For example, suppose your program is working with car makes and models. In this case, you might need to work with values for the manufacturer, the model, the year the car was made, and the purchase price. One way to go about this would be to set up variables for each item of data, like so: Dim carMake As String Dim carModel As String Dim yearMade As Integer Dim carPrice As Currency This approach works, but what if you need to work with the data from multiple cars at once? You could set up new variables for each car, but that seems too inefficient. A better way is to define a "CarInfo" data type that holds all the required information. Here's how you would do it: Type CarInfo make As String model As String made As Integer price As Currency End Type The Type keyword tells VBA that you're creating a user-defined data type. In this example, the new data type is named CarInfo. The statements between Type and End Type define the various elements within the new data type. Note that you need to place this definition at the module level; VBA doesn't let you define new data types within a procedure. Now you use the data type as you would any other. For example, the following statement declares a new variable named myCar to be of type CarInfo: Dim myCar As CarInfo From here, you refer to the various elements within the data type by separating the variable name and the element name with a period (.), like so: myCar.make = "Porsche" myCar.model = "911 Turbo" myCar.made = 2007 myCar.price = 122000 Why is it important to choose the correct data type for your variable?Choosing the right data types for your tables, stored procedures, and variables not only improves performance by ensuring a correct execution plan, but it also improves data integrity by ensuring that the correct data is stored within a database.
Which data type is appropriate for its variable?In VBA, each variable has a specific data type, which indicates which type of data it may hold. For instance, a variable that holds text strings has the data type String and is called a string variable. A variable that holds integers (whole numbers) has the data type Integer and is called an integer variable.
What is datatype as used in variable declaration?All variables in the Java language must have a data type. A variable's type determines the values that the variable can have and the operations that can be performed on it. For example, the declaration int count declares that count is an integer ( int ).
How do you assign a data type to a variable?To assign value to a variable equal sign ( = ) is used. The = sign is also known as the assignment operator. x = 100 # x is integer pi = 3.14 # pi is float empname = "python is great" # empname is string a = b = c = 100 # this statement assign 100 to c, b and a.
|