作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Excel VBA 代码从 MySQL 数据库中选择值时遇到了一个非常奇怪的问题。我构建了一个包含以下列的简单数据库:
假设有以下条目:
01;平底锅;彼得;纽约
02; P;彼得;纽约
但现在的问题是,如果我从表格中选择 *,它只会显示以下输出:
01; P;平底锅;纽约
02; P;平底锅;纽约
这意味着我只能看到条目的最小长度......那里发生了什么?我在不同的模块中有非常随意的 VBA 代码来完成该任务:
公共(public)变量:
Public cn As ADODB.Connection
Public rs As ADODB.Recordset
Public strSql As String
连接模块:
Public Function connectDB()
Dim strServer_Name As String
Dim strDB_Name As String
Dim strUser_ID As String
Dim strPassword As String
With Sheet2
strServer_Name = .Range("B2").Value
strDB_Name = .Range("B3").Value
strUser_ID = .Range("B4").Value
strPassword = .Range("B5").Value
End With
Set cn = New ADODB.Connection
cn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver}" & _
";SERVER=" & strServer_Name & _
";DATABASE=" & strDB_Name & _
";UID=" & strUser_ID & _
";PWD=" & strPassword & ""
End Function
子模块:
Sub request()
strSql = "SELECT * FROM test"
Call connectDB
Set rs = New ADODB.Recordset
rs.Open strSql, cn, adOpenDynamic
With Sheet1.Range("A1")
.ClearContents
.CopyFromRecordset rs
End With
Call disconnectDB
End Sub
这是 VBA 问题还是我的 MySQL 中存在任何错误?
最佳答案
好吧,我在这篇文章中找到了解决方案:sql-query-doesnt-return-full-results-for-only-one-field
按照其他人的方法解决了。在我的代码中,它看起来像这样:
Sub request()
Dim iCols As Integer
Dim iRows As Integer
strSql = "SELECT * FROM test"
Call connectDB
Set rs = New ADODB.Recordset
rs.Open strSql, cn, adOpenDynamic
iRows = 10
While Not rs.EOF
For iCols = 0 To rs.Fields.Count - 1
Tabelle1.Cells(iRows, iCols + 1).Value = rs.Fields(iCols).Value
Next
rs.MoveNext
iRows = iRows + 1
Wend
Call disconnectDB
End Sub
但是感谢所有试图帮助我的人!
关于mysql - Excel VBA MySQL "SELECT * FROM table"不完整信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369937/
我是一名优秀的程序员,十分优秀!