Visual Basic 6 Code Sample



  1. Sample Visual Basic Program Code
  2. Visual Basic 6 Sample Code
  3. Visual Basic 2010 Code Samples
  4. Visual Basic 6 Code Examples With Activex
  5. Visual Basic Programs With Codes
  6. Visual Basic 6 Source Code Samples

This is a collection of highly requested Visual Basic code that I put together back in 1998 and 1999. Almost all of these samples will work on fine on Visual Basic 5.0 and for Microsoft Excel and Microsoft Access Programming (when done with VBA), however, I've only made sure that they work in VB6. Back in the day I used to answer a lot of questions and I frequented a lot of VB message boards. I noticed that the same questions kept getting asked over and over again, so I started saving code that seemed to be important. 95% of the code you'll find on this page was written by me, the other samples were either edited or submitted. This guide does not claim to have the best answers, just answers that will get the job done. I hope you find it helpful.

Visual Basic Sample Codes Visual Basic 6 is a third-generation event-driven programming language first released by Microsoft in 1991. In VB 6, there is no limit of what applications you could create, the sky is the limit. You can develop educational apps, financial apps, games, multimedia apps, animations, database applications and more. (Visual Basic 6.0) Firebase POST - Pushing Data. Uses the POST method to append a new record at a particular location in the database. Firebase automatically generates the push ID and returns it in the response. The data used in this example is at Chilkat Firebase Pigs Database, and is shown here.

If you aren't able to find what you need here, you can check out the main programming section for additional VB 6.0 code and tutorials.

Visual Basic Code Bank
Code TitleCode Description
Add a Menu to Another Program An example of how to add a menu to another program.
Beep like QBasic's Sound This shows you how to set the frequency & duration of a beep sound (kinda like qbasic's sound function).
Center a Form Check this code out if you want to know how to put your forms in the center of the screen.
Convert VB3 Forms to VB6 Forms This will explain how to convert 16 bit Visual Basic forms to 32 bit Visual Basic forms.
Count Lines This code shows you how to count the number of lines that are in a textbox.
Count the Times Loaded This code shows you how to count the number of times the user has used your program.
Count Words This code shows you how to count the number of words that are in a textbox.
Cut, Copy, Paste, & Undo Ever wanted to use the cut, copy, paste, & undo commands in your program? Check this out.
Disable/Enable Ctrl+Alt+Del This shows you how to use the Windows API to disable/enable ctrl+alt+del. This only works for Windows 95 and Windows 98.
Encrypt/Decrypt Text This code will let you easily encrypt/decrypt strings of text.
Flip Picture This is an example that shows you a fast way to flip a picture in a picturebox horizontally or vertically.
Font Lister This code shows you one way on how you can add a list of all the fonts on your computer to a listbox.
Form Mover This code will show you how to move a form with a label.
Get HTML Color This example shows you how to get the HTML value of a color.
Get Your Computer's Name This is some code that shows you how to get your computer's name (yep, it has a name).
Hex-Editor Related Code VB probably isn't the best language to make a hex editor with, but here are some hex type examples anyway.
HTML Color Fade This is a short example on how to fade one color into another with HTML tags.
HTML Color Fade Preview This is an example on how to preview color faded text in a picturebox.
HTML Waving Text This is an example on how to generate the HTML code for waving text.
Icons in System Tray For some reason this is requested a lot, so I thought I'd put up an example on how to do it. This code only works in Windows 95 and Windows 98.
Kill Duplicate Items in a Listbox Ever wanta get rid of all the duplicates that are in a listbox? This code shows you how.
Listbox Open An example of how to open files to a listbox.
Listbox Save An example of how to save the list inside of a listbox.
Macro Font Draw This is a code example that shows you how to create an ASCII Art Font option for an ASCII Art Shop program.
Open Default Browser This example shows you how to open up to a webpage using your default browser.
PictureBox Fade This code shows you how to fade a picturebox from one color to another.
Play Midi Shows you one way you can play a midi (*.mid) file.
Play Wav Shows you one way you can play a wav file.
Random Numbers This code shows you how to generate random numbers within a given range.
Replacing Text in a String This code shows you one way to replace text within a string.
Resize it (Form Stretch) This code lets you easily resize the controls on your form when you resize it. It creates a cool stretch effect.
Scramble Shows you how to scramble words. This code is good for something like a Scrambler game.
Screen Saver Creation This explains how to create screen savers in Visual Basic.
Score Keeper This is a KeepScore function. It's for keeping score in games like Scrambler where points are usually kept in a listbox with people's names.
Select All the List Items This shows you the fastest way to select all the items in a listbox (the Windows API way).
Set Windows Wallpaper This code shows you how to set the wall paper for Windows.
Spell Checker This is a code example that shows you how to make a spell checker for your VB programs! It works by calling up MSWord's spell checker to spell check your documents.
StayOnTop This code will make your forms so they stay on top of all of the other windows on the screen.
Tile a Pic in the Background This shows you how to tile a picture in the background of a form.
Timeout/Pause This code will allow your program to use timeout statements, giving time for certain events to happen.
VB3 Interface for VB5/6 Just when you get used to VB3's interface, they change things up on you in VB5 and VB6. This explains how to set VB5/6's interface so it acts just like VB3.
Total41 Code Examples
Click Here to Return to the Programming Section

This code shows you how to add a menu to another program.The only thing is that nothing will happen when you click on the items. To make something happen when you click on an item you have to subclass the menu (I'd help with that but I don't have any subclassing controls, or at least not right now). Put this in your *.bas file:

Then put something like this in a button:

This shows you how to set the frequency & duration of a beep sound. Put this in your *.bas file1:53 AM 8/19/2007:

Put something like this in a button:

Or, if you want to do something a little more fun, try this:

Here's how to center a form so it will appear in the middle of the screen, put this in a button:

Here's how to convert old 16 bit VB froms into 32 bit VB forms: In a 16 bit version of VB (like VB3), select 'File' from the menu, then click on 'Save File As...' A dialog box will pop up. Look for a checkbox on that dialog box that says 'Save as Text.' Make sure that is checked. Once you do that, save the file. You should now be able to use that form in 32 bit versions of VB (like VB6).

This code will count how many lines of text there are in a string by counting the number of times the'carriage return' character shows up and adding one to that. This code will not count lines of text that wrap in a textbox. Put this code in a button:

This code shows you how to count the number of times your program is used. Place this function in your *.bas file:

Example on how to use this function, put something like this in the form load event:

This code lets you count the number of words that are in a textbox (note this is very different from the len function which counts the number of characters in a string). Put these two functions in your *.bas file:

Then put something like this in a button:

Ever wanted to use the cut, copy, paste, & undo commands in your program? Now you can, check this out:

Cut: (put this in a button)

Copy: (put this in a button)

Paste: (put this in a button)

Undo: (put this in your bas)

Put this in a button:

The following code only works in Windows 95 and Windows 98. Put this in your *.bas file:

Then put this in a button to disable c+a+d:

Then put this in a button to enable c+a+d:

Below is an example of a substitution cipher. Substitution ciphers can be broken by a cryptanalyst who knows what they're doing, so this method of encryption shouldn't beused if you want your data to be 99% secure. However, substitution ciphers will keep common users from being able to seeyour data. For high levels of security look into public key encryption andAES.To continue on, copy and paste the function below into your *.bas file:

How to use this function:

To encrypt a word just put something like this in a button:

And then to unencrypt the word just call the function again!

This code demonstrates a fast way to flip a picture in a picturebox horizontally or vertically. Put this in your *.bas file:

Then put something like this in a button:

This isn't the fastest way to add all the fonts on your computer to a listbox, but it's the easiest. Put something like this in a button:

Sample Visual Basic Program Code

Copy this code into your *.bas file:

How to Use:

Put this code in the label's MouseDown event:

Put this code in the label's MouseMove event:

This is an example that shows you how to get the html color value (ie, the hex value) of a color. You will need to add theMicrosoft Common Dialog control to your project for this to work. Put this code in a button:

Did you ever want to know what your computer's name was? Not me, but anyway, if you do want to know try this... Put this code in your *.bas file:

Then put something like this in a button:

Ok, this example code will show you how to open up an exe, copy it, replace strings in the copy with strings of your choice, and then make a new program with the new strings in it.

For this to work you need to have two textboxes on a form (named: txtProgInput and txtProgOutput) and two lists (named lstOldStrings and lstNewStrings). txtProgInput contains the name of program you're editing (like:'C:somefile.exe') and txtProgOutput contains the name of the program you're making from this program. lstOldStrings contains a list of strings your replacing and lstNewStrings contains the list of what you're replacing them with. IMPORTANT NOTE: The length of the string you're replacing must be the same as the length of the string you're replacing it with, otherwise you'll get an error. Put this code in a button:

HTML color values are stored in a hexadecimal format. Each HTML color value is divided into 3 parts: the color's amount of red, green, & blue - #FF0000. The amount of red a color can have is in the range of 0 to 255 (same goes for green & blue). So if you have 255 for red and20 for blue and green, your color will be sort of redish looking.

Anyway, the following is an example on how to fade one color into another. Put this function in your *.bas file:

Then put something like this in a button:

This example shows you how to create a fade preview of color faded text inside of a picturebox. You will need to add a picturebox control named 'Picture1' to your form for this to work.Put this code in a button:

Just like the title says. Put this code in a button:

This code did not work when tested in Windows XP. It was written for Windows 95/98.

This shows you how to put an icon in the system tray. It uses an icon stored in Picture1.picture as the icon.Put this in your *.bas file:

Then put something like this in a button to add the icon to the tray:

Put something like this in a button to remove the icon:

This code loops though a list (List1) checking each item with every other item, then when it finds a duplicate it removes it. Put this code in a button:

Visual Basic 6 Code Sample

Here's another way to do it, this way is somewhat faster and uses some Windows API functions (sentin by Sopon). First put this in your *.bas file:

Then make this a function in your *.bas file:

Then put something like this in a button:

This code shows you how to open a file to a listbox. Make sure you have a CommonDialog control added to your form. Put something like this in a button:

This code shows you how to save the contents of a listbox. Make sure you have a CommonDialog control added to your form. Put something like this in a button:

AOL Macro Fonts allowed you to type in large ASCII art text, like you see below.

Visual Basic 6 Code Sample|¯¯¯-¯)::)¯¯,¯_':|¯¯¯¯¯¯¯||
:|__|¯|°:/__/'__:|¯¯|__|¯¯|
::|__|¯::|__:|/|__|':¯¯|__|¯¯

This code example shows you how to create a macro font feature for a Macro Shop / ASCII Art Shop program. It makes it so you can select a (*.pmf) file (a sample pmf file can be downloaded here), load it into a 2D array, and thenhave the output display when the user types text in an input textbox. You will need the followingfor this example to work:

txtInput - An input textbox named 'txtInput'. This is where the user types their input.
txtOutput - An output textbox named 'txtOutput'. This is where the output is displayed. This textboxshould have it's multiline property set to true and it's font type set to 'Arial' and point size set to 10.
CommonDialog1 - A common dialog control should be added to the project.

Put something like this in your *.bas file:

Put something like this in the load a font button:

And then put something like this in the Change event of txtInput:

This shows you how to open your default browser. Put this function in your *.bas file:

Then put something like this in a button:

This code will fade one color into another color in a picturebox. It's a nice tid-bit to keep in mind if you want to make your own title bar for a form. Put this in a button:

Copy this function into your *.bas file:

The following code can go in buttons to play/stop/pause/etc:

This code will be the same as described in playing a midi file with one key difference. When opening afile, use 'waveaudio' as the type. Example:

This code lets you generate a random integer within a given range. Copy this function and paste it in your *.bas file:

Example on how to use:

'x' will be equal to a random integer in the range: [10, 20] (ie, between 10 and 20, including 10 and 20)

This small example on how to use the 'Replace' function in Visual Basic 6.0. Originally this code used the Mid and Instrstring functions, but since VB 6.0 you can simply use 'Replace'.

How to use (put in button):

This code lets you resize your forms with a neat stretch effect. Copy and paste the code as directed by the comments:

Here are a neat series of functions that will allow you to scramble each word in a sentence. This code would word well for a scrambler program.Put these functions your *.bas file:

Example on how to use these functions to scramble the words in a sentence:

Here's what you do to make a screen saver: Start a new project. Set the windowstate property of the form to '2 - Maximized', and the border style of the form to zero. In the 'KeyDown' event of the form put the 'End' statement. Then in the mouse move event of the form put something like this:

Now select 'Make some_project_name.exe' in the file menu. When it asks you what you want to name the exe, add a '*.scr' extension to the end of the name (example: Blah.src). Then select the directory 'c:windowssystem' as the place to make this program. And there you go, you've made a screensaver which you can use on your desktop (note: you'll probably want to jazz up the form a little bit first).

Visual Basic 6 Sample Code

Additional Info: (Provided by Mike Clem)

Try adding 'SCRNSAVE: ' in front of the name of your screen saver when compiling if you cannot get Windows to recognize that your creation is a screen saver. Example: 'SCRNSAVE: Blah.SCR'

Problem of Windows not recognizing the Screen Saver occured in WFW 3.11 using VB 3.0.

This is a KeepScore function. It's for keeping score is games like Scramblerwhere points are usually kept in a listbox with people's names.Code written by deep arctic.

Visual basic 6 code examples

Here is an example on how you can try this function out:

This is an example that shows you how to select all of the list items in a listbox. This should work for any listbox window. Just make sure the listbox's multiselect property is setto '1 - Simple' in the properties window. Put something like this in your *.bas file:

Visual Basic 2010 Code Samples

Then put something like this in a button:

This code shows you how to set the wall paper for windows, put something like this in you *.bas file:

Then put something like this in a button or in the form_load:

Ever wanted a spell checker feature in one of your programs? Check this out, it calls up the MSWord's spell checkerso you are able to spell check your documents! You need MSWord95 or better for this code to work. This example spell checks the text in a textbox named 'Text1'.Put this in a button:

This makes a form stay on top of all other applications running in Windows. Put this code in your *.bas file:

Example on how to use (put this in the form's load event):

Example on how to remove a form from being the top most window:

This example shows you how to tile a picture in the background of a form. Put something like this in the form's load event:

This sub lets up create a pause in your programming code for whatever amount of seconds youtell it to pause for: (copy & paste is sub in your *.bas file)

Example on how to use (pause for one second):

Here's another way to do it, this way uses the Windows API (sentin by Sopon). First put this in your *.bas file:

Then make this a sub in your bas:

Example on how to use (pause for one second):

Here's how to give VB5/6 the VB3 interface: Select 'Tools'>'Options' from the menu. Then when the 'Options' form pops up select the 'Advanced' tab. Make sure the 'SDI Development Environment' checkbox is checked, and click 'OK'. When you restart VB5/6, you should have the same kind of interface that you had with VB3.

C# / .NET Code Samples
DotNetResourcesExtract v1.00
DotNetResourcesExtract is a small utility that scan dll/exe files of .NET applications, and extract all .NET embedded resources (Bitmaps, Gifs, HTML files, and so on) stored in them into the folder that you specify.
Visual C++ Code Samples And Utilities

Visual Basic 6 Code Examples With Activex

Source Code Samples
Changing the screen brightness programmingly - By using the Gamma Ramp API
Some video cards allows you to programmingly modify the Gamma Ramp values.You can use this feature to change the brightness of the entire screen.
Preventing application crash by using exception handling mechanism
When a program does something wrong, for example, reading from invalid memory address,the operating system displays a 'crash' window with information about the problem that caused the exception,and terminates the application. This sample project demonstrates how to avoidthis kind of crashes, display your own exception information window,and allow the user to continue running the application after the exception occurred.
Moving items up and down in a ListView control.
In some occasions, you might want to allow the user the change the order of items in the ListView, by moving the selected items up and down. In the API of ListView control, there is no support for swapping between 2 items. The only way to do that, is by manually swapping all data of the items, including the lParam value and all columns !
Displaying 'Browse Folders' dialog with the default folder selected.
The 'Browse For Folder' dialog allows the user to select a folder from all available local drives and network resources. This code snippet demonstrate how to display this dialog-box.
Displaying the 'Properties' of a file or folder.
When you select a file or folder in Explorer window, and choose 'Properties' from the menu, you get the properties window that contains some essential information about the file: The size of file, created date, modified date, attributes, and so on.It's possible to display this properties window programmatically, by using the ShellExecuteEx API function.
Enumerating Windows credentials with CredEnumerate function
This code sample enumerates all credentials of the current logged on user,and dump them into the standard output.
Utilities With Full Source Code
ExeInfo v1.01
The ExeInfo utility shows general information about executable files (*.exe), dynamic-link libraries (*.dll),ocx files, and drivers files.
AtNow v1.1
AtNow is a command-line utility that schedules programs and commands to run in the near future.
WinExplorer v1.30
WinExplorer is a utility that shows all system's windows in hierarchical display.
For every window in the hierarchy, you can view its properties, like handle, class name, caption, size, position and more. You can also modify some properties, like Caption and Visible/Enable.

Visual Basic Programs With Codes

Visual Basic Code Snippets and Utilities

Visual Basic 6 Source Code Samples

In the following section, you can find a variety of Visual Basic code samples. For every sample, you can download the entire project for opening in Visual Basic 6.0.

Revealing the passwords behind asterisks in Internet Explorer
This small utility reveals the passwords stored behind the asterisks in the web pages of Internet Explorer 5.0 and above.
Creating a real console application in Visual Basic !
Although the Visual Basic compiler doesn't allow officially to create console applications, it's possible to do it with a simple trick.
Read more >>
Revealing the passwords behind asterisks
This code snippet scans all opened windows, and reveals the passwordsbehind the password text-boxes.
Access 95/97 Password Revealer
This small utility reveals the database password of Microsoft Access files.
It works only with version 95 and 97 of Microsoft Access.
System resources in Windows 95/98
This code snippet shows the percentages of free system resources in Windows 95 and Windows 98.
Fast string concatenation
With this class, you can concatenate small strings into one big string in faster way than the standard '&' operator.
Using bidi32.dll for Logical-Visual conversion
This code snippet is designated for using in bidirectional systems only (Hebrew and Arabic).
It uses the functions in bidi32.dll to convert strings from Visual to Logical and from Logical to Visual.
Painting a Gradient between 2 colors on a form
This code snippet shows how to paint a gradient on a form using Win32 API.
Drawing a rotated text on a form
This code snippet demonstrate how to draw a rotated text on a form.
Creating a circle shaped window
This code snippet demonstrates how to create a circle shaped window, by using a few Win32 API calls.
Web client tester
With this small utility, you can send HTTP queries and see how the servers responds to those queries.
Faster items adding in ComboBox
This code snippet shows you how to add combo items with Win32 API. It's much faster than using the AddItem method of VB.
Faster items adding in ComboBox (with item data)
This code snippet shows you how to add combo items (both strings and items data) with Win32 API. It's much faster than using the AddItem method of VB.
Executing Control Panel applets
This code snippet shows how to run standard Control Panel applets (Display Properties, Reginal Settings, Mouse Properties, and more)from Visual Basic code.
Converting network drive-based path to universal path name
This code snippet shows how to use the WNetGetUniversalName for convertingnetwork drive-based path (Like I:windows) to universal path name (Like MyComputercwindows)
Opening special Explorer windows
This code snippet shows you how to open special Explorer windows, like My Computer, Control Panel, Printers, Fonts and more...
DoEvents alternative function
This code snippet shows how to create an alternative function for the standard DoEvents function.
Registering and using a new window class
This code snippet shows you how to register a new window class in Visual Basic anduse that class to create new windows, and show them on a form.
Displaying and using a dialog box from external DLL file
This code sample shows you how to display a dialog box from external DLL file.In this sample, the standard 'Run' dialog of Microsoft Windows is shown on the screen.
Displaying the 'Shut Down Windows' dialog
This code snippet displays the 'Shut Down Windows' by using undocumented API in shell32.dll
Getting the filename of an ActiveX class name