Hello:
I am trying to write a code that will delete all records found in my DAO recordset Below is the code I have so far: -
'Procdure to give the user the ability to delete all records
-
'for a predefined recordset from the tblStaticAllForecast table
-
-
LockSQL = "SELECT * FROM tblStaticAllForecast WHERE" & _
-
" DivisionIDFK = " & Val(Me.cboDivision.Value) & _
-
" And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
-
" And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
-
" And YearID = " & Val(Me.CboYear.Value) & _
-
" And MonthID = " & Val(Me.CboMonth.Value) & _
-
" And FWeek = " & Val(Me.cboWeek.Value)
-
-
Dim rst As DAO.Recordset
-
Set rst = CurrentDb.OpenRecordset(LockSQL)
-
recordexists = rst.RecordCount
-
-
'If no records are found
-
If recordexists = 0 Then
-
MsgBox "There are no records to delete."
-
Else
-
-
rst.MoveLast 'Move to last record
-
rst.MoveFirst 'Move to First record
-
-
If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
-
" Click the ok button to proceed", vbOKCancel, vbDefaultButton2) = vbOK Then
-
-
'code will delete the records that the user has selected.
-
-
-
End If
-
End If
-
-
End Sub
-
-
Is there a quick code that would do this?
Keith.
5 14356
Hello:
I am trying to write a code that will delete all records found in my DAO recordset Below is the code I have so far: -
'Procdure to give the user the ability to delete all records
-
'for a predefined recordset from the tblStaticAllForecast table
-
-
LockSQL = "SELECT * FROM tblStaticAllForecast WHERE" & _
-
" DivisionIDFK = " & Val(Me.cboDivision.Value) & _
-
" And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
-
" And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
-
" And YearID = " & Val(Me.CboYear.Value) & _
-
" And MonthID = " & Val(Me.CboMonth.Value) & _
-
" And FWeek = " & Val(Me.cboWeek.Value)
-
-
Dim rst As DAO.Recordset
-
Set rst = CurrentDb.OpenRecordset(LockSQL)
-
recordexists = rst.RecordCount
-
-
'If no records are found
-
If recordexists = 0 Then
-
MsgBox "There are no records to delete."
-
Else
-
-
rst.MoveLast 'Move to last record
-
rst.MoveFirst 'Move to First record
-
-
If MsgBox("The number of records you are about to delete is " & recordexists & "." & _
-
" Click the ok button to proceed", vbOKCancel, vbDefaultButton2) = vbOK Then
-
-
'code will delete the records that the user has selected.
-
-
-
End If
-
End If
-
-
End Sub
-
-
Is there a quick code that would do this?
Keith.
Are you simply wanting to delete all Records within the criteria of what you're deleting?
If so, once you established the connection, you can simply add a SQL Statement that will delete all the records that have been selected from you're conditional statement.
example -
-
DoCmd.RunSQL ("DELETE * FROM tblStaticAllForecast WHERE" & _
-
" DivisionIDFK = " & Val(Me.cboDivision.Value) & _
-
" And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
-
" And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
-
" And YearID = " & Val(Me.CboYear.Value) & _
-
" And MonthID = " & Val(Me.CboMonth.Value) & _
-
" And FWeek = " & Val(Me.cboWeek.Value))
-
ADezii 8,834
Recognized Expert Expert
Are you simply wanting to delete all Records within the criteria of what you're deleting?
If so, once you established the connection, you can simply add a SQL Statement that will delete all the records that have been selected from you're conditional statement.
example -
-
DoCmd.RunSQL ("DELETE * FROM tblStaticAllForecast WHERE" & _
-
" DivisionIDFK = " & Val(Me.cboDivision.Value) & _
-
" And WrkRegIDFK = " & Val(Me.cboWrkReg.Value) & _
-
" And CreditRegIDFK = " & Val(Me.cboCreditReg.Value) & _
-
" And YearID = " & Val(Me.CboYear.Value) & _
-
" And MonthID = " & Val(Me.CboMonth.Value) & _
-
" And FWeek = " & Val(Me.cboWeek.Value))
-
Hello Pianoman, there is actually a much easier method: -
DoCmd.SetWarnings False
-
DoCmd.RunSQL Replace(LockSQL, "Select", "Delete")
-
DoCmd.SetWarnings True
I was away for a couple days and did not have access to the forum. I guess there is more than one way to skin a cat... Because I was in "Loop" mode, I came up with this solution: -
'Delete the records that the user has selected.
-
-
With rst
-
-
.Delete
-
-
End With
-
-
'Check to make sure that at least one record exists in the recordsert
-
-
If (rst.RecordCount > 0) Then
-
-
rst.MoveFirst ' Start deletion from first record
-
-
'Delete one record at a time using a do while loop
-
-
Do While Not rst.EOF
-
rst.Delete
-
rst.MoveNext
-
Loop
-
End If
-
-
MsgBox "Records have been deleted.", vbInformation, "Message"
-
-
'Close the recordset
-
End If
-
End If
-
End If
-
End Sub
-
Thanks for the ideas and help. By the way, I like the cleaner look of the other ideas.....
Keith.
Hello Pianoman, there is actually a much easier method: -
DoCmd.SetWarnings False
-
DoCmd.RunSQL Replace(LockSQL, "Select", "Delete")
-
DoCmd.SetWarnings True
ADezii:
How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?
The reason I ask is that I am thinking of using it on something else I need to do. That is to say where the records have to append to a table; I could use the same method by swapping out the word "Select" for "Insert" in my SQL?
What do you think?
Keith.
ADezii 8,834
Recognized Expert Expert
ADezii:
How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?
The reason I ask is that I am thinking of using it on something else I need to do. That is to say where the records have to append to a table; I could use the same method by swapping out the word "Select" for "Insert" in my SQL?
What do you think?
Keith.
How does your method work. Is your code just simply swapping out the word "Select" for "Delete" in my SQL?
You hit the nail on the head, since the SQL is exactly the same except for these 2 Keywords, Replace() simply substitutes Delete for Select in the Statement.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steve |
last post by:
I have several pairs of synchronized subforms in an application. I have a Delete
button for each pair that uses the following code or similar to delete a record
in the second subform:
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
End If
ExitHere:
Me!SubName.SetFocus
|
by: hpi |
last post by:
Hello,
I have a table : Batch
It contains fields
batchnummer : Number (Long Integer)
datum : Date/Time
status : Number (Long Integer)
nr_records : Number (Long Integer)
|
by: Jan |
last post by:
Hi
I have a database that I use to keep track of the sales promotions that we
send to companies.
I normally send a mailing based on a subset of the companies in the database
(found using the query: QryICTMassDistribution3) , I then use a form and
the code below to create a new record in the corrispondence table to show
what corrispondence has been sent to various companies.
|
by: paulwilliamsonremove |
last post by:
Hi,
I have two queries: "qryHistoryPersonIDs" that just contains the
"personID" numeric field, and "qryDonations" that just contains the
"personID" field, and a date field ("dDonationDate").
I want to delete every record in the qryHistoryPersonIDs recordset
that are not found in the qryDonations recordset for a particular date
range.
|
by: tony010409020622 |
last post by:
I just spent 4 months taking a dotnet class where i learned very
little. One of the things I did not learn is this: What are the
dotnet equivilents of commands such as:
Adodc1.Recordset.AddNew
Adodc1.Recordset.Update
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.MoveNext
Adodc1.Recordset.Delete
| |
by: igendreau |
last post by:
I'm trying to clean up a database of mine, and I need to convert some
old DAO code over to ADO. When I was using DAO, I had no problem
running this script and deleting the record using rs.Delete. But when
I rework the code in ADO, it tells me I can't delete it because there
are related records in another table. Never had that problem in DAO
and I haven't touched the relationships. Ignore most of this code and
focus on the recordset "rs"....
|
by: felicia |
last post by:
Hi All,
Below is my code to delete records:
adodcAllEntries.Recordset.MoveFirst
Do While (adodcAllEntries.Recordset.EOF = False)
If adodcAllEntries.Recordset.Fields(0) = selected_id Then
adodcAllEntries.Recordset.Delete
End If
|
by: AccessHunter |
last post by:
I am using the following code to find and delete records in a table. The logic will go through each record and if a duplicate row is found will delete it. I ran this code and it worked the first time. Its not deleting the rows when I tried the second time. I debugged the code and its actually going through the delete step but the row is not getting deleted as it did the first time. Please help.
Thanks in advance.
Function...
|
by: kcdoell |
last post by:
Hello I have a code where I want to delete the records that are found in my DAO recordset. I took a stab at this for the first time and got it to work but it is only delete one record at a time. If I execute the code again my record count will be minus one and then it will delete another single record etc etc until there are no records to delete. How could I create a loop statement so that I don't have to keep on executing the code??? Below...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |