lenin
Apr 9th, 2000, 12:02 AM
Hello,
this question has probably been asked a million times, but I have never had cause to query dates from VB - ADO to Access, and it is causing me some hassle.
In short, I generate a simple select statement, assign this to the commandtext object of a command and execute it. The string contains a date, which I pass as for example April 4th would be #04/04/00#
The string executes perfectly well if copied from the VB environment ( from immediate window ) and ran directly as a query in Access, however executing the same string via VB / ADO returns -1 as recordcount. Can anyone help me out? I am presuming that ADO adds "something" to the query string which Access has a problem with.
Is there a way around this issue?
Many thanks in Advance.
Source below:
Private Sub cmdRun_Click()
'
' Declare command and SQL String, and ADO Recordset
'
Dim lc_Query As ADODB.Command
Dim ls_SQL As String
Dim lrs_results As New ADODB.Recordset
'
' Instantiate the object
'
Set lc_Query = New ADODB.Command
'
' Initialise String
'
ls_SQL = "Select Detail, StartTime, StartDate, EndTime, EndDate, Outcome from All1 where "
'
' Check Date options
'
If optBeforeDate = True Then
ls_SQL = ls_SQL & "StartDate < '" & DTPicker1.Value & "'"
ElseIf optAfterDate = True Then
ls_SQL = ls_SQL & "StartDate > #" & DTPicker1.Value & "#"
Else
ls_SQL = ls_SQL & "StartDate = #" & DTPicker1.Value & "#"
End If
'
' Associate Command object with Active Connection
' Associate the commandText object with the SQL String.
'
lc_Query.ActiveConnection = lconn
lc_Query.CommandText = ls_SQL
lc_Query.CommandType = adCmdText
'
' Associate the results from the query with the recordset.
'
Set lrs_results = lc_Query.Execute
lrs_results.MoveFirst
MsgBox lrs_results(0)
this question has probably been asked a million times, but I have never had cause to query dates from VB - ADO to Access, and it is causing me some hassle.
In short, I generate a simple select statement, assign this to the commandtext object of a command and execute it. The string contains a date, which I pass as for example April 4th would be #04/04/00#
The string executes perfectly well if copied from the VB environment ( from immediate window ) and ran directly as a query in Access, however executing the same string via VB / ADO returns -1 as recordcount. Can anyone help me out? I am presuming that ADO adds "something" to the query string which Access has a problem with.
Is there a way around this issue?
Many thanks in Advance.
Source below:
Private Sub cmdRun_Click()
'
' Declare command and SQL String, and ADO Recordset
'
Dim lc_Query As ADODB.Command
Dim ls_SQL As String
Dim lrs_results As New ADODB.Recordset
'
' Instantiate the object
'
Set lc_Query = New ADODB.Command
'
' Initialise String
'
ls_SQL = "Select Detail, StartTime, StartDate, EndTime, EndDate, Outcome from All1 where "
'
' Check Date options
'
If optBeforeDate = True Then
ls_SQL = ls_SQL & "StartDate < '" & DTPicker1.Value & "'"
ElseIf optAfterDate = True Then
ls_SQL = ls_SQL & "StartDate > #" & DTPicker1.Value & "#"
Else
ls_SQL = ls_SQL & "StartDate = #" & DTPicker1.Value & "#"
End If
'
' Associate Command object with Active Connection
' Associate the commandText object with the SQL String.
'
lc_Query.ActiveConnection = lconn
lc_Query.CommandText = ls_SQL
lc_Query.CommandType = adCmdText
'
' Associate the results from the query with the recordset.
'
Set lrs_results = lc_Query.Execute
lrs_results.MoveFirst
MsgBox lrs_results(0)