Code needed to delete and update records

coxmd410

New Member
Joined
Nov 20, 2012
Messages
14
I have an Access database that has one table containing employee information. I have created Forms for easy user-interface. I am having problems with one of my forms.

When looking at my main menu (a form) you see employee names. You can select a name and click an "edit/delete" button that opens a pop-up form showing all the information for that employee.

I have a button on that pop-up form to delete the employee you are viewing. The problem is my code is deleting all records in the table, not just the record being viewed.

Here is my code:

Private Sub deleteRecords_Click()

Dim db as Database
Set db = CurrentDb
db.Execute "Delete * From employees;"

End Sub

Also, could someone please explain what Dim db and Set db do? I just copied them from online? Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
As far as I can tell this what you are doing:

Dim db As Database - creates a variable to hold an Access Database
Set db = CurrentDb - assigns the current database to that variable. Basically you now have a reference to the database you're working on

db.Execute - execute a SQL statement on that database

The last line is where you're going wrong in my opinion. It needs to have a where clause something like:

db.Execute "Delete * From employees Where employeeId = " & txtBoxId.Value & ";"

employeeId = the field name in your database holding the employee id
txtboxId = a text box holding the currently selected employee id number

Not sure about this as I don't use Access a lot but thats what it look like
HTH!
 
Upvote 0
I ran this and it gave me error 424. No Object. Any idea why?

As far as I can tell this what you are doing:

Dim db As Database - creates a variable to hold an Access Database
Set db = CurrentDb - assigns the current database to that variable. Basically you now have a reference to the database you're working on

db.Execute - execute a SQL statement on that database

The last line is where you're going wrong in my opinion. It needs to have a where clause something like:

db.Execute "Delete * From employees Where employeeId = " & txtBoxId.Value & ";"

employeeId = the field name in your database holding the employee id
txtboxId = a text box holding the currently selected employee id number

Not sure about this as I don't use Access a lot but thats what it look like
HTH!
 
Upvote 0
So you ran this?

Code:
[COLOR=#333333]Private Sub deleteRecords_Click()[/COLOR]

[COLOR=#333333]Dim db as Database[/COLOR]
[COLOR=#333333]Set db = CurrentDb[/COLOR]
[COLOR=#333333]db.Execute "Delete * From employees [/COLOR]Where employeeId = " & txtBoxId.Value & ";"

[COLOR=#333333]End Sub[/COLOR]

Try running the one below instead. What line does it error on?
Code:
[COLOR=#333333]Private Sub deleteRecords_Click()[/COLOR]

[COLOR=#333333]Dim db as Database[/COLOR]
[COLOR=#333333]Set db = CurrentDb
[/COLOR]
Debug.Print txtBoxId.Value
[COLOR=#333333]db.Execute "Delete * From employees [/COLOR]Where employeeId = " & txtBoxId.Value & ";"

[COLOR=#333333]End Sub[/COLOR]

Hopefully this should narrow down the problem...
 
Upvote 0
That worked, thanks!

I only have one last question. When looking at the form, the user sees all the employee information listed. I want to give the user the ability to type over what is currently showing and click a "save" button that will update the employee table with all the information they typed in.
 
Upvote 0
Not sure if you're thinking of other strategies, but if you bind your form to the table, then edits are all handled automatically by Access. It is always possible to use the button wizard to add a save button (which is unnecessary but seems to make users feel good).
 
Upvote 0

Forum statistics

Threads
1,203,752
Messages
6,057,152
Members
444,908
Latest member
Jayrey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top