MySQL ODBC在Excel VBA中显示小数为空
我只是想用MySQL创build一个玩具数据库,并通过VBA将其导入到Excel中。 当我使用MySQL的Excel插件的时候没问题,但是当我试图通过VBA实现自动化的时候,除了十进制(65,30)之外的所有列都被正确的导入,在myArray中显示为“空”值。 代码:
Sub DBUpdate() ThisWorkbook.Worksheets("data").Cells.ClearContents Dim Password As String Dim SQLStr As String Dim Server_Name As String Dim User_ID As String Dim Database_Name As String Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily' With ThisWorkbook.Worksheets("DB") Server_Name = .Range("IPaddress").Value Database_Name = .Range("DBname").Value 'Name of database' User_ID = .Range("userID").Value 'id user or username' Password = .Range("password").Value 'Password' SQLStr = "SELECT * FROM trade_details" End With Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT Cn.Open "Driver={MySQL ODBC 5.2 ANSI Driver};Server=" & _ Server_Name & ";Database=" & Database_Name & _ ";Uid=" & User_ID & ";Pwd=" & Password & ";" rs.Open SQLStr, Cn, adOpenStatic Dim myArray() myArray = rs.GetRows() nCols = UBound(myArray, 1) nRows = UBound(myArray, 2) For K = 0 To nCols ' Using For loop data are displayed ' header Range("b1").Offset(0, K).Value = rs.Fields(K).Name ' row data For R = 0 To nRows 'Range("b2").Offset(R, K).Value = myArray(K, R) ThisWorkbook.Worksheets("data").Cells(R, K).Value = myArray(K, R) Next Next rs.Close Set rs = Nothing Cn.Close Set Cn = Nothing End Sub
我注意到的一件事是,真正的空值显示为'空',但数字显示为'空' ,types是'变异/空' 。
我还读了Excel只默认扫描前8行来确定列types。 但是我从第一行开始填充数据(即使情况是这样,它不能是空的)?
系统规格:
- Win10 64bit
- Office 2013
- 64位MySQL
- 服务器5.7
- MySQL ODBC 5.2 ANSI驱动程序
我真的很困惑,因为一切都是股票configuration,我甚至知道这是从Excel / VBA方面还是MySQL / ODBC方面,甚至没有一个人遇到这个问题时,我GOOGLE了。 任何input赞赏。
正如所评论的,Excel的单元格限制或接口不支持MySQL的最大小数限制(65,30)。 在VBA中,使用Doubletypes可以保持数值。 但是为了在电子表格上工作,考虑减lessMySQL中的小数点字段:
ALTER TABLE `tableName` MODIFY COLUMN `largenumberfield` DECIMAL(10,7);
此外,考虑绕过VBA数组的需要,并使用logging集作为唯一的数据容器。 在Excel中,可以使用range属性CopyFromRecordset
指定数据的最左列。 您还可以使用logging集的Field.Name
属性来拉列标题:
Sub DBUpdate() Dim Server_Name As String, SQLStr As String Dim Database_Name As String Dim User_ID As String, Password As String Dim Cn As Object, rs As Object, fld As Object Dim i as Integer Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily' With ThisWorkbook.Worksheets("DB") Server_Name = .Range("IPaddress").Value Database_Name = .Range("DBname").Value 'Name of database' User_ID = .Range("userID").Value 'id user or username' Password = .Range("password").Value 'Password' SQLStr = "SELECT * FROM trade_details" End With ' OPEN CONNECTION Set Cn = CreateObject("ADODB.Connection") Cn.Open "Driver={MySQL ODBC 5.2 ANSI Driver};Server=" & _ Server_Name & ";Database=" & Database_Name & _ ";Uid=" & User_ID & ";Pwd=" & Password & ";" ' OPEN RECORDSET rs.Open SQLStr, Cn, adOpenStatic ThisWorkbook.Worksheets("data").Cells.ClearContents ' COLUMN HEADERS i = 0 ThisWorkbook.Worksheets("data").Range("A1").Activate For Each fld In rs.Fields ActiveCell.Offset(0, i) = fld.Name i = i + 1 Next fld ' DATA ROWS ThisWorkbook.Worksheets("data").Range("A2").CopyFromRecordset rs rs.Close Set rs = Nothing Cn.Close Set Cn = Nothing End Sub