- "Const" means constant. It doesn't (can't ) change. So if you define a string, "strQry" , as a constant, why would you think you could change a value in it?
- When using a variable in a string, you have to concatenate the variable to the string. If you use the variable name in the string, you are using the name of the variable, not the value.
Try adding the line in blue to your current code:
Code:
Const strQry As String = "SELECT * FROM [Calculation] WHERE Date BETWEEN strdate AND endate"
Msgbox strQry
What is displayed in the message box? Not the dates!!
This works because you have numbers, not variables.
Code:
Const strQry As String = "SELECT * FROM [Calculation] WHERE ID BETWEEN 50 AND 100"
Msgbox strQry
This dosen't work because you need to concatenate the variable "i" to the string:
Code:
i=50
Const strQry As String = "SELECT * FROM [Calculation] WHERE ID BETWEEN i AND 100"
Msgbox strQry
This works because "i" is concatenated to the string:
Code:
i=50
Const strQry As String = "SELECT * FROM [Calculation] WHERE ID BETWEEN " & i & " AND 100"
Msgbox strQry
- You don't have values assigned to strdate and endate.
NOTE: "Date" is a reserved word in Access and shouldn't be used for object names. Plus it is not descriptive. "Date" of what? DOB, effective date, date entered, ....
I don't use ADO, so I can't help you with that part of the code.
But try this:
Code:
Option Explicit
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim strdate As String
Dim endate As String
Dim strQry As String
Dim strDb As String
Sheets("RawData").Select
strdate = <user Form control> '<<= change to your control name
endate = <user Form control> '<<= change to your control name
strDb = "C:\Documents and Settings\YuC\My Documents\Database1.accdb"
strQry = "SELECT * FROM [Calculation] WHERE Date BETWEEN #" & strdate & "# AND #" & endate & "#"
' Create the database connection
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDb & ";"
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Open strQry
End With