Hi,
I have been reading a lot about ADODB but I couldn't figure my problem. I hope someone here can help.
I have an Excel Sheet which I use as Database. The database has 11 columns and I insert data with the following function:
Now I want to retrieve this data. i.e. I want all F1 where F2 and F3 are 0 AND I want them ordered descending. I'm trying to achieve this with:
The result I am getting looks like this:
9
8
7
6
5
4
3
2
15
14
13
12
11
10
1
I assume, that the data is interpreted as String instead of an integer. But I explicitely stated the data as Integer when storing the data into the DB.
Am I missing something here?
Thanks for your help,
kind regards,
m
I have been reading a lot about ADODB but I couldn't figure my problem. I hope someone here can help.
I have an Excel Sheet which I use as Database. The database has 11 columns and I insert data with the following function:
Code:
Sub testInsert()
Dim adoCommand As New ADODB.Command
Dim sQuery As String
Dim i As Integer
Dim strTest As String
strTest = "test"
sQuery = "INSERT INTO " & sDBName & " (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11) VALUES (@F1, @F2, @F3, @F4, @F5, @F6, @F7, @F8, @F9, @F10, @F11)"
sAdoPath = ThisWorkbook.FullName
sAdoConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & sAdoPath & ";Extended Properties=""Excel 12.0;HDR=No"";"
With adoCommand
.ActiveConnection = sAdoConnectString
.CommandType = adCmdText
.CommandText = sQuery
.Prepared = True
.Parameters.Append .CreateParameter("F1", adInteger, adParamInput, , 1)
.Parameters.Append .CreateParameter("F2", adInteger, adParamInput, , 0)
.Parameters.Append .CreateParameter("F3", adInteger, adParamInput, , 0)
.Parameters.Append .CreateParameter("F4", adVarChar, adParamInput, 8000, strTest)
.Parameters.Append .CreateParameter("F5", adInteger, adParamInput, , 1111111)
.Parameters.Append .CreateParameter("F6", adVarChar, adParamInput, 8000, "Description")
.Parameters.Append .CreateParameter("F7", adVarChar, adParamInput, 8000, "Responsibility")
.Parameters.Append .CreateParameter("F8", adVarChar, adParamInput, 8000, "Involved")
.Parameters.Append .CreateParameter("F9", adInteger, adParamInput, , 546465)
.Parameters.Append .CreateParameter("F10", adInteger, adParamInput, , 546465)
.Parameters.Append .CreateParameter("F11", adVarChar, adParamInput, 10, "o")
'.Execute , , adCmdText + adExecuteNoRecords
End With
For i = 1 To 15
adoCommand("F1") = i
adoCommand.Execute , , adCmdText + adExecuteNoRecords
Next i
End Sub
Now I want to retrieve this data. i.e. I want all F1 where F2 and F3 are 0 AND I want them ordered descending. I'm trying to achieve this with:
Code:
Sub testSelect()
Dim adoCommand As New ADODB.Command
Dim sQuery As String
Dim mrs As New ADODB.Recordset
Dim strTest As String
strTest = "test"
sQuery = "SELECT F1 FROM " & sDBName & " WHERE F2=@F2 AND F3=@F3 ORDER BY F1 DESC"
sAdoPath = ThisWorkbook.FullName
sAdoConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & sAdoPath & ";Extended Properties=""Excel 12.0;HDR=No"";Persist Security Info=True;"
'sAdoConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & sAdoPath & ";Extended Properties=""Excel 12.0;HDR=No"";"
With adoCommand
.ActiveConnection = sAdoConnectString
.CommandType = adCmdText
.CommandText = sQuery
.Prepared = True
.Parameters.Append .CreateParameter("F2", adInteger, adParamInput, , 0)
.Parameters.Append .CreateParameter("F3", adInteger, adParamInput, , 0)
End With
Set mrs = adoCommand.Execute
With mrs
Do While Not .EOF
For k = 0 To .Fields.Count - 1
Debug.Print .Fields(k)
Next k
.MoveNext
Loop
End With
mrs.Close
End Sub
The result I am getting looks like this:
9
8
7
6
5
4
3
2
15
14
13
12
11
10
1
I assume, that the data is interpreted as String instead of an integer. But I explicitely stated the data as Integer when storing the data into the DB.
Am I missing something here?
Thanks for your help,
kind regards,
m