What's your favourite function? Whether it's Kill or FileCopy, Replace or Round, if it's useful, it's in our exclusive three part series on the grooviest intrinsic Visual Basic features. Definitely one to print out for the office notice board.
Every now and then, we all stumble across a neat, intrinsic Visual Basic function.
Perhaps you discover a sparkling gem inside that 2000-page programming manual. Or maybe you uncover some little-known feature during a casual stroll through the help files.
Still, no matter how you meet each other, the latest research has found 99.8% of developers go through a five minute function cycle of being "very impressed" to "still quite impressed" to "what was that thing called?".
So, as a result of the findings of that exclusive VB-World commissioned survey, we decided to compile this three-part series dedicated to that 99.8%. Thankfully it was put together by the other 0.2% that managed to keep a record of their cool code findings.
Don't forget that nothing in this series will require you to add extra components to your project. These are all little-known, intrinsic features available to you right now or tiny code snippets that you can easily slot into your project.
The series is split into three separate parts Beginning, Intermediate and Advanced. None of this stuff is difficult indeed, many are simple one-liners but certain features are more suited to particular skill levels.
If you're already well acquainted with Visual Basic, you may want to start at the Intermediate level and work upwards, but again - it all depends on your experience.
I hope you enjoy this special feature and would to thank all the many contributors that have helped make it possible. Credits appear underneath each snippet.
And hey if you've got a cool function you'd like to add, use the Bulletin Board link below!
And don't forget someone famous once said, "Even if on your journey you uncover but one new land, has it not been worth the investment? Yes, for the garden of thy mind now bears another seed upon which one day the sun may shine and the intelligence blossom".
Yeah. Good point.
Note: All functions have been tested to work with Visual Basic versions 5 and 6, unless where stated.
You can check if a string or variant is a number by using the IsNumeric() function. Here's how it works:
If IsNumeric(Text1.Text) = True Then MsgBox("That's a number!")Else MsgBox("That is NOT a number!")End If
You can use IsDate() in much the same way.
-- "Micro" Maria, Ohio.
Remove Redundant Spaces
The Trim() function is one of my favourites. It removes all the leading and trailing spaces within a string. It goes like this:
MsgBox Trim( _ " this is my string with surrounding spaces ")
And it returns "this is my string with surrounding spaces", minus the excess baggage! Very useful indeed.
-- James Matthews, an MCP from Down Under.
Is your Program Running Twice?
Check out the PrevInstance property of the intrinsic App object to check if an instance of your program is currently running. Here's a code snippet:
If App.PrevInstance = True Then MsgBox "Application already running!" EndEnd If
-- Peter Potter, Anonymous-by-the-Sea
You can use the Val() function to extract numbers from a string, like this:
MsgBox Val("40 years old")
This returns the number 40. Basically, if it sees a number at the beginning of the string, it carries on and grabs it all. If it finds no number at the beginning, it returns 0.
-- Ben Davis, Teenage Programmer from Istanbul.
I don't know about you, but it seems strange how few people really use the Format() function. It is very powerful. I particularly like the way it handles dates:
MsgBox Format(Date, "dddd dd mmmm yyyy")
This returns "Monday 03 April 2000", or whatever the current date is. Here are the extra options you can insert in the second argument:
- dddd the day, ie Monday
- ddd the short day name, ie Mon
- dd two-digit day number, ie 05
- d day number, ie 5 or 14
- mmmm the month, ie October
- mmm the short month name, ie Oct
- yyyy the year, ie 2000
- yy the short year, ie 00
- dd/mm/yyyy plain date, ie. 01/03/2000
You can use the following when the first parameter passed is the Time() function, instead of Date():
- am/pm returns am or pm
- hh returns the two-digit hour number, ie 04
- h returns the hour number, ie 4 or 11
- mm - returns the two-digit minute, ie 08 or 32
- m return the minute, ie 8 or 32
- ss returns the two-digit second, ie 04 or 54
- s returns the second, ie 4 or 54
- hh:mm:ss returns a typical time, ie 04:59:03
You can also format numbers and so on using this cool command for instance, you can insert commas at the thousand markers and so on. Check out the Help for more information.
Hope someone finds this useful!
-- Jack Croydon, London.
Ending your Program
If you want to end your program at any time, just type 'End' in your code!
-- Ben Davis (again).
Resetting the Combo Box
If you want to reset the value of a combo box to nothing, try setting its ListIndex property to 1.
-- Ben Davis (yet again ;-).
You can replace text in Visual Basic 6 by using the new Replace() function.
You can use this as such:
Replace(YourString, StringToFind, StringToReplaceItWith)
Here's an example:
MsgBox Replace("This is my test string", "string", "")
This returns the string "This is my test ". Here's another one:
MsgBox Replace("Visual Basic is cool", "cool", "very cool")
This returns "Visual Basic is very cool".
Those with older versions of Visual Basic can use the version of Replace here (http://killervb.com/code/commonbas/Replace.htm.
Dealing with Fonts and Printers
If you want the user to select from a list of fonts, forget API calls and all that rubbish. Remember that the intrinsic 'Printer' object includes a Fonts property. Try adding a ListBox to your Form, then running the following code:
For i = 0 To Printer.FontCount - 1 List1.AddItem Printer.Fonts(i)Next I
You should get a list of all the fonts on your system. You can then set the Font of a Text Box by running something like:
Text1.FontName = List1.Text
In fact, talking about the Printer object don't forget that if you're doing a little basic printing, you can handle it all from within Visual Basic so no need for exporting to Word, HTML files and so on. For more information, look up the 'Printer object' in the Help files. Hope you guys will find this useful!
-- Helen Doubtfire, VB Queen.
Rounding a Number (VB6 Only)
A new function in Visual Basic 6 allows you to round a number up or down.
You can use it like this:
MsgBox Round(Number, NumberOfDigitsAfterDecimalPoint)
MsgBox Round(3.5, 0)
Returns 4. Beware that:
MsgBox Round(3.45, 1)
Returns 3.4, which is clearly incorrect. So the Round function is buggy beyond 1 digit after the decimal point. You have been warned!
-- Val Benetton.
Reverse a String (VB6 Only)
You can reverse a string in Visual Basic 6 using the StrReverse() function.
Here's an example:
This returns "!olleH".
-- MiniMonkey from Somewhere Touching the Big Pond.
That's all for this week - surf back to VB-World next week when I'll be uncovering another array of little known, but oh-so-useful, Visual Basic tips and tricks.