Tom, you really need to Execute the query statement instead of using
RunSQL. There is no confirmation message to annoy you, but you can still
determine if an error occurred. You can also determine the number of
records deleted.
This example assumes you want to delete from Table1, where the field named
MyDate is between the values in the 2 text boxes on the same form. It
builds the delete query string, executes it, generates a trappable error
if the deletion does not complete successfully, and reports how many
records were deleted.
Dim db As DAO.Database
Dim strSql As String
Const strcJetDate = "\#mm\/dd\/yyyy\#"
If IsDate(Me.txtStartDate) And IsDate(Me.txtEndDate) Then
Set db = dbEngine(0)(0)
strSql = "DELETE FROM Table1 WHERE [MyDate] Between " & _
Format(Me.txtStartDate, strcJetDate) & " And " & _
Format(Me.txtEndDate, strcJetDate) & ";"
db.Execute strSql, dbFailOnError
MsgBox db.RecordsAffected & " record(s) deleted."
Else
MsgBox "Both dates required."
End If
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Tom said:
Thanks for your response,
I tried to use docmd.setwarnings=false, usually it eliminate the massages
but not in this case.
can tou help me with the syntax for the msgbox that show how many were
deleted?
Thanks a lot,
Tom
schasteen said:
You can in your code turn off the wrning messages and turn them back on
when
complete.
DoCmd.SetWarnings False
....
DoCmd.SetWarnings true
and just use a messagebox to show how many records were deleted.
:
Thank you so much.
Few more issues, please
When I run my Delete query, there 2 conformation from access, one about
commit a change and second about how many rows were deleted.
I would like to eliminate them in the code( I know I can go to
"options" and
remove the checkmarks). If I choose "No" in one of these masssages I
get run
time error because of my code.
second, I would like to create my own massage that notify about how
many
records were deleted
Thanks for the help,
Tom
msgbox ("Are sure you want to delete all records beteen " &
Forms![Formname]![textbox1] &" and " & Forms![Formname]![textboxs2]
&
"?")
:
Thank you, but whay is the right syntax for that?
How do I put Value of a text box in the sring of the massage?
Thanks again,
Tom
Since the values are in the text boxes on the form, you could read
them
from there.
How can I insert those parametersn into a massage box?
I want to create a Mgsbox that say:
Are sure you want to delete all records beteen textbox1 and
textboxs2?
Thanks,
Tom
I`m traying to create a query that delete records by dates.
I created a form with 2 unbound text boxes to enter dates.
I created a delete query that the cratiria is under date.
between
textbox1 and textbox2. it didnt work that well.
Is there a better way to delete records between dates?
What is the right way to do it?