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:
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
This query performs well and gives the desired result, but when I change it to this one:
Select param0,count(param0) From [Sheet1$] where eventid='addToCart' group by param0 order by count(param0) desc;
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:
Select eventid,param0,param1,count(*) From [Sheet1$] where eventid='search' group by param0, param1 order by count(*) desc;
It gives 'automation error'. I searched for it but could not get a suitable solution. Can anyone help with this? Thank you!
Edit: all the three queries give correct results in MySQL workbench. Now I need to perform the query directly in Excel sheet.