how can i use Unicode in Visual basic  6.0 plz tell me an example

Posted on 2003-03-12
Medium Priority
Last Modified: 2007-12-19
plz tell me how can i use unicode in vb 6.0
Question by:ibrarsahel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2

Accepted Solution

yronnen earned 500 total points
ID: 8117871
VB6 store strings internally as unicode. You do not have to do anything.

If you have an ANSI string that you want to convert to unicode, you can do it this way:
dim m() as byte

for more info check msdn for "StrConv Function"

Expert Comment

ID: 8117877
Actually, the demo code will not work, since the "hello" string is already unicode. If you load text from a database or a file and this text is ANSI, you can use the function strconv.

Assisted Solution

VBtom earned 500 total points
ID: 8137312

It's true that VB6 stores string as unicode.
You use AscW$() for getting a unicode number of a character, and ChrW$() for producing a unicode character (if you cannot type it: the VB editor works in ANSI).
For example, if you want that a string contains "Athína" in Greek characters, you say:
MyStringVariable =  ChrW$(913) & ChrW$(952) & ChrW$(942) & ChrW$(957) & ChrW$(945)

Asc$() and chr$ are for ANSI (0..255).

If you have ANSI text, e.g. from a text file or an access97 database, you could use StrConv to convert it, but it's done automatically when you assign it to a string variable.

So working with unicode in VB is no problem.

But the big problem is displaying unicode characters when they're not in the standard ANSI set:
VB controls like textboxes only show ANSI-characters (0..255) - (I'm not sure,  might be DBCS as well, but that counts for the same)
When you assign a string to the .Text or .Caption of a VB control, all unicode information gets lost and unicode characters are converted to ANSI characters using the default codepage of the OS.
This means e.g. that accents get lost, you get another character or you see a square.

For displaying other charcters, you need a special font that has the desired characters in the 0..255 charset.
If you don't have such font, it should be easy to find a free font on the internet that can do see.
But 2 problems remain:
-if you want to display for instance both west-european characters in the standard ANSI set (like é ñ ü), polish characters and greek characters, you need 3 different fonts (a standard ansi font, a special CE font and a special Greek font).
-as far as I found out, there seems no way to convert unicode to another than the standard charset (may be there exists some api function for that). This means that the automatic conversion does not work for displaying the characters even if you use an appropriate font.
I once had to display polish characters from an excel file (unicode) in a vb textbox and the other way. The way I solved it, was:
-I found out the numbers of the special characters in unicode (Word is useful for that),  and the numbers in a special font for them.
-then I made two functions UnicodeToCE and CEToUnicode: these examine the numbers of all characters in the string; if it's one of the special charcters, it's replaced by the right one.
For example, the polish "l" with a line through is unicode nr. 322 and  has number 179 in a special CE font,  so depending on the direction chrw$(322) is changed to chr$(179), or the other way.
-Then I passed all text that had to go from excel to the textbox or the other way through my functions.

Maybe not elegant but it worked and I don't know if there's a better solution.

Hope this helps you.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8143239
I forgot to say some important things about unicode and vb6:

-Whether you'll have problems depends of the languages used: the standard ansi charset contains all special characters needed for westeuropean languages: Scandinavian languages, German, Dutch, French, Spanish, Portuguese and Italian. So you won't have problems with these at all.

-You can use AscW() /ChrW() without $ as well, it doesn't always work with $

-There IS one control in VB6 that can display unicode charcters correctly: a richtextbox with the font set to Arial Unicode MS. It can receive unicode characters correctly through copy and paste or OLE-drag and drop.
However, it's not possible to get the unicode number of a character in the .Text of it: you'll always get 0..255 numbers (this makes me suspect that it should be possible to get the 0..255 numbers in special fonts of unicode characters through some api-function). Neither is it possible to add a unicode string  composed with chrw() to it correcty by saying RichTextbox1.Text = Richtextbox1.Text & MyUnicodeString. I don't know how you can without using clipboard or oledata.

Note: if you need to find out unicode and 0..255 numbers of certain characters, post a comment, then i can explain you how.

LVL 22

Assisted Solution

danaseaman earned 500 total points
ID: 8595836
The easiest way to use Unicode with Vb is to use the Unicode aware controls from FM20.Dll(Forms 2.0 Object Library). This DLL is part of MS Office and is non-redistributable but you can download it free from MS as part of SetupPad.Exe.

Put a Forms 2.0 Textbox control on your form and test it with this code:

Private Sub Form_Load()
   TextBox1 = "Chinese" & StrConv("ÿþsS`Oegُ?Q„vîv„v", vbFromUnicode)
End Sub

You can also copy Unicode from Offce and other apps into the clipboard and paste them into this TextBox.

Expert Comment

ID: 8595963
Hi danaseaman,

I know that the "userform"-textboxes (like in Word en Excel) can display unicode text without problem.
But I have 2 questions about that:
*what about the distribution rights if you make a VB app that uses that?
*how can I put such a textbox on a VB form without getting system error &H80070485?
LVL 22

Expert Comment

ID: 8596339
Since you can't distribute FM20.Dll with your package the user must have MSOffice installed. If the user doesn't have Office installed then he/she can download and install the free SetupPad.Exe from MS at the link in my previous post.

Unable to put  textbox on a VB form without getting system error &H80070485...
Go to this link for info on system error. I found that FMENU20.Dll  must also be present on your system or VB will not let you put the textbox on a VB form! This occurs on non-English Office installations. SetupPad.Exe should include this extra DLL.

If you are using 2000/XP and it was installed with FarEast(Asia) support you should see the Unicode in the Textbox. You can add FarEast(Asia) support via the control panel. On 98/ME makes sure you have a Unicode Font installed such as Arial Unicode and set the TextBox to that Font.

I'm looking for a Unicode aware textbox(free) so that I don't need the extra dependency. The alternative is to write an all API TextBox yourself with Unicode support using CreatWindowExW.

Screenshots of a working Unicode aware project can be seen on my site at:
Click on "Coming Soon"
LVL 22

Expert Comment

ID: 8596465

Expert Comment

ID: 8601813
thank you very much for the anwers to my questions about your suggestion.
I have a dutch office and fmenu20.dll was missing indeed.
btw, a useful article about writing/reading unicode in textboxes using api's is http://www.fawcette.com/Archives/premier/mgznarch/vbpj/1999/08aug99/bb0899.pdf

LVL 22

Expert Comment

ID: 8603934
I've read that article before. It suggests using Windowless controls as an alternative to work with Unicode but I can't get it to work (I still get "??????" in lieu of Unicode) using XPPro/Sp1. If I find a free alternative other than  FM20.Dll(Forms 2.0 Object Library) I will post it here.
LVL 29

Assisted Solution

leonstryker earned 500 total points
ID: 9234122
I needed to place some Greek text in regular labels  This is the function i used to do so:

Function ConvText(lblLabel As Object, strString As String, strLang As String, _
                        Optional blnBold As Boolean = False, _
                        Optional intFontSize As Integer = 8) As Boolean
'Procedure Name: ConvText
'       Purpose: Converts Unicode text so it can be displayed in a label
'    Parameters: lblLabel       -   Label used to display the text
'                strString      -   Text to be converted
'                strLang        -   Language to be converted
'                blnBold        -   Optional font setting to bold
'                intFontSize    -   Optional font setting size
Dim strSQL As String, rsRecSet As ADODB.Recordset
Dim arrUnicode() As Byte, intCharSet As Integer, strFont As String, intLen As Integer
Dim intIndex As Long

    ConvText = True
'    '/ Open connection to Oracle DB
'    If Not OpenConnOracle(cnConn) Then Exit Function
    '/ Get language specific information
    strSQL = "SELECT * FROM Lang WHERE TLANGDSC = '" & UCase(strLang) & "'"
    If Not OpenRecSet(strSQL, cnConn, rsRecSet) Then Exit Function
    If Not rsRecSet.EOF Then
        intCharSet = rsRecSet("nCharSet")
        If intCharSet = 0 Then
            GoTo EXIT_FUNCTION:
        End If
        strFont = rsRecSet("tFont")
        '/ Close language specific record set
        CloseRecSet rsRecSet
    End If
    '/ Parse string into byte array
    arrUnicode() = strString
    intLen = Len(strString)
    '/ Set label settings
    With lblLabel
        .Caption = ""
        .Font.Bold = blnBold
        .Font.Name = strFont
        .Font.Size = intFontSize
        .Font.Charset = intCharSet      '/ 161 for Greek
    End With
    '/ Convert by character, skip every other one for unicode characters, but not standard ones
    For intIndex = 0 To intLen * 2 - 1
        If ((intIndex + 1) Mod 2) <> 0 Then
            Select Case arrUnicode(intIndex)
                Case Is < 128                   '/ English text
                    lblLabel.Caption = lblLabel.Caption & Chr$(arrUnicode(intIndex))
                Case Else
                    '/ Other hex numbers may be needed for other languages &H30 works for Greek
                    lblLabel.Caption = lblLabel.Caption & Chr$(arrUnicode(intIndex) + &H30) & ""
            End Select
        End If
    Next intIndex
    Exit Function
    Debug.Print Err.Number & " : " & Err.Description
    ConvText = False
End Function

Expert Comment

ID: 9447197
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
LVL 29

Expert Comment

ID: 9461955
It is very hard to recomend anything then the original question is so general.  I think it should probably be deleted.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question