CStr() vs Str() - vb6

I have to modify some old VB6 code, but I've been using VB.Net for so long I have forgotten some of the subtleties, and documentation is getting harder to find.
In Vb.Net CStr() takes into account international settings and uses commas instead of dots for people with foreign language settings whereas Str() always uses English conventions. I can't remember though if this was the same in VB6.

OK, tested it by changing my locale settings, and the following code..
Yes it seems that CSTR() is locale aware, and STR() isnt..
Private Sub Form_Load()
Dim X As String
Dim Z As Currency
Z = 456789.12
X = CStr(Z) 'with locale settings of sep='.' and dec=',' prints 456789,12
'X = Str(Z) 'with locale settings of sep='.' and dec=',' prints 456789.12
Me.Caption = X
End Sub

Related

Microsoft Access isnumeric system language issue

We are having an issue with Microsoft access and a function called " isnumeric ". When running our software(it's using Access) on a English Windows, 12.2 is isnumeric = false , but on a Swedish Windows 12.2 isnumeric = true.
I am by no means a developer, I'm just trying to find out why this problem occur, since one of our developers is running into this issue right now.
First of all: IsNumeric() is locale aware, keep that in mind when developing for an international market. E.g. Debug.Print IsNumeric("$12.2") returns False for me, whereas Debug.Print IsNumeric("€12.2") returns True.
That said, I can see two possibilities for this to happen: 1) the regional settings on the English Windows have been edited or 2) you're using a self-written isnumber method.
When you create a public method with the same name as an intrinsic method, your method takes precedence over VB's method. If you now want to use VB's method instead of your own, you need to prefix that with its namespace, which in the case of IsNumeric is VBA: VBA.IsNumeric.

Classic ASP language (monthname for chinese)

Have been struggling to find a solution for this.
I am looking for monthname to return the chinese version of January (or spanish etc.). At the moment it is returning english.
The site is installed on an english windows machine.
I have tried adding session.lcid as well as: <%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
but I keep getting the english version of the month name (and to change each occurrence is a major code change). Also I need to try and keep the date format as per what I have as changing that might be an issue as well.
Can anyone recommend an answer? Much appreciated.
You need the SetLocale function and the locale identifier of the language.
Sample test vbs script
Option Explicit
Dim language, outputString
For Each language In Array( "es-es", "en-us", "zh" )
SetLocale language
outputString = outputString & MonthName( 2 ) & vbCrLf
Next
WScript.Echo outputString
Output

Converting a byte array with system locale set to Japanese in VB6

In my legacy VB6 application I'm receiving a zip file as a byte array from a webservice. This byte array is converted to a string using the StrConv function and stored on the file system.
Dim arr() As Byte
Dim sUnicode as String
nFile = FreeFile
arr = objHTTP.responseBody
sUnicode = StrConv(arr, vbUnicode)
Open sFile For Output As #nFile
Print #nFile, sUnicode
Close #nFile
So far so good, this has worked correctly for over a decade.
Now the application is used in Japan as well and the code above leads to a corrupt zip file.
I already found out that the issue is related to the Japanese system locale on the target system.
I tried passing the locale id 1033 to the StrConv function
StrConv(arr, vbUnicode, 1033)
Next I tried implementing the solution as descibed by this link
Encoding of Text Files in VB 6.0
Also I tried changing the system locale using the 'SetLocaleInfo' api-call.
None of the attempts have lead to a valid zip file on a OS with the system locale set to Japanese.
Does anybody know how to get a working solution?
Thanks in advance,
Jos
You should avoid the string conversion entirely. Try something like this:
Dim arr() As Byte
nFile = FreeFile
arr = objHTTP.responseBody
Open sFile For Binary As #nFile
Put #nFile, , arr
Close #nFile
This writes the contents of your array directly to the file.

how to format the specified string as bold and set size in testcomplete using VB scripting language inj testcomplete

I am trying to set the format for a particular string as bold and font size and colorindex.i am using VB script in testcomplete tool.can you please suggest me which method is used for that
sub sample
mystring="welcome to testcomplete"
aqstring.format(mystring)=Bold
mystring.Font.ColorIndex=10
mystring.Font.size=14
end sub
here i am getting vb script run time error..please help me which method i have to use..give some examples
Well for it to add parameters on top of the string, the string needs to be attached to an object, for example, a log parameter below. A log parameter can support font styles because it is an object that does have more properties than text and length(like strings). In this case, a simple string cannot support font styles.
Sub EventControl1_OnUnexpectedWindow(Sender, Window, LogParams)
LogParams.MessageText = "An unexpected window has appeared."
' Specifies the string that will be posted to the Additional Info panel
LogParams.FontStyle.Bold = True
LogParams.FontColor = clSilver
LogParams.Color = clFuchsia
End Sub
Here is the complete directory on string manipulation -> Click Here
You'll notice that there are no articles on string formatting and colors, that's because a string is a scripting tool, and not an object that can support formatting.

VB6 control iteration: one control gives me an error; which one?

I am trying to loop through all controls in a form:
For Each ctrl in Me.Controls
in order enable/disable the control based on some conditions.
But there is a control on the form that gives an error when I try to access it. What kind of control does that, and how do I find it?
When you get your error and click Debug, is the error on the line setting a control's Enabled property?
If so, add a Debug.Print statement writing out the control's name. Do so on the line before setting the Enabled property.
Here's what I mean:
Dim ctrl As Control
For Each ctrl In Me.Controls
Debug.Print ctrl.Name
ctrl.Enabled = True
Next
The Debug.Print statement will write out to the Immediate Window the name of the control that was last processed in the loop, presumably the one that caused your error.
EDIT
This might work. Put this control in a Panel control and set the Panel's Enabled property to False. If I recall correctly, in VB6 setting a container control's Enabled property to False will also set the container's child controls Enabled to False. If your control's Enabled property really is read-only, I'm curious what would happen.
Try this:
Dim ctr As Control
Dim CtrStatus Boolean
CtrStatus = False
For Each ctr In Me.Controls
If (SSTab.hwnd = GetParent(ctr.hwnd)) Then
Call CallByName(ctr, "Enabled", VbLet, CtrStatus)
else
ctr.Enabled = CtrStatus
End If
Next
Another approach is as follows, that also works at runtime (as opposed to just in the IDE):
private sub SetEnabled()
on error goto errHandle
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = True
Next
exitPoint:
exit sub
errHandle:
MsgBox "Error " & err.Description & " with Control " & ctrl.Name
resume exitPoint
end sub
Suppress the error reports before the loop and then set it back to standard error handling:
On Error Resume Next
For Each ctrl In Me.Controls
ctrl.Enabled = lIsEnabled
Next
On Error GoTo 0
OR name your controls with a standard prefix/suffix that you can check by name and skip in the loop.
For Each ctrl In Me.Controls
If Left(ctrl.Name, 3) = "sst" Then
ctrl.Enabled = lIsEnabled
End If
Next
Tosa: from your comment on AngryHacker's answer, I think you are checking the container incorrectly.
Your code is like this
' BAD CODE '
If ctrl.Container = fraMovies Then
For me that gives error 450 Wrong number of arguments or invalid property assignment. Do you get the same error?
The code should use Is rather than =, like this
' GOOD CODE '
If ctrl.Container Is fraMovies Then
Explanation. You want to check whether two variables "point" to the same control. Controls are objects: you must use Is not = to check whether two object variables "point" to the same object. This is a classic pitfall in VB6.
One last word. Next time, could you try to post 10 lines or less of actual code, reproducing the error, and give the exact error number and message and the exact line on which it occurs? It really does make it much easier for us to solve your problem - I know it's work for you to shorten the code, but you'll get better answers that way.
EDIT Welcome back! :) You said some controls don't have a Container property. You could try wrapping the test in On Error Resume Next, something like this.
' GOOD CODE '
Dim bMatch As Boolean
Dim ctrl As Control
For Each ctrl In Me.Controls
bMatch = False
On Error Resume Next
bMatch = (ctrl.Container Is fraMovies)
On Error Goto 0
If bMatch Then
ctrl.Enabled = True
End If
Next
To avoid such problems follow the given rules while naming contols
When you name an element in your Visual Basic application, the first character of that name must be an alphabetic character or an underscore.
**Begin each separate word in a name with a capital letter, as in FindLastRecord and RedrawMyForm.
Begin function and method names with a verb, as in InitNameArray or CloseDialog.
Begin class, structure, module, and property names with a noun, as in EmployeeName or CarAccessory.
Begin interface names with the prefix "I", followed by a noun or a noun phrase, like IComponent, or with an adjective describing the interface's behavior, like IPersistable. Do not use the underscore, and use abbreviations sparingly, because abbreviations can cause confusion.
Begin event handler names with a noun describing the type of event followed by the "EventHandler" suffix, as in "MouseEventHandler".
In names of event argument classes, include the "EventArgs" suffix.
If an event has a concept of "before" or "after," use a suffix in present or past tense, as in "ControlAdd" or "ControlAdded".
For long or frequently used terms, use abbreviations to keep name lengths reasonable, for example, "HTML", instead of "Hypertext Markup Language". In general, variable names greater than 32 characters are difficult to read on a monitor set to a low resolution. Also, make sure your abbreviations are consistent throughout the entire application. Randomly switching in a project between "HTML" and "Hypertext Markup Language" can lead to confusion.
Avoid using names in an inner scope that are the same as names in an outer scope. Errors can result if the wrong variable is accessed. If a conflict occurs between a variable and the keyword of the same name, you must identify the keyword by preceding it with the appropriate type library. For example, if you have a variable called Date, you can use the intrinsic Date function only by calling DateTime.Date.

Resources