Post by MikeDAll these answers with Val, Trim, Str, CStr, etc. What's wrong with this?
strtext = Format$(txtopid.Text, "####")
Nothing, though it's probably the slowest way of doing it (not that it's
likely to matter if you're doing this on a data entry form that's probably
updated a lot less than a million or so times per second <g>).
My results, based on the code below (in sec.):
Format: 12.41
Trim/Str/Val: 11.52
CStr/Val: 7.50
CStr/CDbl: 4.41
CStr/CInt: 2.66
CStr/CLng: 2.39
Public Sub TestIt()
Const NumIter As Long = 5000000
Const StringNum As String = "0999"
'Used a constant value, to focus times on the methods involved, rather
'than extraneous code. Times seem to vary a bit based on the string
'you use, but not significantly.
Dim i As Long
Dim s As String
Dim t As Double
t = Timer
For i = 1 To NumIter
s = Format(StringNum, "####")
Next
Debug.Print "Format: " & Format(Timer - t, "0.00")
t = Timer
For i = 1 To NumIter
s = Trim(Str(Val(StringNum)))
Next
Debug.Print "Trim/Str/Val: " & Format(Timer - t, "0.00")
t = Timer
For i = 1 To NumIter
s = CStr(Val(StringNum))
Next
Debug.Print "CStr/Val: " & Format(Timer - t, "0.00")
t = Timer
For i = 1 To NumIter
s = CStr(CDbl(StringNum))
Next
Debug.Print "CStr/CDbl: " & Format(Timer - t, "0.00")
t = Timer
For i = 1 To NumIter
s = CStr(CInt(StringNum))
Next
Debug.Print "CStr/CInt: " & Format(Timer - t, "0.00")
t = Timer
For i = 1 To NumIter
s = CStr(CLng(StringNum))
Next
Debug.Print "CStr/CLng: " & Format(Timer - t, "0.00")
End Function