Welcome to the second part of our Favourite Functions feature!
A big thanks to all the wonderfully intelligent developers out there that have invested their precious time into compiling this exciting collection.
Yessiree, this three-part series lists all the latest and greatest features immediately available to your Visual Basic project, no extra components required!
This week, we’re covering everything from the clipboard to file operations, correct capitalisation to directory creation it’s all here.
Oh, why do I bother writing this introduction? Nobody ever reads it. Let’s just get on with the show…
Note: If you’ve missed part one of this series, check it out here.
Capitalise First Letters
One of my favourite functions is StrConv(). You can use it to capitalise the first letter of a group of words, ideal for properly formatting names. You use it like this:
MsgBox StrConv("jonathan plaza", vbProperCase)
This function will return ‘Jonathan Plaza’. But be wary of names such as McDonald, O’Brien and other anomalies it won’t handle those properly!
— Jonathan Plaza, living in VB-World 😉
Sending Keystrokes to Other Programs
Here’s a nice little trick to try. Open up WordPad, which comes with Windows 95/98 (in the Accessories menu). Notice that it’s title is "Document WordPad".
Now run this code in Visual Basic:
AppActivate ("Document - WordPad")SendKeys ("Hello from Visual Basic!")
The AppActivate function brings the application with that title to the ‘front’ of your screen. The SendKeys function just acts as though you were typing the string you pass it so in other words, ‘Hello from Visual Basic!’ gets tapped into WordPad.
For more information on the various SendKeys commands, look the method up in Visual Basic help.
— Adam "AppActivate" Alanson.
Setting Tab Orders
When you press tab on a form, the focus moves from one control to the next. To change the order of this, you alter the TabIndex control of each individual control.
Here’s a great tip for getting through all your controls with minimal effort start with the very last control you want to have the focus and work backwards, setting the TabIndex property of each to 0.
You’ll find that Visual Basic automatically renumbers each control for you and all your horrid tab problems are solved!
— Paul Simpson, Manchester.
Use the Clipboard
You can easily implement clipboard functionality in your application using the inherent Clipboard object. Here’s a simple example of its usage:
Clipboard.SetText("My Text Clipboard Text")MsgBox Clipboard.GetText
Simple, yes – but powerful!
— The Mysterious AppMan, Hotmail.
File Sizes
To find out the size of a file, in bytes, use the FileLen() function. Here’s an example:
MsgBox FileLen("c:autoexec.bat")
That’s it!
— Mysterious AppMan’s younger brother, Colin.
File Operations
Did you know Visual Basic provides complete support for all basic file operations? Here’s my top three file handling code snippets:
Copy a File:
FileCopy("c:oldfile.txt", "c:newfile.txt")
Delete a File
Kill("c:oldfile.txt")
Move a File
Name "c:oldfile.txt" as "c:newfile.txt"
Hah, who needs the API?
— Danny Weidman, Cardiff.
Get a File Date and Time
You can use the FileDateTime() function to retrieve details of when a file was last modified.
— David Homer, Canada.
Check if a File Exists
Here’s a groovy little function I wrote to check whether a file actually exists:
Public Function FileExists(FileName As String) As Boolean If Dir(FileName) = "" Then FileExists = False Else FileExists = True End IfEnd Function
You use it like this:
If FileExists("c:nofilehere.txt") = False Then _MsgBox ("File does not exist!")
— Barry Manilow, Copacobana (North of Havana).
Reading and Writing the Registry
You can read and write to the registry using SaveSetting() and GetSetting().
Here’s an example of writing:
Call SaveSetting(App.Title, "UserDetails", _"Name", "Micky")
And reading:
MsgBox GetSetting(App.Title, "UserDetails", "Name")
Here’s the syntax for each method:
Call SaveSetting(ApplicationName, SectionName, _KeyName, Value)
MsgBox GetSetting(ApplicationName, SectionName, _KeyName[, DefaultValue])
You can delve into the registry and view your stored values by running regedit.exe but do so with caution and be careful not to delete anything you shouldn’t!
— Anne Keitgen, Germany.
Create a Directory
Did you know Visual Basic has a built-in command to create a directory? For instance, to create the directory ‘vbworld’, run this:
MkDir ("c:vbworld")
— Dr *.DOS, Hotmail.
Neat Code Tips
Hey fellow VB guys and gals, don’t forget that you don’t always have to start a new line to run a separate line of code just use the colon (:).
For example, this is interpreted as two lines of code yet takes up less room in the code window. It’s great for compacting If-Then-Else statements, etc.
MsgBox "Happy Birthday": MsgBox "To Me"
— Brad Windsor, Pittsburgh.
Well, that concludes this second selection of top code tips and tricks – check back next week for our third and final instalment!