A1VBCode Forums

Delete item in database based on date field not working


http://a1vbcode.com/vbforums/Topic31197.aspx

By Raycomp - 2/4/2012

I hope someone here might have the answer





I have a form from where I would like to delete all records before a specified date, the form has a text field and command button that should delete records in the database older than 3 months but it does not delete the records.



the code on my form is as follows







Private Sub Form_Load()

Dim test As String



test = DateAdd("m", -3, Now) 'this uses the current date and subtract 3 months

Text1.Text = test

Text1.Text = Format$(test, "mm/dd/yyyy")



End Sub





Private Sub Command1_Click()



Dim cs As New ADODB.Connection

Dim rec As New ADODB.Recordset

Dim flds As New ADODB.Recordset



cs.ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & "C:\Program Files\Hazmat Control\HazMat.mdb"

cs.Open



Dim date1 As String

cs.BeginTrans

cs.Execute "DELETE * From ControlDetailsA where Date <= " & Text1.Text 'date is the field in the access table

cs.CommitTrans



End Sub




If I however change the code to delete by control number then it is successful. Looks like I am missing something when trying to use the date field to select the records.