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

Deleting List item using Form

Hi,
using a form I have been trying to get a list box to display the values of database table and have two options on there.
a) the user can select a list item and click a button to delete that item
b) the user can type text into a text box and click another button to add that value to the list box (and therefore the table sitting behind it)

i found a very useful starting point here http://www.thescripts.com/forum/thread634077.html

however, when i click to delete the item from the list, a Message box appears asking me to enter a parameter value. If the value entered is exactly the same as item chosen on the list then the item is removed, otherwise 0 items get removed. have i not done something right?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant]=" & [lstConsultant].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17.  
  18.  
  19.  
  20. End Sub
any help much appreciated,
Thanks hooijdonk
Feb 14 '08 #1
9 3095
Hi,
using a form I have been trying to get a list box to display the values of database table and have two options on there.
a) the user can select a list item and click a button to delete that item
b) the user can type text into a text box and click another button to add that value to the list box (and therefore the table sitting behind it)

i found a very useful starting point here http://www.thescripts.com/forum/thread634077.html

however, when i click to delete the item from the list, a Message box appears asking me to enter a parameter value. If the value entered is exactly the same as item chosen on the list then the item is removed, otherwise 0 items get removed. have i not done something right?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant]=" & [lstConsultant].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17.  
  18.  
  19.  
  20. End Sub
any help much appreciated,
Thanks hooijdonk
Hi Hooijdonk,

Would you be able to provide the source for your list box? and if different the structure of the underlying table?

Kind Regards,
Ken Farrawell.
Feb 14 '08 #2
ADezii
8,834 Expert 8TB
Hi,
using a form I have been trying to get a list box to display the values of database table and have two options on there.
a) the user can select a list item and click a button to delete that item
b) the user can type text into a text box and click another button to add that value to the list box (and therefore the table sitting behind it)

i found a very useful starting point here http://www.thescripts.com/forum/thread634077.html

however, when i click to delete the item from the list, a Message box appears asking me to enter a parameter value. If the value entered is exactly the same as item chosen on the list then the item is removed, otherwise 0 items get removed. have i not done something right?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant]=" & [lstConsultant].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17.  
  18.  
  19.  
  20. End Sub
any help much appreciated,
Thanks hooijdonk
Try (pay particular attention to Line #7):
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant]='" & Me![lstConsultant].Column(0) & "'"
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17. End Sub
Feb 14 '08 #3
Thanks for your replies. Sorry i should have stated that the code i place above was part of a working solution. Firstly i had the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE 'Consultant' " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant].[Request ID] =" & Me![lstConsultant].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17.  
  18. End Sub
this prompted for the Parameter Value Consultant.Request ID and then for the Parameter Value of the selected Item. If both values entered were identical and in the list then the whole list would be deleted! To move on from here i changed the SQL statement to
Expand|Select|Wrap|Line Numbers
  1. "WHERE [Consultant] =" & Me![lstConsultant].Column(0)
this just asked for one parameter value (in the prompt box the value selected in the list is also displayed) and if the value entered matched the selected list item then this single item would be deleted. I therefore think one side of the WHERE clause isn't working correctly, however i'm not sure which side or why.

The listbox was just created using the wizard function and table structure is just a single column 'Consultant' from which i asked the listbox wizard tool to read the values of. Not sure if that helps. Sorry am still getting to grips with VB and VBA
Feb 15 '08 #4
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
if u use forms use unbound controls then it is easy to delete anything in any record...
Feb 15 '08 #5
ADezii
8,834 Expert 8TB
Thanks for your replies. Sorry i should have stated that the code i place above was part of a working solution. Firstly i had the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelConsultant_Click()
  2. On Error GoTo Err_cmdDelConsultant_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE 'Consultant' " & _
  6.       "FROM [Consultant] " & _
  7.       "WHERE [Consultant].[Request ID] =" & Me![lstConsultant].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstConsultant].Requery ' requery the list
  10.  
  11. Exit_cmdDelConsultant_Click:
  12.  
  13. Err_cmdDelConsultant_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdDelConsultant_Click
  17.  
  18. End Sub
this prompted for the Parameter Value Consultant.Request ID and then for the Parameter Value of the selected Item. If both values entered were identical and in the list then the whole list would be deleted! To move on from here i changed the SQL statement to
Expand|Select|Wrap|Line Numbers
  1. "WHERE [Consultant] =" & Me![lstConsultant].Column(0)
this just asked for one parameter value (in the prompt box the value selected in the list is also displayed) and if the value entered matched the selected list item then this single item would be deleted. I therefore think one side of the WHERE clause isn't working correctly, however i'm not sure which side or why.

The listbox was just created using the wizard function and table structure is just a single column 'Consultant' from which i asked the listbox wizard tool to read the values of. Not sure if that helps. Sorry am still getting to grips with VB and VBA
convexcube had the right idea, kindly Post the Row Source for your List Box.
Feb 15 '08 #6
convexcube had the right idea, kindly Post the Row Source for your List Box.
sorry i'm not sure what you mean by the row source. the Consultants table behind the listbox currently just has dummy values in of single and multiple random characters. In the properties of the Listbox the row source is simply Consultant. I'm aware this probably isn't what you mean?

thanks again for the replies,
Hooijdonk
Feb 15 '08 #7
ADezii
8,834 Expert 8TB
sorry i'm not sure what you mean by the row source. the Consultants table behind the listbox currently just has dummy values in of single and multiple random characters. In the properties of the Listbox the row source is simply Consultant. I'm aware this probably isn't what you mean?

thanks again for the replies,
Hooijdonk
If the Table Name is Consultants, then the Row Source should be the name of the Table itself, 'Consultants' and not 'Consultant'. Kindly post the Fields in the Consultants Table along with their Data Types in the proper order.
Feb 15 '08 #8
Hi yes sorry about the error there, the table is Consultant and it currently just has one field 'Consultant' and it's data type is simply Text with field size 50.
could adding the ID field and somehow using that in part of the WHERE query help?
Feb 18 '08 #9
ADezii
8,834 Expert 8TB
Hi yes sorry about the error there, the table is Consultant and it currently just has one field 'Consultant' and it's data type is simply Text with field size 50.
could adding the ID field and somehow using that in part of the WHERE query help?
Given your statements, then:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. strSQL = "DELETE [Consultant] FROM Consultant WHERE " & _
  4.           "Consultant.[Consultant] = '" & Me![lstConsultant] & "'"
  5. DoCmd.RunSQL strSQL
  6.  
  7. Me![lstConsultant].Requery ' requery the list
BTW, it is seldom a good idea to give your Table, and a Field contained within it, the same Name.
Feb 19 '08 #10

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

Similar topics

3
by: bbutell | last post by:
I add items to a list of ids within a <TD> named lstCarts using the following code: function lstCarts_ondblclick() { index = NewProgram.lstCarts.selectedIndex; if (index != -1) { vID =...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
10
by: A_PK | last post by:
I am writing mobile application using Vb.net when i click the last row of list view, and try to delete it.... will promtpy the following message "Additional information:...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
0
by: steven.shannon | last post by:
Hello, I'm writing an app that involves deleting all the items in a specified Outlook folder regardless of item type (i.e. Contacts, Tasks, etc.). This code was ported from VBA where it worked...
1
by: tsuki | last post by:
Hello, I have a problem concerning DOM and XML. The structure of my xml is the following: <?xml version="1.0" encoding="utf-8"?> <website> <language1> <seite> <bereich1>
6
by: yc022 | last post by:
Hi all, this is my first time using this so i'm not really sure how it works. please bear with me. i am trying to delete a row in a database using a select list. First of all i have a query to...
2
by: aberry | last post by:
I have strange issue when deleting element of list which I'm browsing. Here is the code , please have a look. >>> li = >>> for name in li: print name mike anil jassi >>> for name in li:
4
by: sphinney | last post by:
I'm not exactly sure how to start this post. My question is pretty simple, but it will take a little bit of context before I can state it. (And thanks in advance for taking the time to read this!) ...
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
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:
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.