Delete records from table using code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What general code would I use if want to delete the records of a table based
on a certain field criteria, say delete from table [A] all records in which
[year] = 2005?
 
Execute a Delete query statement.

Something like this:
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = 2005;"
dbEngine(0)(0).Execute strSql, dbFailOnError
 
This works great if a state year as a number in the cod as you have it. But
if I try to do dependent on a input field I get an error "missing parameter"

Year=txtYear.Value

Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = Year;"
dbEngine(0)(0).Execute strSql, dbFailOnError


--
David McKnight


Allen Browne said:
Execute a Delete query statement.

Something like this:
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = 2005;"
dbEngine(0)(0).Execute strSql, dbFailOnError

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

David McKnight said:
What general code would I use if want to delete the records of a table
based
on a certain field criteria, say delete from table [A] all records in
which
[year] = 2005?
 
Anonther Question on this if I want a range of years? Something like:
beginYear=txtbeginYear.Value
endYear=txtEndYear.Value
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] > beginYear < endYear;"
dbEngine(0)(0).Execute strSql, dbFailOnErrorDavid McKnight

???

David McKnight said:
This works great if a state year as a number in the cod as you have it. But
if I try to do dependent on a input field I get an error "missing parameter"

Year=txtYear.Value

Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = Year;"
dbEngine(0)(0).Execute strSql, dbFailOnError


--
David McKnight


Allen Browne said:
Execute a Delete query statement.

Something like this:
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = 2005;"
dbEngine(0)(0).Execute strSql, dbFailOnError

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

David McKnight said:
What general code would I use if want to delete the records of a table
based
on a certain field criteria, say delete from table [A] all records in
which
[year] = 2005?
 
Assuming you have text boxes named txtBeginYear and txtEndYear on your form,
you can concatenate those numbers into the string:

If (IsNull(Me.txtBeginYear) Or IsNull(Me.txtEndYear)) Then
MsgBox "Both years required."
Else
strSql = "DELETE FROM [A] WHERE [A].[Year] Between " & _
Me.txtBeginYear & " And " & Me.txtEndYear & ";"
dbEngine(0)(0).Execute strSql, dbFailOnError
End If

BTW, I hope that Year is just an example here. There is a VBA function named
Year(), so it is not a good name for a field. In some contexts, Access can
misunderstand what it refers to.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

David McKnight said:
Anonther Question on this if I want a range of years? Something like:
beginYear=txtbeginYear.Value
endYear=txtEndYear.Value
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] > beginYear < endYear;"
dbEngine(0)(0).Execute strSql, dbFailOnErrorDavid McKnight

???

David McKnight said:
This works great if a state year as a number in the cod as you have it.
But
if I try to do dependent on a input field I get an error "missing
parameter"

Year=txtYear.Value

Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = Year;"
dbEngine(0)(0).Execute strSql, dbFailOnError


--
David McKnight


Allen Browne said:
Execute a Delete query statement.

Something like this:
Dim strSql As String
strSql = "DELETE FROM [A] WHERE [A].[Year] = 2005;"
dbEngine(0)(0).Execute strSql, dbFailOnError

message
What general code would I use if want to delete the records of a
table
based
on a certain field criteria, say delete from table [A] all records in
which
[year] = 2005?
 
Back
Top