Lỗi UNICODE tiếng việt khi copy từ Recordset

  • Thread starter Thread starter itqn2004
  • Ngày gửi Ngày gửi
Liên hệ QC

itqn2004

Thành viên mới
Tham gia
1/5/20
Bài viết
3
Được thích
0
Hello all,
mình mới lập trình VBA exel. Đang gặp vấn đề là dữ liệu mình lấy bị lỗi unicode không đọc được
Mã:
Sub VBA_SQL_BaoCao()
Dim sSQLQry As String
Dim ReturnArray

Dim Conn As New ADODB.Connection
Dim hp_rs As New ADODB.Recordset

Dim DBPath As String, sconnect As String
Dim file_report_car As String
Dim ws As Worksheet
Dim ws_hp As Worksheet
Dim wb As Workbook
Dim ws_b As Worksheet
Dim lastRow As Integer
Dim i As Integer
'DBPath = ThisWorkbook.FullName 'Refering the sameworkbook as Data Source

DBPath = ThisWorkbook.Path & "\tonghop_hp.xlsx"
file_report_car = ThisWorkbook.Path & "\output\BaoCaoDoanhThu.xlsx"
'delete workbook'
'create workbook'
'If Len(Dir$(file_report_car)) > 0 Then'
     'Kill file_report_car'
'End If'
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"

sconnect = "Provider=MSDASQL;DSN=Excel Files;DBQ=" & DBPath & ";HDR=No';CharSet=utf-8;Auto Translate=False;"
'If any issue with MSDASQL Provider, Try the Microsoft.Jet.OLEDB:
'sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & ";Extended Properties="Excel 8.0;HDR=No;"

'sconnect = "ODBC;Driver={Microsoft Excel Driver (*.xls)};DSN=Excel Files;DBQ=D:\TDN_DATA\tonghop_hp.xlsx;MaxBufferSize=2048;PageTimeout=5;ReadOnly=False;content=text/x-ms-odc; charset=utf-8;"

Conn.Open sconnect

hp_sSQLSting = "SELECT Ten, KHACH_HANG, MA_TUYEN, Count(*) AS So_Luong " & "FROM [HP$]" & " WHERE MA_TUYEN IS NOT NULL GROUP BY  MA_TUYEN" & " ORDER BY TUYEN"
hp_rs.Open hp_sSQLSting, Conn
Set wb = Workbooks.Open(file_report_car)
'wb.Sheets(1).Name = "HP"
'wb.SaveAs file_report_car
Set ws = wb.Sheets(1)
'clear data range
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("A3:F" & lastRow).ClearContents
ws.Range("A2").CopyFromRecordset hp_rs
Dữ liệu mình lấy ok nhưng khi viết vào file exel thì bị lỗi unicode không đọc được
mọi người có cách nào chỉ giúp
Thanks
 
Bạn thử bỏ charset =utf-8 xem được không.
 
Mình đã thử nhưng vẫn bị lỗi

Trong code người ta có ghi kìa bạn

Mã:
If any issue with MSDASQL Provider, Try the Microsoft.Jet.OLEDB

Bạn chuyển qua dùng chuỗi kết nối trong đó có ghi thử xem sao

Mã:
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & ";Extended Properties="Excel 12.0;HDR=No;"

Ở trên mình thấy đề là
"HDR=No"
mà ở dưới lại ghi tên cột ra là sao vậy cà ?
 
Trong code người ta có ghi kìa bạn

Mã:
If any issue with MSDASQL Provider, Try the Microsoft.Jet.OLEDB

Bạn chuyển qua dùng chuỗi kết nối trong đó có ghi thử xem sao

Mã:
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath & ";Extended Properties="Excel 12.0;HDR=No;"

Ở trên mình thấy đề là
"HDR=No"
mà ở dưới lại ghi tên cột ra là sao vậy cà ?
mình mới học cũng chưa rõ HDR là gì nữa, dòng connect mình lấy trên mạng
 
Web KT

Bài viết mới nhất

Back
Top Bottom