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

Deleting a Record after selecting info with a combo box, access 2003

Hello;

I am attempting to use a command button "Delete Current Assignment Record" on a form to delete a user-selected record from a table entitled "Registration" with Key field "Registration ID".

I am using a combo box named "Select Task" that displays info from three tables to enable the user to select a record. The form's data source is the table Registration. column 7 within the combo box = the Registration ID.

the on click event for the command button is:

Private Sub Delete_Current_Assignment_Record_Click()
Delete [Registration].* from Registration where [Registration].[Registration ID] = [select task].Column(7)
End Sub

I have also tried using the acCmdDelete run command, but am unsure how to make sure the table Registration is set prior to execution.

MS ACCESS 2003;
OS Windows 2000 svc pack 4


suggestions on how to make this work properly would be appreciated.
Jan 16 '09 #1
6 4901
Stewart Ross
2,545 Expert Mod 2GB
Hi. To execute an SQL statement from VBA code you need to use either the DoCmd.RunSQL method, or the Execute method of the current database object. Trying to run the SQL the way you have done at present is certain to lead to the VBA compiler informing you there is a syntax error.

The normal way to approach this task is to build a SQL statement as a text string, then use RunSQL or Execute to execute the statement. Building it as a string is not difficult, but there are slight differences in approach if the WHERE clause is referring to a numeric value or a string, as string literals have to be enclosed in single quotes. Both versions are shown below, but make sure you use the correct one and delete the other in your final version:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Delete_Current_Assignment_Record_Click()
  2. Dim strSQL as String
  3. strSQL = "Delete * from Registration where [Registration ID] = '" & Me![select task].Column(7) & "'" ' this one is the string version
  4. strSQL = "Delete * from Registration where [Registration ID] = " & Me![select task].Column(7) ' this one is the numeric version
  5. CurrentDb.Execute strSQL
  6. End Sub
The Execute method does not generate any user warnings; RunSQL does. To use RunSQL instead of Execute the warnings have to be turned off then on again after execution as shown below:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings False
  2. DoCmd.RunSQL strSQL
  3. DoCmd.SetWarnings True
Welcome to Bytes!

-Stewart
Jan 16 '09 #2
Thank You...I am trying the CurrentDb.Execute version...but get an error

"too few parameters. expected 1." at the

CurrentDb.Execute strSQL statement.
Jan 22 '09 #3
This caused a prompt for me to enter a registration ID, and upon entering deleted all the information in the table, vice the record that had the same registration ID...it does reflect the right registration ID in debug, before running the do command, but prompts for entry of the Registration ID and then deletes all info in the table...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Delete_Current_Assignment_Record_Click()
  2. Dim strSQL As String
  3. strSQL = "Delete * from Registration where [Registration ID] = " & Me![select task].Column(7)
  4. DoCmd.SetWarnings False
  5. DoCmd.RunSQL strSQL
  6. DoCmd.SetWarnings True
Jan 22 '09 #4
I'm guessing that for some reason, when the "delete from" runs that the table Registration referenced in the code is not being addressed (current focus?) in any case, getting a prompt to key in the Registration ID when I run it."

any further suggestions are appreciated.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Delete_Current_Assignment_Record_Click()
  2. Dim strSQL As String
  3. strSQL = "Delete * from Registration where [Registration ID] = " & Me![select task].Column(7)
  4. DoCmd.SetWarnings False
  5. DoCmd.RunSQL strSQL
  6. DoCmd.SetWarnings True
Jan 23 '09 #5
Stewart Ross
2,545 Expert Mod 2GB
Hi again. When you use a RunSQL statement it 'does what it says on the tin'; there is no possibility of your SQL string not addressing table Registration - the SQL statement explicitly names that table and no other.

The fact that you are being asked for a value for Registration ID suggests that the underlying field in that table which you are wishing to match is not actually called 'Registration ID' but something else (e.g. RegistrationID). Check the actual name of the field in table Registration, because it must be spelled exactly as you see it in table design view. Otherwise, the SQL will be referring to a non-existent field and the delete will not function at all (since no existing row will match the SQL Where clause if it is referring to a non-existent field).

-Stewart
Jan 24 '09 #6
OldBirdman
675 512MB
At this point, I would use the tools Access supplies to check the SQL statement.

1. Stop the code at line 5, the DoCmd.RunSQL strSQL
2. In immediate window, display the contents of strSQL with ?strSQL - then copy entire string to clipboard
3. From Database window, select "Create query in Design view"
4. Close the ShowTable window without selecting anything
5. Click "SQL" on left end of query design toolbar (if not SQL, click downarrow next to designview icon
6. Clear "SELECT;"
7. Paste from clipboard your SQL statement.
8. Change DELETE to SELECT and click tableview icon on toolbar
9. Use designview to modify query until you get correct single record to display when you run query.
10. Switch to SQLview, and use this to compare with your string, still in immediate window. Replace "SELECT" with "DELETE"

OldBirdman
Jan 25 '09 #7

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

Similar topics

7
by: Megan | last post by:
Hi everybody- I inherited a database that somehow uses a bound combo box as a record selector. Let me give you some background. The form is based on data from 2 tables. The first table, Person,...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
13
by: ricky.agrawal | last post by:
I'm really not sure how to go about this in Access. What I've created is a table for each location. Those tables are identical in format but different in information. The tables are named after...
3
by: M.L. Abram | last post by:
Hello all, I do not know if this question is regarding table design, queries, or programming. Below, I have given a table design using Access 2003. Fields 'Product' and 'Color' are primary keys...
6
by: AA Arens | last post by:
Hi, I have a database with 2 main forms. Contacts and companies. I share the base with two others via LAN. On the companies form I have buttons to navigate throught the records (>400). We are...
1
by: JHite | last post by:
I am using Access 2003. My Form has 4 fields: MailingListID, auto generated – primary key LastName FirstName NickName I used the Combo box wizard to set up look-up box on the form. I answered...
1
by: anco85 | last post by:
Hi. Im a total access and VB noob and require your much appreciated help. I have a table that list all the vehicles in our company. I created a form to view this information much easier with a...
1
by: Ricardonz | last post by:
Hi, I am using access 2007 but saving as 2003 file. I have a form with 2 layers of sub-dataheets. When I delete a record by right clicking and selecting delete from the innermost datasheet the...
1
by: Kyosuke18 | last post by:
Hi everyone, I have a problem in deleting a data that is connected on the database.. I tried this code but it shows me an error: Run-time error '-2147217900(80040e14)': Syntax error in string in...
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: 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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.