title: Excel VBA SQL query ADODB
body: <p>Hi I have an existing Excel sheet with some data inside, and now I want to perform queries directly from VBA. This is what I have now:</p>
  
  <pre><code>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=" &amp; DBPath &amp; ";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
  </code></pre>
  
  <p>This query performs well and gives the desired result, but when I change it to this one:</p>
  
  <pre><code>Select param0,count(param0) From [Sheet1$] where eventid='addToCart' group by param0 order by count(param0) desc;
  </code></pre>
  
  <p>Because the param0 is like this: most of them are numbers, but some of them are numbers and characters mixed together, so the query result only returns the pure-number entries. So how can I configure the database so that it recognizes the param0 field should be text, instead of int? Also, when I perform this query:</p>
  
  <pre><code>Select eventid,param0,param1,count(*) From [Sheet1$] where eventid='search' group by param0, param1 order by count(*) desc;
  </code></pre>
  
  <p>It gives 'automation error'. I searched for it but could not get a suitable solution. Can anyone help with this? Thank you!</p>
  
  <p><strong>Edit:</strong> all the three queries give correct results in MySQL workbench. Now I need to perform the query directly in Excel sheet.</p>