473,385 Members | 1,356 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,385 software developers and data experts.

delete records using querydef with parameter deletes ALL records

hpi
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)

It contains a number of records and I want to delete the record with a
specific batchnummer eg 89
I created a querydef "Delete Batch by Batchnummer"

PARAMETERS batchnummer Long;
DELETE *
FROM batch
WHERE batchnummer=[batchnummer];

This is the VB code I used

Sub doit()
Dim qd As QueryDef

Set qd = CurrentDb.QueryDefs("Delete Batch by Batchnummer")

qd.Parameters("batchnummer") = 89
qd1.Execute
End Sub

After running this procedure ALL records are gone from the Batch table.
I also tested this for other similar tables and I had the same problem.

If I use a 'normal' query which selects records (no deletion) AND I use a
recordset (qd.openrecordset(...))
then it seems to work. But I can't use a recordset when I want to do a
delete.
I know, I can use a construction where I create a string containing the
where clause and run it using
docmd.RunSQL but I'm just wondering why this doesn't work.

Thanks

Hans
Nov 12 '05 #1
5 5003
I don't use parameter queries much myself, but have you tried...

Sub doit(bNum As Long)
Dim sqlStr As String
Dim db As Databse

Set db=CurrentDB
sqlStr ="DELETE batchnummer.*, batchnummer.batchnummer FROM batchnummer
WHERE"
sqlStr =sqlStr & " (batchnummer.batchnummer = " & bNum & ");"

db.Execute sqlStr, dbFailOnError
End Sub

Add whatever error traping you need to ensure success.

Mike Storr
www.veraccess.com
"hpi" <no****@hotmail.com> wrote in message
news:40*********************@read.news.nl.uu.net.. .
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)

It contains a number of records and I want to delete the record with a
specific batchnummer eg 89
I created a querydef "Delete Batch by Batchnummer"

PARAMETERS batchnummer Long;
DELETE *
FROM batch
WHERE batchnummer=[batchnummer];

This is the VB code I used

Sub doit()
Dim qd As QueryDef

Set qd = CurrentDb.QueryDefs("Delete Batch by Batchnummer")

qd.Parameters("batchnummer") = 89
qd1.Execute
End Sub

After running this procedure ALL records are gone from the Batch table.
I also tested this for other similar tables and I had the same problem.

If I use a 'normal' query which selects records (no deletion) AND I use a
recordset (qd.openrecordset(...))
then it seems to work. But I can't use a recordset when I want to do a
delete.
I know, I can use a construction where I create a string containing the
where clause and run it using
docmd.RunSQL but I'm just wondering why this doesn't work.

Thanks

Hans

Nov 12 '05 #2
hpi
Can anyone answer this question?

tx

Hans

"hpi" <no****@hotmail.com> wrote in message
news:40*********************@read.news.nl.uu.net.. .
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)

It contains a number of records and I want to delete the record with a
specific batchnummer eg 89
I created a querydef "Delete Batch by Batchnummer"

PARAMETERS batchnummer Long;
DELETE *
FROM batch
WHERE batchnummer=[batchnummer];

This is the VB code I used

Sub doit()
Dim qd As QueryDef

Set qd = CurrentDb.QueryDefs("Delete Batch by Batchnummer")

qd.Parameters("batchnummer") = 89
qd1.Execute
End Sub

After running this procedure ALL records are gone from the Batch table.
I also tested this for other similar tables and I had the same problem.

If I use a 'normal' query which selects records (no deletion) AND I use a
recordset (qd.openrecordset(...))
then it seems to work. But I can't use a recordset when I want to do a
delete.
I know, I can use a construction where I create a string containing the
where clause and run it using
docmd.RunSQL but I'm just wondering why this doesn't work.

Thanks

Hans

Nov 12 '05 #3
hpi
Hi,

Thanks, this was the solution.

Pretty logical too. The parameter is simply a field with a fixed value.

Hans

Chuck Grimsby <c.*******@worldnet.att.net.invalid> wrote in
news:10********************************@4ax.com:

Sure. I'm surprised that the query doesn't delete all the records all
the time however.

Change your query to be:

PARAMETERS lngbatchnummer Long;
DELETE *
FROM batch
WHERE batchnummer = lngbatchnummer;

so that query engine won't confuse the parameter with the field name.
On Mon, 26 Jan 2004 14:19:01 +0100, "hpi" <no****@hotmail.com> wrote:
Can anyone answer this question?

"hpi" <no****@hotmail.com> wrote in message
news:40*********************@read.news.nl.uu.net ...
I have a table : Batch
It contains fields
batchnummer : Number (Long Integer)
datum : Date/Time
status : Number (Long Integer)
nr_records : Number (Long Integer)
It contains a number of records and I want to delete the record with
a specific batchnummer eg 89
I created a querydef "Delete Batch by Batchnummer"
PARAMETERS batchnummer Long;
DELETE *
FROM batch
WHERE batchnummer=[batchnummer];
This is the VB code I used
Sub doit()
Dim qd As QueryDef
Set qd = CurrentDb.QueryDefs("Delete Batch by Batchnummer")
qd.Parameters("batchnummer") = 89
qd1.Execute
End Sub


Nov 12 '05 #4
On Wed, 28 Jan 2004 00:20:02 GMT, Chuck Grimsby wrote:

I have to disagree somewhat to keeping field and conrol names different.
Having them the same can be emmensly helpfull when doing record
manipulations through recordsets. Just loop through the table fields and
make tbl.FieldName = Form.Controls("FieldName") or vice-versa.
In one project a form had 68 fields on it, and the updates were done using
4 lines of code (not including trapping)
I think it's more important to remember to qualify objects than it is to
use unique names ie - tableName.Field in queries and Form( ! or . )Name in
code. Especially when you need to make things re-usable.

--
Mike Storr
veraccess.com

Just FYI....

It's *really* easy to confuse Access.
As much as you can, as *often* as you can, make everything unique.

Don't duplicate field names, control names (especially make sure that
the control name isn't the same as the field name!), object names,
variable names, etc., etc., etc..

Not only will doing the above help Access know what you are asking it
to do, it'll help you know what you're referring to.

On Tue, 27 Jan 2004 20:56:52 +0000 (UTC), hpi <hp*@hpi.com> wrote:
Thanks, this was the solution.
Pretty logical too. The parameter is simply a field with a fixed value.

Chuck Grimsby <c.*******@worldnet.att.net.invalid> wrote in
news:10********************************@4ax.co m:
Sure. I'm surprised that the query doesn't delete all the records all
the time however.
Change your query to be:
PARAMETERS lngbatchnummer Long;
DELETE *
FROM batch
WHERE batchnummer = lngbatchnummer;
so that query engine won't confuse the parameter with the field name. On Mon, 26 Jan 2004 14:19:01 +0100, "hpi" <no****@hotmail.com> wrote:
Can anyone answer this question?"hpi" <no****@hotmail.com> wrote in message
news:40*********************@read.news.nl.uu.n et...
> I have a table : Batch
> It contains fields
> batchnummer : Number (Long Integer)
> datum : Date/Time
> status : Number (Long Integer)
> nr_records : Number (Long Integer)
> It contains a number of records and I want to delete the record with
> a specific batchnummer eg 89
> I created a querydef "Delete Batch by Batchnummer"
> PARAMETERS batchnummer Long;
> DELETE *
> FROM batch
> WHERE batchnummer=[batchnummer];
> This is the VB code I used
> Sub doit()
> Dim qd As QueryDef
> Set qd = CurrentDb.QueryDefs("Delete Batch by Batchnummer")
> qd.Parameters("batchnummer") = 89
> qd1.Execute
> End Sub


Nov 12 '05 #5
On Wed, 28 Jan 2004 10:33:47 GMT, Chuck Grimsby wrote:

I'm intrigued as to how you think I'm only "...starting to get the
idea...". I think I just have a different approach. For a number of the
scenarios we (my business) have come across, having the form bound to a
record source, was more complex than having it unbound and working with the
data through queries and recordsets, although there are cases where we do
bind them as well.
Without going into to many details, the four line example I used is only
part of a process that accepts arguments such as form instance, data
sources and targets, and as such is used against two unrelated forms.
--
Mike Storr
veraccess.com
It sounds like you're starting to get the idea behind what modular (or
"re-useable") code is all about. Here's another idea or two for you
however...

Keeping the control names the same across applications can make the
code you write re-useable across applications without changing the
code at all. This can make recordset manipulations even easier, and
application development even faster (and easier).

Another way to do this is to pass the control(s) (or form) to a
standardized module, then use the control's controlsource property to
do whatever it is you need to do. This method becomes even more
important when dealing with forms that are *really* similar, although
in a truly modular environment, there wouldn't even be a need for
similar forms!

You might also benefit from using a query, rather then the table
directly, as the source for your forms, and then refer to the ordinal
order of the fields to do your manipulation. Rather then worrying
about getting the field names right, all you have to do is to get them
in the right order once. As long as the field you want to manipulate
is field # 7, it really doesn't matter if it's named "DateLastUpdated"
or "DateLastChanged". In such cases is doesn't matter at all what the
control *or* field names are!

On Tue, 27 Jan 2004 23:07:34 -0500, Mike Storr <st******@sympatico.ca>
wrote:

Nov 12 '05 #6

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

Similar topics

3
by: cfxchange | last post by:
I am looking into work-arounds for what seems to be a flaw, or "undocumented feature" of SQL Server replication and Instead of Delete triggers not playing together. It seems that if you want to...
10
by: D. Dante Lorenso | last post by:
I'd like to run a clean up command on my tables to eliminate rows that I'm no longer using in the database. I want to do something like this: DELETE FROM tablename WHERE...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
6
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from...
2
by: Robin | last post by:
I have a main table that I need to delete records that arn't referenced in another. Query says I cannot delete. If I remove the reference query all deletes ok. Hope there is a way around this ?...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
1
by: JC | last post by:
Hello, I am trying to change a select, find duplicates, query into a delete query. I want to get rid of the records in the main table "tblHurnsHistory." I changed the Find Duplicates query to...
1
by: Bobby | last post by:
Hi I am using Access 2003 mdb as a front end to an application which uses SQL Server 2000 as the backend. The two are connected using ODBC. On one particular table (the Stock table), I have a...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.