Excel VBA SQL查询ADODB

时间:2016-02-11 03:58:16

标签: excel vba ado

您好我有一个现有的Excel工作表,里面有一些数据,现在我想直接从VBA执行查询。这就是我现在所拥有的:

Private Sub CommandButton1_Click()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
Dim newSheet As Worksheet
'DBPath = ThisWorkbook.FullName
DBPath = "C:\someData.xlsm"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
Conn.Open sconnect
sSQLSting = "SELECT username,count(username) FROM [Sheet1$] group by username order by count(username) desc;"
mrs.Open sSQLSting, Conn
Set newSheet = Sheets.Add
ActiveSheet.Range("A1").CopyFromRecordset mrs
mrs.Close
Conn.Close
End Sub

此查询运行良好并提供所需的结果,但是当我将其更改为此结果时:

Select param0,count(param0) From [Sheet1$] where eventid='addToCart' group by param0 order by count(param0) desc;

因为param0是这样的:大多数都是数字,但有些是数字和字符混合在一起,所以查询结果只返回纯数字条目。那么如何配置数据库以便它识别出param0字段应该是text而不是int?此外,当我执行此查询时:

Select eventid,param0,param1,count(*) From [Sheet1$] where eventid='search' group by param0, param1 order by count(*) desc;

它给出了自动化错误'。我搜索它但无法得到合适的解决方案。有人能帮忙吗?谢谢!

编辑:所有三个查询都在MySQL工作台中提供了正确的结果。现在我需要直接在Excel工作表中执行查询。

2 个答案:

答案 0 :(得分:1)

您的第二个问题在于,您要求 eventid 字段而不将其包含在 GROUP BY 子集合中。

SELECT eventid, param0, param1, count(*)
  FROM [Sheet1$]
  WHERE eventid='search'
  GROUP by eventid, param0, param1
  ORDER BY COUNT(*) DESC;

我针对我编写的一些示例数据运行了修复过的查询,并提出了这个问题。

Sub grp_param()
    Dim cnx As Object, rs As Object, rs1 As Object
    Dim sWS1 As String, sWS2 As String, sWB As String, sCNX As String, sSQL As String
    Dim ws1TBLaddr As String

    ws1TBLaddr = Worksheets("Sheet4").Cells(1, 1).CurrentRegion.Address(0, 0)
    sWS1 = Worksheets("Sheet4").Name

    sWB = ThisWorkbook.FullName
    'for 64-bit Office
    'sCNX = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" & sWB _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    'for 32-bit or 64-bit Office
    sCNX = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sWB _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    Debug.Print sCNX

    Set cnx = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    Set rs1 = CreateObject("ADODB.Recordset")

    cnx.Open sCNX

    'Select param0,count(param0) From [Sheet1$] where eventid='addToCart'
    ' group by param0 order by count(param0) desc;
    sSQL = "SELECT param0, COUNT(param0) " & _
           "FROM [" & sWS1 & "$" & ws1TBLaddr & "] " & _
           "GROUP BY param0 " & _
           "ORDER BY count(param0) DESC;"
    Debug.Print sSQL

    rs.Open sSQL, cnx

    With Worksheets.Add(after:=Sheets(Sheets.Count))
        .Name = "Summary Data"
        .Range("A1").Resize(1, 2) = Array("param0", "count")
        .Range("A2").CopyFromRecordset rs
    End With

    'Select eventid,param0,param1,count(*) From [Sheet1$] where eventid='search'
    'group by param0, param1 order by count(*) desc;
    sSQL = "SELECT eventid, param0, param1, COUNT(*) " & _
           "FROM [" & sWS1 & "$" & ws1TBLaddr & "] " & _
           "GROUP BY eventid, param0, param1 " & _
           "ORDER BY count(param0) DESC;"
    Debug.Print sSQL

    rs1.Open sSQL, cnx

    With Worksheets(Sheets.Count)
        .Range("E1").Resize(1, 3) = Array("eventid", "param0", "count")
        .Range("E2").CopyFromRecordset rs1
    End With

    rs.Close: Set rs = Nothing
    rs1.Close: Set rs1 = Nothing
    cnx.Close: Set cnx = Nothing

End Sub

我不清楚为什么mySQL允许最后一次查询,但我的背景是在T-SQl中,它肯定会扼杀它。

答案 1 :(得分:0)

对于第一个混淆,我注意到这里的帖子:link,但我不想添加另一个文件进行处理,所以最后别无选择,我通过添加预处理文件在标题行正下方的4行文本中。 (在我的例子中,所有字段都可以是文本;我在MySQL中这样做)由于数据量非常大,这些虚拟文本不会影响结果,但可以帮助我正确生成数据库。