To convert any number to a string, you can use the Str Function. The Str Function uses the following syntax
Str$(Number)
**** Note the $ that has been appended to the Str function name (and many other string functions) You do not have to do this, but if you don't, it will return a Variant rather than a string... which is another VB behavour I can't understand, as we are specifically wanting a string! VB will automatically convert this to a string, but at a performance cost (not noticable if you just do it once, but it is a good habit to get into!). You can see our Performance article for more info. ***
The following code converts the number contained in lNum, and puts it into a textbox
Dim lNum As Long
lNum = 1000
Text1.Text = Str$(lNum)
In many cases it is not necessary to convert a number to a string, as VB automatically does this for you, however it is a good practice to get into, and it is also slightly faster using the Str function, instead of letting VB do it for you. You can also use the Hex command to convert a number to a Hex string. To do this properly, use the following code. This makes all the letters lowercase, and then adds &H on to the beginning, making it a valid Hex string
strHexString = "H&" & LCase$(Hex$(lNumber))
Comments