问题
I have a Database in Access 2003 that needs to work with foreign language characters. The characters show up fine in the table. However, when VBA tries to read them it cannot do it.
As an example, the column ANSWER_TEXT from cf_Answer displays: 佛吉尼亞海灘
But a select statement in VBA:
sqlstmt = "SELECT ANSWER_TEXT AS ans_text FROM cf_Answer"
Set rst_a = dbs.OpenRecordset(sqlstmt, dbOpenSnapshot)
rst_a![ans_text]
returns ??????
.
I know this has something to do with UTF-8 encoding but I cannot find a way to set it. Is there a way to set it in the table? Currently, ANSWER_TEXT is of datatype memo
. Or perhaps there is a way to set VBA to understand those characters?
Can anyone at least point me in the right direction?
回答1:
The issue may be due to where you're displaying that unicode text.
I pasted those same characters into a text field in my table. Retrieving them with DLookup
in the Immediate window causes them to be displayed as question marks because the Immediate window does not display unicode as you wish.
? DLookup("some_text", "tblFoo", "id = 1")
??????
A MsgBox
also displays them as question marks.
MsgBox DLookup("some_text", "tblFoo", "id = 1")
However a form text box control does have the capability to handle unicode properly. Binding the text box to the field which contains those characters gives me this ...
A query can also reference unicode characters, and this uses one in its WHERE
clause and displays them all correctly when the query is opened in Datasheet View.
SELECT f.id, f.some_text
FROM tblFoo AS f
WHERE (((f.some_text) Like '佛*'));
I suspect this all comes down to how you're trying to use those unicode characters and where you're displaying them.
In a comment, you stated writing those unicode characters to a text file would produce only question marks. However, if you write unicode to a text file (as in the procedure below) and display the file in an editor which is capable of handling unicode correctly, you will see the same characters you see in Datasheet View of the table where they are stored. This screenshot shows Wordpad opened with the file which was created from the code below.
Dim objFso As Scripting.FileSystemObject
Dim objFile As Scripting.TextStream
Set objFso = New Scripting.FileSystemObject
Set objFile = objFso.OpenTextFile(CurrentProject.Path & _
Chr(92) & "unicode.txt", ForWriting, True, TristateTrue)
objFile.Write DLookup("some_text", "tblFoo", "id = 1")
objFile.Close
Set objFile = Nothing
Set objFso = Nothing
来源:https://stackoverflow.com/questions/15910166/foreign-characters-show-up-as-in-vba-2003-how-to-set-up-utf-8