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

Delete records from Table open in Form

9
Dear all,

I have started working with vba only three weeks ago so forgive me for any basic mistakes. I have a small problem, I am creating a database for work and I am stuck

I have made a search function to look up my User in "Tbl_Users". The form opens with a subquery showing the found records. So far so good.

What I am trying to implement is a 'Delete' Button, to delete the record selected in the SubQuery by the press of that button. This is the applicable part of my code so far:
Expand|Select|Wrap|Line Numbers
  1. Set rstdeleteuser = CurrentDb.OpenRecordset("Tbl_Users")
  2.         With rstdeleteuser
  3.             !UserID = gvSelectedUser
  4.             .Delete
  5.         End With
  6.     rstdeleteuser.Close
  7.     Set rstdeleteuser = Nothing
  8.  
This should usually work, the problem is however that the table is open through the subquery on my form.
Is there a way to temporarily close the table without losing my 'gvSelectedUser' (=Global Variable), or is there perhaps another way to do this?

Thanks!
May 14 '14 #1
6 3696
jimatqsi
1,271 Expert 1GB
Jen, it is not clear what you mean by "subquery.". If you mean only that the form is bound to a query and not the table itself, it should not be a problem.

You code is opening the table with no where clause so who knows what you're deleting. You could try this, but back up your work first. Get rid of all your code shown and use this instead.

Expand|Select|Wrap|Line Numbers
  1.     DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  2.     DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
  3.  
Jim
May 14 '14 #2
JenZzz
9
I'm sorry, It seems I haven't been clear and made a few mistakes. Let's start from scratch.
I have a Table named 'Tbl_Users'.
I basically want to delete a record from that table.
I have a Form called 'Show Users'. On this Form, there is a subform called 'Qry_Users subform'. This subform shows a Datasheet on the main form and gets it's data from a query, which in turn gets its data from 'Tbl_Users'.

So we have a main Form -> SubForm -> Query -> Table.
I want to delete a record from the Table by pressing a button on the Main Form.
The error I get with the code depicted in my post is exactly this:

Run-Time Error '3008'
The Table 'Tbl_Users'is already openend exclusively by another user, or it is already open through the user interface and cannot be manipulated programmatically.

This led me to believe the reason the code does not work is due to the table being used by the qry -> subform -> form itself!

The question is, how do I solve this? Can it be done by momentarily closing the table, or do I need to take a whole other approach?

Thanks!
May 14 '14 #3
JenZzz
9
Problem has been solved using the following code, seems I have made the problem seem more complex than it was.....
Expand|Select|Wrap|Line Numbers
  1.    Set rstdeleteuser = CurrentDb.OpenRecordset("Tbl_Users", dbOpenDynaset)
  2.         With rstdeleteuser
  3.             .FindFirst "UserID= '" & gvSelectedUser & "'"
  4.             If .NoMatch = False Then
  5.                 .Edit
  6.                 !UserID = gvSelectedUser
  7.                 .Delete
  8.             End If
  9.         End With
  10.     rstdeleteuser.Close
  11.     Set rstdeleteuser = Nothing
May 15 '14 #4
jimatqsi
1,271 Expert 1GB
Jen, the .Edit does nothing without a .Update. That is, every .Edit must be followed by a .Update to actually write the changes to disk.

But that's okay because there is no need to update a record you are about to delete. So within your If block you need only the .Delete instruction.

Jim
May 15 '14 #5
JenZzz
9
So the .Edit in my code here does nothing? Because it keeps giving me an error without it.
Anyway, thanks for the help :)
Jens
May 15 '14 #6
jimatqsi
1,271 Expert 1GB
It does? That's curious. I don't do a lot of record deleting but it seems to me I don't have to lock it with an .Edit before I do the delete. Might be some reason having to do with Access setting or form properties. I'll look into that but probably one of the more learned guys here can solve that mystery before me.

Jim
May 15 '14 #7

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

Similar topics

3
by: Craig Brown | last post by:
I would like to open selected (Filtered) records in a form. I currnetly have a custom menu but would like to only open certain records from i.e Button 1 will open Site 1 Button 2 will open...
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...
3
by: Joe | last post by:
Hi there, question regarding Access 2000 - locking records: I want to be able to lock a record (or several records) on a form so that particular record can not be edited until I undo the lock (im...
2
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying...
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: satish mullapudi | last post by:
Hi All, I am getting strange situation. These r the steps I have followed: 1. Created an EMPLOYEE table with around 14 fields & 688038 records. (so a large table indeed). 2. Tried to delete all...
5
by: Gareith Stanley | last post by:
Hi there I have an access form that allows the user to browse records (< and > buttons) sorted by another field within that table (eg, all entries under a staff group, all entries under a student...
6
by: scott.tang | last post by:
I'm experiencing a very strange problem. My application is MS Access front-end and MS SQL server back-end database. I have a SQL statement that deletes records from a table after an export...
6
by: kstevens | last post by:
I have tables setup with a main table for information and a subtable that records the "multiple" records for the main record. I have written a query to go in and find Null or "" values to delete...
13
mseo
by: mseo | last post by:
hi, I have a form for deleting records but I use unbound controls in the mainform to set the criteria of filtering the records that will be viewed in the subform I have delete command button in...
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: 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: 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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.