A comment is a piece of text in a code section that the database engine would not consider when reading your code. As such, a comment can be written any way you want. In Visual Basic, the line that contains a comment can start with a single quote. Here is an example: Private Sub Form_Load() ' This line will not be considered as part of the code End Sub Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be read. Here is an example: Private Sub Form_Load() ' This line will not be considered as part of the code Rem I can write anything I want on this line End Sub Comments are very useful and it is strongly suggested that you use them regularly. They can never hurt your code and they don't increase the size of your database. Comments can help you and other people who read your code to figure out what a particular section of code is used for, which can be helpful when you re-visit your code after months or years of not seeing it.
We saw earlier that you could declare a variable based on a built-in object of VBA. To specify the particular object you are referring to, you can (must) use the Set operator to assign an existing object to your variable. This would be done as follows: dim ctlFirstName as Control Set ctlFirstName = TextBox If you are displaying a string but judge it too long, you can segment it in appropriate sections as you see fit. To do this, you can use vbCrLf. Here is an example: Sub Exercise() Dim FirstName As String, LastName As String, FullName As String Dim Accouncement As String FirstName = "Valère" LastName = "Edou" FullName = FirstName & " " & LastName Accouncement = "Student Registration - Student Full Name: " & _ vbCrLf & FullName End Sub
Parentheses are used in two main circumstances: in an event (or procedures, as we will learn) or in an operation. The parentheses in an operation help to create sections in an operation. This regularly occurs when more than one operators are used in an operation. Consider the following operation: 8 + 3 * 5 The result of this operation depends on whether you want to add 8 to 3 then multiply the result by 5 or you want to multiply 3 by 5 and then add the result to 8. Parentheses allow you to specify which operation should be performed first in a multi-operator operation. In our example, if you want to add 8 to 3 first and use the result to multiply it by 5, you would write (8 + 3) * 5. This would produce 55. On the other hand, if you want to multiply 3 by 5 first then add the result to 8, you would write 8 + (3 * 5). This would produce 23. As you can see, results are different when parentheses are used on an operation that involves various operators. This concept is based on a theory called operator precedence. This theory manages which operation would execute before which one; but parentheses allow you to completely control the sequence of these operations.
We know that it is suitable to use one-word names for objects in Microsoft Access. In reality, Microsoft Access, as mentioned already, is particularly flexible with names. We saw that we could use square brackets to enclose a name made of. As seen in Lesson 2, this principle is the same here.
We know that the exclamation point operator "!" is used to access a member of a collection.
It is usually suitable to write lines of code that are not too long. This makes it easy to read code and avoid scrolling left and right. In some cases, you will not have much choice but to create long expressions. Unlike many other languages such as C/C++/C#, Java, etc, Visual Basic doesn't allow to simply continue a line of code from one line to the next without alerting the compiler. Still, if a line of code becomes too long, there is a technique you can use to span on various lines. To continue a piece of code from one line to the next, type an empty space followed by an underscore symbol, then continue your code on the next line.
So far, we have seen various ways of creating a database, including creating a blank database or using the wizard. Besides these techniques, you can also programmatically create a database. To do this, first declare a variable of type Application and initialize the variable with the version of the Microsoft Access that will be used. To actually create the database, call the NewCurrentDatabase method of the Application class. This method takes as argument the path and the name of the new database. The name should include the .mdb extension but if you omit it, the extension would be added when the database is created. Here is an example that creates a new database named Championship in a folder named Programs on the C: drive: Private Sub cmdCreateDatabase_Click() Dim strNewDB As String Dim appAccess As Access.Application strNewDB = "C:\Programs\Championship.mdb" Set appAccess = CreateObject("Access.Application.9") appAccess.NewCurrentDatabase strNewDB End Sub
From our introduction to variables, you may remember that the computer stores its data in memory using small locations that look like boxes and each box contains a bit of information. Because a bit can be represented only either as 1 or 0, we can say that each box contains 1 or 0. Bit manipulation consists of changing the value (1 or 0, or 0 or 1) in a box. As we will see in the next few operations, it is not just about changing a value. It can involve reversing a value or kind of "moving" a box from its current position to the next position. The operations on bits are performed on 1s and 0s only. This means that any number in decimal or hexadecimal format involved in a bit operation must be converted to binary first. You will almost never perform some of the operations we are going to review. You will hardly perform some other operations. There is only one operation you will perform sometimes: the OR operation.
Remember that, at any time, a box (or chunk) in memory contains either 1 or 0:
Bit reversal consists of reversing the value of a bit. If the box contains 1, you can reverse it to 0. If it contains 0, you can reverse it to 1. To support this operation, the Visual Basic language provides the Not Operator. As an example, consider the number 286. The decimal number 286 converted to binary is 100011110. You can reverse each bit as follows:
Bitwise conjunction consists of adding the content of one box (a bit) to the content of another box (a bit). To support the bitwise conjunction operation, the Visual Basic language provides the And operator. To perform the bit addition on two numbers, remember that they must be converted to binary first. Then:
As an example, consider the number 286 bit-added to 475. The decimal number 286 converted to binary is 100011110. The decimal number 4075 converted to binary is 111111101011. Based on the above 4 points, we can add these two numbers as follows:
Therefore, 286 And 4075 produces 100001010 which is equivalent to:
This means that 286 And 4075 = 256 + 16 + 2 = 266 This can also be programmatically calculated as follows: Sub Exercise() Dim Number1 As Integer Dim Number2 As Integer Dim Result As Integer Number1 = 286 Number2 = 4075 Result = Number1 And Number2 End Sub
Bitwise disjunction consists of disjoining one a bit from another bit. To support this operation, the Visual Basic language provides the Or operator. To perform a bitwise conjunction on two numbers, remember that they must be converted to binary first. Then:
As an example, consider the number 305 bit-disjoined to 2853. The decimal number 305 converted to binary is 100110001. The decimal number 2853 converted to binary is 101100100101. Based on the above 4 points, we can disjoin these two numbers as follows:
Therefore, 305 Or 2853 produces 101100110101 which is equivalent to:
This means that 286 And 4075 = 2048 + 512 + 256 + 32 + 16 + 4 + 1 = 2869 This can also be programmatically calculated as follows: Sub Exercise() Dim Number1 As Integer Dim Number2 As Integer Dim Result As Integer Number1 = 286 Number2 = 4075 Result = Number1 Or Number2 End Sub
Bitwise exclusion consists of adding two bits with the following rules. To support bitwise exclusion, the Visual Basic language provides an operator named Xor:
As an example, consider the number 618 bit-excluded from 2548. The decimal number 618 converted to binary is 1001101010. The decimal number 2548 converted to binary is 100111110100. Based on the above 2 points, we can bit-exclude these two numbers as follows:
Therefore, 305 Or 2853 produces 101110011110 which is equivalent to:
This means that 286 And 4075 = 2048 + 512 + 256 + 128 + 16 + 8 + 4 + 2 = 2974 This can also be programmatically calculated as follows: Sub Exercise() Dim Number1 As Integer Dim Number2 As Integer Dim Result As Integer Number1 = 286 Number2 = 4075 Result = Number1 Xor Number2 End Sub
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|