473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

What's wrong with this code?

A97.

Simplified description: I have a query which returns one field containing
numbers.

I have a table where one field contains the same numbers as the query
(amongst others). The query and the table are not related in any way.

I want to loop through each record in the query, find matching records in
the table and delete them. The code counts the correct number of records
(5073) provided I comment out the delete command - if the delete command is
executed then the number of records deleted is about 3400. Here's an
extract from the code:

With rs1 'The Query
.MoveFirst
Do Until .EOF Or .BOF 'Do this until the end of the query
strCriteria = "[DRG_VER] = '" & rs1!DRG_VER & "'" 'Get the DRG_VER
from the query
rs2.MoveLast
rs2.MoveFirst
rs2.FindNext strCriteria 'Find a DRG_VER in the BUILD_ACT_TBL
that matches that from the query
If rs2.NoMatch = False Then 'There is a match
intCount = intCount + 1
Debug.Print intCount & " " & rs2![DRG_VER]
'rs2.Delete
End If 'There was no match
'rs2.FindNext strCriteria
.MoveNext 'Move to the next record in the query
Loop
End With

I've tried liberal sprinklings of MoveLasts and MoveFirsts and even tried
wrapping it all up into a transaction but to no avail. Can anyone advise?

Many thanks,
Keith.
www.keithwilby.com
Nov 13 '05 #1
3 1404
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It might be a better idea to use a DELETE query that would delete all
occurences in BUILD_ACT_TBL where it's DRG_VER = the query's DRG_VER.
Is that what you want? If so, perhaps this (untested):

DELETE B.*
FROM BUILD_ACT_TBL As B INNER JOIN Query As Q
ON B.DRG_VER = Q.DRG_VER

Then you'd save that code in a QueryDef and your VBA code would be like
this (DAO):

Dim db As DAO.Database
Dim qd As DAO.QueryDef
Set db = CurrentDB
Set qd = db.QueryDef("<query name>")
qd.Execute

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQe+TKYechKqOuFEgEQLzPwCg9JyJQqNsXvPtdJq3pmg5z8 S4VCMAoJQX
TLK80cK4+seyBfggI8K+47P2
=TWS9
-----END PGP SIGNATURE-----
White Bilky wrote:
A97.

Simplified description: I have a query which returns one field containing
numbers.

I have a table where one field contains the same numbers as the query
(amongst others). The query and the table are not related in any way.

I want to loop through each record in the query, find matching records in
the table and delete them. The code counts the correct number of records
(5073) provided I comment out the delete command - if the delete command is
executed then the number of records deleted is about 3400. Here's an
extract from the code:

With rs1 'The Query
.MoveFirst
Do Until .EOF Or .BOF 'Do this until the end of the query
strCriteria = "[DRG_VER] = '" & rs1!DRG_VER & "'" 'Get the DRG_VER
from the query
rs2.MoveLast
rs2.MoveFirst
rs2.FindNext strCriteria 'Find a DRG_VER in the BUILD_ACT_TBL
that matches that from the query
If rs2.NoMatch = False Then 'There is a match
intCount = intCount + 1
Debug.Print intCount & " " & rs2![DRG_VER]
'rs2.Delete
End If 'There was no match
'rs2.FindNext strCriteria
.MoveNext 'Move to the next record in the query
Loop
End With

I've tried liberal sprinklings of MoveLasts and MoveFirsts and even tried
wrapping it all up into a transaction but to no avail. Can anyone advise?

Nov 13 '05 #2
MGFoster <me@privacy.com> wrote:
It might be a better idea to use a DELETE query that would delete all
occurences in BUILD_ACT_TBL where it's DRG_VER = the query's DRG_VER.
Is that what you want?


Many thanks for such a quick response. Unfortunately, owing to the
complexity of the table relationships within the app, I'm unable to join
the recordsets in a query since it would not be updatable. This is why I
was attempting to do it the way I have.

Regards,
Keith.
Nov 13 '05 #3

"White Bilky" <ke*********@AwayWithYerCrap.com> wrote in message
news:Xn************************@10.15.188.42...
A97.

Simplified description: I have a query which returns one field containing
numbers.

I have a table where one field contains the same numbers as the query
(amongst others). The query and the table are not related in any way.

I want to loop through each record in the query, find matching records in
the table and delete them. The code counts the correct number of records
(5073) provided I comment out the delete command - if the delete command
is
executed then the number of records deleted is about 3400. Here's an
extract from the code:

With rs1 'The Query
.MoveFirst
Do Until .EOF Or .BOF 'Do this until the end of the query
strCriteria = "[DRG_VER] = '" & rs1!DRG_VER & "'" 'Get the DRG_VER
from the query
rs2.MoveLast
rs2.MoveFirst
rs2.FindNext strCriteria 'Find a DRG_VER in the BUILD_ACT_TBL
that matches that from the query
If rs2.NoMatch = False Then 'There is a match
intCount = intCount + 1
Debug.Print intCount & " " & rs2![DRG_VER]
'rs2.Delete
End If 'There was no match
'rs2.FindNext strCriteria
.MoveNext 'Move to the next record in the query
Loop
End With

I've tried liberal sprinklings of MoveLasts and MoveFirsts and even tried
wrapping it all up into a transaction but to no avail. Can anyone advise?

Many thanks,
Keith.
www.keithwilby.com


Could you not use a subquery to get something like:

"DELETE FROM Table1 WHERE Table1.ID
IN (SELECT Table2.ID FROM Table2
WHERE Table2.SomeField="Whatever")

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.