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

Deleting rows from a table using a query

47
Hi,

I am probably real dumb with these codes. I am working on a small project in which one of the most important things to be done is to delete fields from a table. So what i did was to write a SQL query and connect to a form's command button. But this has to only delete fields where the table's primary key matches with what the user enters in a text field on the form. The SQL code i used is

DELETE field1,field2,field3,.....
FROM <TableName>
WHERE field7=<FormName>.<TextboxName>

But there seems to be a problem with the code. Could someone help me with this

Thanks
May 19 '08 #1
12 1782
Megalog
378 Expert 256MB
DELETE field1,field2,field3,.....
FROM <TableName>
WHERE field7=<FormName>.<TextboxName>
Try:

Expand|Select|Wrap|Line Numbers
  1. DELETE TableName.field, TableName.field2, TableName.field3
  2. FROM TableName
  3. WHERE (((TableName.field7)=[Forms]![FormName]![TextboxName));
Make sure that the value in the table field, is of the same type that's being matched up in the form field. (number vs number, text vs text) If not, then a Convert function will be needed.
May 19 '08 #2
neosam
47
Try:

Expand|Select|Wrap|Line Numbers
  1. DELETE TableName.field, TableName.field2, TableName.field3
  2. FROM TableName
  3. WHERE (((TableName.field7)=[Forms]![FormName]![TextboxName));
Make sure that the value in the table field, is of the same type that's being matched up in the form field. (number vs number, text vs text) If not, then a Convert function will be needed.
Hi,

I did that but the query does not have a regular query icon but it has a "x!" and that is perhaps why the query is not being connected to the form's command button. If run the query seperately it works though!

Thanks
May 19 '08 #3
Megalog
378 Expert 256MB
Nope that's correct, different query types will show different icons. The 'x!' is the delete query icon.

How are you running the query in your form? You should be using:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCommandButtonName_Click()
  2.  
  3. DoCmd.OpenQuery [YourDeleteQueryName]
  4.  
  5. End Sub
If that works, you should get prompted a warning that you are deleting rows.
If you want to disable the warnings afterwards, use:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCommandButtonName_Click()
  2.  
  3. DoCmd.SetWarnings False
  4. DoCmd.OpenQuery [YourDeleteQueryName]
  5. DoCmd.SetWarnings True
  6.  
  7. End Sub
May 19 '08 #4
neosam
47
Nope that's correct, different query types will show different icons. The 'x!' is the delete query icon.

How are you running the query in your form? You should be using:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCommandButtonName_Click()
  2.  
  3. DoCmd.OpenQuery [YourDeleteQueryName]
  4.  
  5. End Sub
If that works, you should get prompted a warning that you are deleting rows.
If you want to disable the warnings afterwards, use:

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCommandButtonName_Click()
  2.  
  3. DoCmd.SetWarnings False
  4. DoCmd.OpenQuery [YourDeleteQueryName]
  5. DoCmd.SetWarnings True
  6.  
  7. End Sub

Hi,

I get a error message which is "Can't find project or library". is that something to do with references???

Thanks
May 19 '08 #5
Megalog
378 Expert 256MB
Please post what code you're using in your command button click event.
May 19 '08 #6
neosam
47
Please post what code you're using in your command button click event.
Code in VB

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command22_Click()
  2.     DoCmd.OpenQuery (Nom)
  3. End Sub
And the SQL query is .
Expand|Select|Wrap|Line Numbers
  1. DELETE [Nominations].[Employee_Id], [Nominations].[Employee_Name], [Nominations].[Team_Leader], [Nominations].[Date_Of_Training], [Nominations].[Training_Name], [Nominations].[Hours_Of_Training], [Nominations].[Venue], [Nominations].[Time], [Nominations].[Trainee_Id]
  2. FROM Nominations
  3. WHERE [Nominations].[Trainee_Id]=[Forms]![Cancel Nominations]!Text5;
May 19 '08 #7
Megalog
378 Expert 256MB
Edit: forgot the quotes around the query name =)

paste this over your form code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command22_Click()
  2.     DoCmd.OpenQuery "Nom"
  3. End Sub
and paste this over your query code in SQL view
Expand|Select|Wrap|Line Numbers
  1. DELETE [Nominations].[Employee_Id], [Nominations].[Employee_Name], [Nominations].[Team_Leader], [Nominations].[Date_Of_Training], [Nominations].[Training_Name], [Nominations].[Hours_Of_Training], [Nominations].[Venue], [Nominations].[Time], [Nominations].[Trainee_Id]
  2. FROM Nominations
  3. WHERE ((([Nominations].[Trainee_Id])=[Forms]![Cancel Nominations]![Text5]));
  4.  
May 19 '08 #8
neosam
47
Edit: forgot the quotes around the query name =)

paste this over your form code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command22_Click()
  2.     DoCmd.OpenQuery "Nom"
  3. End Sub
and paste this over your query code in SQL view
Expand|Select|Wrap|Line Numbers
  1. DELETE [Nominations].[Employee_Id], [Nominations].[Employee_Name], [Nominations].[Team_Leader], [Nominations].[Date_Of_Training], [Nominations].[Training_Name], [Nominations].[Hours_Of_Training], [Nominations].[Venue], [Nominations].[Time], [Nominations].[Trainee_Id]
  2. FROM Nominations
  3. WHERE ((([Nominations].[Trainee_Id])=[Forms]![Cancel Nominations]![Text5]));
  4.  

Hey it works... Thanks a lottttt!!!
May 19 '08 #9
Megalog
378 Expert 256MB
Hey it works... Thanks a lottttt!!!
Glad to help =) Have fun!
May 19 '08 #10
NeoPa
32,556 Expert Mod 16PB
FYI: DELETE queries delete records rather than fields. Deleting a field would involve changing the design of the table whereas deleting records is simply unloading some of the data.

This means that it is entirely unnecessary to list the fields in the SQL. The following should work for you just as well (This is not intended in any way as criticism MegaLog - Quite the contrary, you've given a fair bit of helpful advice in this thread :)) :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM Nominations
  3. WHERE [Trainee_Id]=[Forms]![Cancel Nominations]![Text5]
May 21 '08 #11
Megalog
378 Expert 256MB
FYI: DELETE queries delete records rather than fields. Deleting a field would involve changing the design of the table whereas deleting records is simply unloading some of the data.

This means that it is entirely unnecessary to list the fields in the SQL. The following should work for you just as well (This is not intended in any way as criticism MegaLog - Quite the contrary, you've given a fair bit of helpful advice in this thread :)) :
Expand|Select|Wrap|Line Numbers
  1. DELETE *
  2. FROM Nominations
  3. WHERE [Trainee_Id]=[Forms]![Cancel Nominations]![Text5]
That was going to be my next suggestion if it didnt work.. I only use "Delete *" in my queries. If I delete only certain field data, I use an update query... which I hope isnt what the OP wanted =) I dont take it as criticism, I like the FYI's, usually learn a little fact here and there.
May 21 '08 #12
NeoPa
32,556 Expert Mod 16PB
Good to hear it - and keep up the good work - some good stuff recently :)
May 22 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ScottH | last post by:
I was looking for thw SQL to delete dupes from a table, and came across this. All who saw it agreed in principle, but I can't quite figure out the logic. If we are deleting all rows whose rowid...
7
by: patrick.mcdonald | last post by:
Hello, Example: I have 100 rows that all have the field vaule of 'XYZ' I would like to delete only 10 of those rows. Is there such a query like "Delete 10 rows from table where table.field...
13
by: Dixie | last post by:
How do I, in VBA from an access form module, add 5 rows to the top of a spreadsheet called MySpreadsheet.xls. The worksheet is called MyWorksheet and there is already data in the first 5 rows. I...
3
by: Christoph | last post by:
I'm delving into using ADO.NET for the first time. In fact, I've never done any database work using C# at all so all of this is new to me. Please bear that in mind if I am asking stupid questions....
5
by: Robert Brown | last post by:
Hi All. I have a routine that checks a SQL Table for all records 3 months prior to a predetermined date, then I insert them into an Archive DB then delete those records from the original table....
3
by: skennd | last post by:
Here's my problem in exact replication: I have used the find duplicate query in Access, and the query determined the following duplicate values by the following query: In (SELECT FROM As...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
4
by: MiziaQ | last post by:
Hey, I'm using the following code to write entries to a data file and then read them in an msflexgrid. I now would like to add code under a delete button to use the table(grid) to delete rows from...
5
flexsingh
by: flexsingh | last post by:
Hello there, I have been trying to delete a row in php for a long time now and its getting frustrating, can any see if they could possible help me please. My first page is <?php // this...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.