Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a date field

Status
Not open for further replies.

bscs1963

Programmer
Apr 7, 2009
22
US
I am coding in vba looking where i am filtering using the following lines.

set rsTemp1 currentdb.openrecordset ("tblReceiving")
set rstemp2 = currentdb.openrecordset ("Select * From [Table1] Where Date = rstemp1!ReceiptDate")

I know that if the ReceiptDate is a text field, then the the entry would be Where Date = '" & rstemp1!ReceiptDate & "'

What is the proper syntax so it realizes that it is a date field?

Can someone also tell me what the proper syntax is for a numeric field?

This seems to be something that is confusing me.

Thanks

I keep getting mismatch in the filter.
 
You delimited dates with # like:
Code:
set rsTemp1 currentdb.openrecordset ("tblReceiving")
set rstemp2 = currentdb.openrecordset ("Select * From [Table1] Where Date =#" & rstemp1!ReceiptDate & "#")
Numeric values require no delimiter.

Duane
Hook'D on Access
MS Access MVP
 

I would even go with something like:
Code:
[blue]Dim strSQL As String[/blue]
set rsTemp1 currentdb.openrecordset ("tblReceiving")[blue]
strSQL = "Select * From [Table1] Where Date =#" & rstemp1!ReceiptDate & "#"
Debug.Print strSQL[/blue]   [green]'See what your DB sees[/green]
set rstemp2 = currentdb.openrecordset ([blue]strSQL[/blue])
This is nice not only because you can see your SQL, but also if you decide to use some error handler and write into an error log, you can write your strSQL into error log.

Have fun.

---- Andy
 
I use these especially if working with non US settings.

Code:
Public Function sqlTxt(varItem As Variant) As Variant
  'for string values in sql
  If Not IsNull(varItem) Then
    varItem = Replace(varItem, "'", "''")
    sqlTxt = "'" & varItem & "'"
  End If
End Function

Function SQLDate(varDate As Variant) As Variant
    'For date values in sql 
    If IsDate(varDate) Then
        If DateValue(varDate) = varDate Then
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
        Else
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
        End If
    End If
End Function
And always do what Andy says about first saving it to a string.
Code:
strSQL = "Select * From [Table1] Where Date = " & sqlDate(rstemp1!ReceiptDate)
Debug.Print strSQL   'See what your DB sees
 

Or - if you do not want to use [tt]Debug.Print[/tt] - you can always put break on the line
Code:
set rstemp2 = currentdb.openrecordset (strSQL)
and in your Immediate Window type:
[tt]
? strSQL[/tt]

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top