Article ID: 196590
Article Last Modified on 3/2/2005
CREATE TABLE whatime
(
id integer identity constraint p1 primary key nonclustered,
aname char(10),
tdate datetime,
tstamp timestamp
)
Insert into whatime(aname,tdate) values('Happy','10/31/98')
Insert into whatime(aname,tdate) values('Go','11/01/98')
Insert into whatime(aname,tdate) values('Lucky','11/02/98')
select * from whatime /* Just checking to see if it worked. */
Option Explicit
Private con As New ADODB.Connection
'A SQL Server timestamp column is a binary array.
' We can store a timestamp in a Visual Basic Variant variable.
Private varTStamp As Variant
Private Sub Form_Load()
Command1.Caption = "Retrieve by Date"
Command2.Caption = "Retrieve by TimeStamp"
Command2.Enabled = False
Command3.Caption = "Quit"
'This example uses the ODBC Provider with a Pubs DSN.
'Modify your connect string as needed.
con.CursorLocation = adUseClient
con.Open ("DSN=Pubs;UID=sa;PWD=;")
End Sub
Private Sub Command1_Click()
'Retrieve a row based on date
'then store the retrieved timestamp column in a Variant.
Dim rs As New ADODB.Recordset
rs.ActiveConnection = con
rs.Open "select * from whatime where tdate = '10/31/1998'"
Debug.Print rs("id"), rs("aname"), rs("tdate"), rs("tstamp")
Debug.Print rs("tstamp").Type '128: timestamp is type adBinary
' Store the timestamp value, to retrieve the record in Command2.
' The timestamp must be stored in a Variant.
varTStamp = rs("tstamp")
Command2.Enabled = True
rs.Close
Set rs = Nothing
End Sub
Private Sub Command2_Click()
'Retrieve a row using the timestamp value from Command1.
'Use a Command object and a Parameter object to explicitly pass
'the timestamp as adVarBinary, size 8.
Dim cmd As New ADODB.Command
Dim param As New ADODB.Parameter
Dim rs As New ADODB.Recordset
cmd.ActiveConnection = con
cmd.CommandType = adCmdText
cmd.CommandText = "select * from whatime where tstamp = ?"
'The parameter must be type adVarBinary, size 8 to pass a timestamp.
Set param = cmd.CreateParameter(, adVarBinary, adParamInput)
param.Size = 8
cmd.Parameters.Append param
'Retrieve based on the Variant from Command1.
param.Value = varTStamp
Set rs = cmd.Execute()
Debug.Print rs("id"), rs("aname"), rs("tdate"), rs("tstamp")
Debug.Print rs("tstamp").Type 'Type 128: adBinary
rs.Close
Set rs = Nothing
Set cmd = Nothing
End Sub
Private Sub Command3_Click()
Unload Me
End
End Sub
Private Sub Form_Unload(Cancel As Integer)
con.Close
Set con = Nothing
End Sub
170380 How To Display/Pass TimeStamp Value from/to SQL Server
181199 How To Determine How ADO Will Bind Parameters
Keywords: kbhowto kbdatabase KB196590