Visual Basic 6 Black Book - Free ebook download as PDF File .pdf), Text File . txt) or read book online for free. Visual Basic 6 Black Book [Steven Holzner] on musicmarkup.info *FREE* shipping on qualifying offers. Completely explains the crucial Visual Basic tool set in detail. The Visual Basic 6 Programming Black Book is jam packed with insight, programming tips and techniques, and real world solutions you can put to work today.
|Language:||English, Spanish, Portuguese|
|Genre:||Academic & Education|
|ePub File Size:||30.85 MB|
|PDF File Size:||19.33 MB|
|Distribution:||Free* [*Sign up for free]|
Author of Physics For Dummies. Learn to: • Grasp physics terminology. • Get a handle on quantum Physics II F Quantum Physics Workbook For Dummies. Visual Basic 6 Black Book: The Only Book You'll Need on Visual Basic Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for. Making ActiveX Control Properties Persistent (PropertyBag Object). Adding A Method To An ActiveX Control. Visual Basic 6 Black Book:Table.
Shelves: computers , work , library , non-fiction , programming , reference My main annoyance with Visual Basic in general is that it is geared towards getting things done quickly without much regard to good programming practices. VB developers tend to learn things by looking at a series of examples rather than really understanding the core implementation of the language. So, recently I found myself in a situation where I needed to get a VB application out quickly without really having any experience with the language at all. In order to avoid becoming what I despise, I My main annoyance with Visual Basic in general is that it is geared towards getting things done quickly without much regard to good programming practices. In order to avoid becoming what I despise, I thought I would pick up a book or two with the hopes that I could find a more detailed documentation of the VB language than I can find by hacking out examples I find on the Internet.
The powerful object-oriented programming capabilities in the Visual Basic 6. Time efficiently and help you design error-free programs that produce the desired. Visual Basic merupakan bahasa pemrograman yang sangat mudah dipelajari, dengan teknik pemrograman visual yang memungkinkan. Upgrading Microsoft Visual Basic employee policy pdf 6. NET13 Oct NET Programming. This ebook is a quick introduction to VB.
NET programming. Chapter 6: Variables again, group box, list box. Free ebook for starting to learn Visual Basic and higher especially coming from VB 6. To those that want to make the switch from VB 6. FREE shipping on qualifying offers.
This book helps you build. Download for FREE in 4 easy steps. I would like to express my sincere gratitude to many people who have made their contributions in one way or another to the successful. Visual Basic Overview Variable Scope Prefixes You use a variable prefix in front of its name to indicate something about that variable. Well continue with variable prefixes.
Microsoft has established scope prefixes for variables as shown in Table 1. For example. Visual Basic Overview intCounter 'Integer sngDividend 'Single Using variable prefixes this way provides some clue as to the variables type. Besides variable prefixes. Note that its also a good idea to prefix function names using the above prefixes to indicate the return type of the function. Microsoft also has a set of prefixes for the standard control types.
Microsoft recommends prefixes for menus and constants as well. Menu And Constant Prefixes Microsoft recommends that you prefix menu controls with mnu and then the menu name followed by the menu item name.
Microsoft also recommends that constant names you declare constants http: Code Commenting Conventions In general. Although standard Visual Basic constants do not include data type and scope information.
Youre not constrained to use them. That completes the prefix and naming conventions. Visual Basic Overview with the Const statement should be mixed case with capitals starting each word. Microsoft also has a set of suggestions on commenting your code. The Microsoft recommendations for those sections appear in Table 1.
Such books get pretty involved. Note also that we multiply the parameter sngSquareMe by itself to square it instead of using the exponentiation operator. There have been whole volumes written on this topic. Well finish the chapter with a look at what we might call best coding practices.
Note that its particularly important to list all the global variables a procedure uses or affects in this initial comment block. On the whole. You might notice that dblSquare takes a Single parameter and returns a Double value.
Visual Basic Overview Heres an example showing how to set up a comment preceding a function named dblSquare: Squares a number ' Inputs: That completes our overview of the Visual Basic programming conventions.
Best Coding Practices In Visual Basic The full construction of a commercial program is usually a project that involves many clear and definite steps. Through the years. Visual Basic Overview " Detailed designDesign the actual data structures.
Although there are formal specifications for PDL. Returns the passed parameter's square root ' Inputs: You use this model to get a conceptual handle on the software while keeping in mind that models are usually flawed at some level. Keeping the model in mind. As the design process continues. Program Design Language Everyone seems to think that programmers use flowcharts. Function dblSqrt Check if the input parameter is negative If the input parameter is negative.
One tool that commercial programmers do find useful is program design language PDL. Each of these steps may have many subparts. So much for overviewlets turn to particulars that affect us as Visual Basic programmers.
This can save a bug from propagating throughout your program and help pinpoint its source. A magic number is a number excluding 0 or 1 thats hardwired right into your code like this: Here we go: This is also an aid in larger programs when things start to get complex. If you need to. Make no assumptions. Its better to declare such numbers as constants. Debugging deeply nested conditionals visually is very. Divide and conquer is the idea here. Three levels of nesting should be about http: An example of programming defensively would be to check data passed to you in a procedure before using it.
Putting code and data together into modules hides it from the rest of the program. Being modularalso called information-hiding and encapsulation in true OOP is the backbone of working with larger programs. When its time to change your code. Certainly if a procedure has two distinct tasks.
All of these practices come from professional programmers. If you pass global variables to procedures. Believe it or not. Global variables can create enormously complex conditions. This can lead to some serious bugs. For example: Microsoft recommends that global variables should be used only when there is no other convenient way to share data between forms.
This is so you can read or debug the string easily. This is part of programming defensively. If the rest of the program must call a Set procedure to set that data. You may be surprised by this.
This is per Microsofts recommendations. Visual Basic Overview the maximum. Although convenient. Because you dont have to declare a variable in Visual Basic to use it. Be consistent.
Because Visual Basic auto-declares variables. This utility. Well see more throughout the book. A better idea is to use Option Explicit to make sure all variables must be explicitly declared. Weve also seen a number of Visual Basic programming considerations. If you work in teams. Thats it for our best practices tips for now. There are several well-known utilities that help programmers work in teams.
Were ready for the rest of the book. Data Tips. And Font Sizes Aligning. Selecting IDE Colors. Auto List Members. Small Code. The IDE is composed of these parts: Visual Basic is in the background while your program runs. If youre not already familiar with the parts of the IDE. The IDE used to be more or less invisible to the programmer. Figure 2. Well start with an overview of the IDE. The current state appears in Visual Basics title bar.
In the Run state. Well cover the Debug state later in the book. This chapter concentrates on the Design state. Its the Design state thats become complex over the years. ActiveX controls to add. Over the years. Note that one important job of the File menu is to create EXE files for your program. To display one of these toolbars.
Besides the main toolbar. When you run a program from the Run menu. Heres a list of those menus and what they do: Well see a great deal more about these menus and the items they contain in the Immediate Solutions section of this chapter.
This toolbar contains buttons matching popular menu items. The Visual Basic Development Environment free-floating at first. A tool tip a small yellow window displaying text will display the tools purpose. If youre unsure what a particular tool in the toolbar does. Here are the text items whose colors you can select: Lets get into the actual meat of the chapter now. The form designer displays the current form under design. There are two drop-down list boxes at the top of the code window: You can place controls into a form simply by drawing them after clicking the corresponding controls tool in the toolbox.
That completes our overview of the IDE. Form designers are really just windows in which a particular form appears. Visual Basic allows you to do that. Code windows are similarly easy to understand: But as when you move into a new house.
Select the type of alignment you want in the Align submenu: Figure You can also set text font and font sizes in the same wayjust specify the new setting and click on the OK button to customize the text the way you want it. To align all the selected controls to the same left. If youve got a number of controls that should be aligned in a straight line. Make sure you have one control in the correct position.
The other controls will be aligned using this key controls position. The sizing handles appear hollow around all but the last control you clicked. Sizing handles. Hold down the Ctrl key and click all the controls you want to align. Select the Align item in the Format menu. Select the Make Same Size item in the Format menu. The Visual Basic Development Environment 5.
Choose the appropriate item in the Make Same Size submenu to size the controls as you want them: The grid is made up of the array of dots you see on a form at design time. To open the Options box. To size all selected controls the same as the key control. Besides setting the units of the grid.
You can change the grid units in twips in the Options box when you click the General tab. To space multiple controls vertically or horizontally. To space the controls horizontally or vertically. This grid is to help you place controls on a form.
While the controls are still collectively selected. That case is a little extreme. Visual Basic programs do not need to have any windows at all. Not necessarily. To specify the startup form for a project. Select the Properties item in the Project menu to open the Project Properties box. Select Add Module in the Project menu. You dont want to use the Application Wizard. Selecting The Startup Form On the other hand. Add this code to the new modules General section in the code window: Sub Main End Sub 5.
Using Visual Basic Predefined Forms. Click the General tab in the Project Properties box if its not already selected. You do that with the General tab of the Project Properties box. And Projects Youre designing a new program. Now when your program starts. Place the code you want in the Main subroutine. Rather than designing a complete standard File menu from scratch. Follow these steps: Besides menus. These are the available menus: Open the Menus folder in the Templates folder.
To add one of the predefined Visual Basic menus. Open the Visual Component Manager from the Tools menu. Select the form you want to add the menu to by clicking it with the mouse. Select the type of menu you want and double-click it.
Here are the available forms. Open the Templates folder in the Visual Basic folder. The new menu will be added to the form you selected. MDI forms. This adds a new form to your project. Adding menus is a little different here.
Select the Make tab. Create the EXE file. He can do that by interrogating the files version information. As you can see in Figure 2. Heres how you do it: To look at the version information in CDU A programs version information includes more than just the version number of the program. All these items are available to the user.
Fill in the information you want. And Form Editor Toolbars By default. Visual Basic displays one toolbar. Select the General tab in the Project Properties box if its not already selected.
If you want them. The stockholders meeting is in five minuteshow can you change your EXE files name? To set the EXE files name. Displaying The Debug. The programs icon is just the icon of the startup form. The Debug toolbar has the following buttons: Enter the name of the project you want to use.
Now youve named your EXE file. You can also remove one or all toolbars the same way. If you have a new icon in ICO file format. What does a bounds violation look like? Heres an example in code where we set up an array and then try to access a location past the end of it: End Sub If you were to run this code. You can turn off bounds checking by following these steps: In the early days of programming. Visual Basic checks to make sure that you dont inadvertently try to access memory past the end or before the beginning of the array when the program runs.
That practice is heavily discouraged today. Select the Compile tab in the Project Properties window. In fairness. Select the Remove Array Bounds Checks checkbox to turn off array bounds checking. Thats itnow youve turned off array bounds checking.
But when you check the Visual Basic Add-Ins menu. Managing Add-Ins The deadline for your project is fast approaching. Although you might want to do this yourself if you know what youre doing. Suddenly it occurs to you that youve already written a lot of the components you need to use the day is saved! But how can you access those components? One easy way is to use the Visual Component Manager. Intel responded quickly and offered to replace the defective chips.
Visual Basic has a check to make sure the Pentium your program runs on is safe. The Visual Basic Development Environment 4. Click the Advanced Optimizations button in the Project Properties window to open the Advanced Optimizations box as shown earlier in Figure 2. That check is enabled by default. How do you add it? For that reason. Checking For Pentium Errors Some time ago. Heres how to use the Add-In Manager: Before turning off array bounds checking.
The Add-In Manager opens. Select the ActiveX control you want to add in the Components box. Select the Project menus Components item.
Try using the regsvr The new control will appear in the toolbox. Some add-ins have an annoying habit of starting when Visual Basic starts. Youve been designing your project but suddenly realize you need a Microsoft Grid control. The Visual Basic Components box opens.
Instead of the Controls tab in the Components box. None of thoseheres how you do it: Thats an ActiveX controlhow do you add those again? Use the Add File To Project menu item? Double-click the toolbox and hope an Insert dialog box comes up?
Add a reference to the actual Grid controls OCX file. If the ActiveX control you want to add to a Visual Basic project doesnt appear in the Components dialog box. You can also add insertable objects like Microsoft Word or Microsoft Excel objects to a Visual Basic project by using the Components dialog box.
Thats itnow youve added the add-in you want. To remove it. Select the add-ins you want. You can do that the same way youd drag that item to a new menujust drag it into the menu bar instead.. If you use one particular menu item a lot. But youre not http: Using the mouse. Heres how you move items between menus or toolbars: Setting Forms Initial Positions Youve completed the projecton schedule and under budget even.
Releasing the mouse adds the menu item to its new location. Right-click the menu bar to open the Customize box.
Just grasp the double upright bars at left in the toolbar Visual Basic uses Explorer-style toolbars and move the toolbar to its new location. The toolbars in Visual Basic are dockable. Besides moving menu items to new locations in menus and toolbars. You might think. You can move menus to either the menu bar or other toolbars this way.. If you want to know the forms exact new position.
That means. And Syntax Checking Depending on your personal tastes. Enabling Or Disabling Quick Info. Using the Form Layout window. This window appears in Figure 2. You can set the forms Left and Top properties if you like. This is a useful feature that can save you time looking up parameter order or type.
Setting a forms initial position couldnt be easierjust drag the form into the new location using the mouse. This is useful if you cant remember exactly what property you want to work with for example. The Auto List Members feature lists the members of an object as youre typing the objects name actually when you type the dot [.
These features are as follows: The Visual Basic Development Environment crazy about where Visual Basic displays the startup form on the screen when the program starts.
Select the options you want from the checkboxes: Auto Syntax Check. Note that Data Tips can only display the values of simple variables. Select the Options item in the Tools menu. For those objects. When Visual Basic is in the Debug state.
Syntax Checking speaks for itselfwhen you move the text insertion point away from a line of Visual Basic code while writing that code.
That can get annoying if youre the type of programmer who likes to move around in a file while writing code What was the name of that variable again? Thats all it takes. Auto Quick Info.
The Visual Basic Development Environment property? Visual Basic will check the lines syntax and display an error box if there is an error. Visual Basic Data Tips are tip tools that appear while youre debugging a program. Select the Editor tab in the Options box. You can turn all of these features on and off following these steps: Want to clear some IDE windows out to make room for the important ones?
Just close the windows by clicking their close buttons the button marked x in the top right of the window. MDI formshow are you supposed to keep them all straight? These days. Your office? The Project Explorer is one such tool. To do that. But how do you get it back? Or how would you get the toolbox back if it disappeared? Or the Properties window? The solution is easy: All you have to do is to select the window you want to show again in the View menu.
Even if youre familiar with searching for text throughout an entire project. Now you can search all the code in an entire project if the code window is openjust click the Current Project option button before searching. One such occasion is when you want to find all the occurrences of specific text throughout an entire projectfor example.
Whoopsnow you need the Form Layout window back. Selecting that item opens the Find box. Open the View menu as shown in Figure 2. This window gives you an overview of your entire project. This is a simple task indeed. To jump to the variables or procedures definition. Doing so opens a pop-up menu.
The Pentium Pro is currently the only processor Visual Basic lets you optimize for.
Or A Particular Processor Your project works the way you want it. To jump to a variables or procedures definition. The Visual Basic Development Environment one more capability that you might not know aboutjumping to a variables or procedures definition just by clicking it.
Besides jumping to a variable or procedures definition in code. The Project Properties box opens. Select the kind of code optimization you want in the " Properties box: This is very useful when.
Select the Compile tab in that box. Optimizing For Fast Code. Select the Properties item in the Visual Basic Project menu. To optimize your program for code size or speed. Isnt there any way to make it less than MB? Adding ActiveX Designers Besides ready-made objects like forms and modules.
Now its time to add an About dialog box. The Visual Basic Project menu allows you to add these items to a project: Select the Designers tab in the Components box. These designers let you design new objects that are part of your project. You can reach the new object designer to design the addition to your project with the Add ActiveX Designer item in the Project menu.
Select the Components item in the Project menu. That item opens a submenu http: And Class Modules Your project is nearly finished. So how do you add new forms to a project? You do that in one of a couple of ways: Adding And Removing Forms.
The Add submenu opens. Select the designer you want to add. The Visual Basic Development Environment showing the available designers. It would be convenient if Visual Basic allowed you to name bookmarks and select from a list of them where to jump to. You jump back to a bookmark with the two items in the Bookmarks submenu marked Next Bookmark and Previous Bookmark.
You can scan through all the objects in the project by moving up and down in the http: You mark a line of code by toggling a bookmark on or off at that location. Isnt there a better way to mark a location and jump back to it when you need to?
There certainly isyou can use a bookmark. Selecting this same item again would remove the bookmark. The Object Browser is very useful to get overviews. Setting Bookmarks You set a bookmark at a particular line of code by clicking that line of code and selecting the Toggle Bookmark item in the Bookmarks submenu of the Edit menu. Now youve lost your place in the dozen separate code files that make up the project.
The Object Browser opens. There are 10 separate windows open in the IDE and youre switching back and forth between them. Jumping To A Bookmark Now that youve set a bookmark and moved away from it while editing your code.
When you find the type of object you want to examine. If you want to learn more about a property or method. What will that code look like? Some of our code will be short. A good start here is essential for the work well do throughout the book.
If youve programmed in other languages.
Were going to take a look at the elements of the Visual Basic language that will let us make Visual Basic code work. The Visual Basic language supports a large number of programming constructs and elements. Well even see how to handle special Visual Basic formats like dates and financial data. And well see some items that programmers like but dont often encounter in programming books.
Well cover tasks that involve some complexity and whose syntax is hard to remember. Dim [WithEvents] varname [ [subscripts] ] [As [New] type] [. Well see how to use text strings. Immediate Solutions Declaring Constants Youve filled your code with numeric valuesand now its time to change them all as you start work on the http: Well see a lot of syntax in this chapter.
As you can imagine. In the topics coming up. The constname identifier is the actual name of the constant.
How do you use constants? You declare constants in Visual Basic with the Const statement: What a pain to have to track down and change all the numeric values called magic numbers throughout all the code. When its time to change those values. This keyword is not allowed in procedures.
The Private keyword is used at the module or form level to declare constants that are private. Private is not allowed in procedures constants in procedures are always private anyway. The Visual Basic Language new version of the software. The type identifier is the data type of the constant.
Like the Public keyword. The expression identifier holds the value you want for this constant. It may be a literal. Isnt there a better way? There is: Use constants and declare them all in one place.
Public declare a global variable. That way. The varname identifier is the name of the variable you are declaring. This keyword specifies that varname is an object variable used to respond to events triggered by an ActiveX object. You use subscripts if youre declaring an array.
You set up the subscripts argument this way: ReDim redimension a dynamic array. The Dim Statement Heres how you use the Dim statement: Dim [WithEvents] varname[ [subscripts] ] [As [New] type] [. Heres an example showing how to declare and use a constant: Static declare a variable that holds its value between procedure calls.
The WithEvents keyword is valid only in class modules. You cant use variables. The New keyword enables creation of an object.
Heres an example of declaring variables using Dim: Variant variables are initialized to Empty. By default in Visual Basic. If you dont specify a type. Heres an example: If you use New when declaring the object variable.
Its better to require all variables to be explicitly declared. This means you dont have to use the Set statement to assign the object reference. In Visual Basic. String for variable-length strings.
Even if you remember what types there are. Selecting Variable Types Its time to create a new variablebut what type should you use?
For that matter. The Visual Basic Language To force variable declarations to be explicit that is. Table 3. Visual Basic has a large number of data formats. If you dont declare a type for a variable.
We also include the literal suffix symbols for numeric values in Table 3. The Visual Basic variable types appear in Table 3. The Variant type deserves special mention. The Visual Basic Language Theres a wide range of data types. The possible conversion statements and procedures appear in Table 3. End Sub Be careful of variants. End Sub In this case. Converting Between Data Types Visual Basic supports a number of ways of converting from one type of variable to anotherin fact.
TimeValue number TIP: Note that you can cast variables from one type to another in Visual Basic using the functions CBool. Year month. Str string One data CBool.
The Visual Basic Language Table 3. DateValue number Decimal number to Hex. Int another Date to day. Second minute. Oct other bases Number to Format. UCase uppercase Date to serial DateSerial. Text http: But in this case. Its tempting to set up a global variable. The Visual Basic Language This avoids setting up a global variable needlessly.
These variables are available throughout the module. You can get an overview of the scope of variables in a Visual Basic project in Figure 3. In general. Visual Basic has a number of data verification functions.
Figure 3. For more information. Note that you cannot use Public in procedures. If you use the Option Private Module statement in a module or form. For these and other reasons. There are three levels of variable scope in Visual Basic. You use subscripts to declare the array. Standard Arrays You usually use the Dim statement to declare a standard array note that in Visual Basic.. The type argument specifies the data type of the variable.
Private arrays private to the form or module theyre declared in. ReDim dynamic arrays. But wait a momenthow are you going to handle the data? Its just a simple program. Well start with standard arrays now. Public arrays global to the whole program. Dim [WithEvents] varname [ [subscripts] ] [As [New] type] [.. Static arrays that dont change when between calls to the procedure theyre in. You can use Dim standard arrays.
An array would be perfect. Dynamic Arrays You can use the Dim statement to declare an array with empty parentheses to declare a dynamic array. Option Base 0 or Option Base 1. Here are a few examples of standard array declarations: You use the Preserve keyword to preserve the data in an existing array when you change the size of the last dimension. The Visual Basic Language Currency.. Dynamic arrays can be dimensioned or redimensioned as you need them with the ReDim statement which you must also do the first time you want use a dynamic array.
You use the Option Base statement at the form. The default value is 0. The varname argument holds the name of the array to re dimension. Heres how you use ReDim: ReDim [Preserve] varname subscripts [As type] [. Heres an example that creates an array with the values 0. This is one of those topics that is made easier with an example. The Visual Basic Language The subscripts term specifies the dimensions of the array using this syntax: The type may be Byte.
If you dont specify any arguments. Heres how you use Array: Array arglist The arglist argument is a list of values that are assigned to the elements of the array contained within the variant. Well finish this topic with a summary of array-handling techniques. Array-Handling Techniques Summary Visual Basic has a number of statements and functions for working with arrays. UBound array Reinitialize an array Erase.. The Private keyword makes a procedure accessible only to other procedures in the module or form in which it is declared..
For references sake. Unlike functions. The Friend keyword is used only in class modules and specifies that the procedure is visible throughout the project. You separate multiple variables with commas.
The arglist identifier is a list of variables representing arguments that are passed to the procedure when it is called. The name identifier is the name of the procedure. The arglist identifier has the following syntax: The Static keyword specifies that the procedures local variables should be preserved between calls.. End Sub The Public keyword makes a procedure accessible to all other procedures in all modules and forms. ReDim Declaring Subroutines Everyone knows about subroutines: Static an array Find the limits of an LBound.
The statements identifier is the group of statements to be executed within the procedure.. For an overview of how to comment procedures. ByVal means that the argument is passed by value. The Exit Sub keywords cause an immediate exit from a Sub procedure. A function is a block of code that you call and pass arguments to. Optional means that an argument is not required..
End Sub ends the procedure definition. You call a Sub procedure using the procedure name followed by the argument list. When you use ByVal. Heres an example of a subroutine: Subroutines can take arguments passed in parentheses but do not return a value.
ParamArray is used as the last argument in arglist to indicate that the final argument is an array of Variant elements. Declaring Functions There are two types of procedures in Visual Basic: