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

Delete selected value from listbox

29
Hi,
I am trying to run a delete sql statement. i want it to delete from a list box, and delete the one which is currently selected.
i have tried a few things, but hane not had success

Please help[
Apr 17 '07 #1
16 13904
Rabbit
12,516 Expert Mod 8TB
Hi,
I am trying to run a delete sql statement. i want it to delete from a list box, and delete the one which is currently selected.
i have tried a few things, but hane not had success

Please help[
What have you tried?
Apr 17 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
Hi,
I am trying to run a delete sql statement. i want it to delete from a list box, and delete the one which is currently selected.
i have tried a few things, but hane not had success

Please help[
What is the row source of the listbox (in SQL)?
Apr 18 '07 #3
Shokoth
29
My list is called lstViewRequest, and this is i have added a delete query into is, and right now, it will ask you for the record you weant to delete.
this is the code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveRecord_Click()
  2.  
  3.   strSQL = "DELETE * " & _
  4.             "FROM [tblRequest] " & _
  5.             "WHERE (([tblRequest].[Request ID])=([Please Enter The Job You Have Accpeted])) ;"
  6.             '"WHERE (([tblRequest].[Request ID])=([lstViewRequest].[Request ID])) ;"
  7.   DoCmd.RunSQL strSQL
  8.  
  9. On Error GoTo Err_cmdSaveRecord_Click
  10.  
  11.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  12.  
  13. Exit_cmdSaveRecord_Click:
  14.  
  15. Err_cmdSaveRecord_Click:
  16.  
  17.     Exit Sub
  18.     MsgBox Err.Description
  19.     Resume Exit_cmdSaveRecord_Click
  20.     End Sub
Apr 18 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
You can't refer to the selected item on the list like this

Expand|Select|Wrap|Line Numbers
  1. ([lstViewRequest].[Request ID])
Try something like this ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveRecord_Click()
  2. On Error GoTo Err_cmdSaveRecord_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [tblRequest] " & _
  7.       "WHERE [tblRequest].[Request ID]=" & Me![lstViewRequest]
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstViewRequest].Requery ' requery the list
  10.  
  11. Exit_cmdSaveRecord_Click:
  12.  
  13. Err_cmdSaveRecord_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdSaveRecord_Click
  17.  
  18.     End Sub
  19.  
Mary
Apr 18 '07 #5
Shokoth
29
that still asks for the ID as well, but this time, the message that pops up is the name this is linked with the record ID.
so if record 10 is selected, the the name (i.e. Frank) comes up on the message box.
Is there no way for that message box not to come up, but jus remove automatically
Apr 18 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
that still asks for the ID as well, but this time, the message that pops up is the name this is linked with the record ID.
so if record 10 is selected, the the name (i.e. Frank) comes up on the message box.
Is there no way for that message box not to come up, but jus remove automatically
The listbox is obviously bound to the name and not the ID.

What is the Row Source SQL of the listbox?
What is the Bound Column Set to?
Apr 18 '07 #7
Shokoth
29
this is the sql statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblRequest.[Request ID], tblRequest.[Request By], tblRequest.[Request Description], tblRequest.[Requested Date], tblRequest.Budget, tblRequest.[Duration (Hours)] FROM tblRequest; 
  2.  
and youre right, it is bounded to column 2, the name field.
Will I have to remove that list, and add another list, and make it have no bounds
Apr 18 '07 #8
Shokoth
29
this is the sql statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblRequest.[Request ID], tblRequest.[Request By], tblRequest.[Request Description], tblRequest.[Requested Date], tblRequest.Budget, tblRequest.[Duration (Hours)] FROM tblRequest; 
and youre right, it is bounded to column 2, the name field.
Will I have to remove that list, and add another list, and make it have no bounds.

The reason why i bounded it is so that the information on that filed goes there.
I was typing sql insert statements in sql, but it wasnt working.

basically, i have two lists there, both which add to the actual form (wihci is linked to the a job table). The first list box adds the person who is going to participate in that job, and the second list adds the service that is being provided for that job.
Apr 18 '07 #9
MMcCarthy
14,534 Expert Mod 8TB
this is the sql statement:

SELECT tblRequest.[Request ID], tblRequest.[Request By], tblRequest.[Request Description], tblRequest.[Requested Date], tblRequest.Budget, tblRequest.[Duration (Hours)] FROM tblRequest;

and youre right, it is bounded to column 2, the name field.
Will I have to remove that list, and add another list, and make it have no bounds
Just set the Bound column to column 1 and try running the code I gave you again.
Apr 18 '07 #10
Shokoth
29
Just set the Bound column to column 1 and try running the code I gave you again.
If i bind it there, it wont enable me to add whats in that field to the required field.

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddItem()
  2.   Dim Nrec As Integer     ' Number of records with item (=0 or 1)
  3.   Dim intJobID As Integer
  4.   Dim ServiceUser As String
  5.   Dim ServiceProvider As String
  6.   Dim ServiceUsed As String
  7.   Dim strRequests As String
  8.   Dim strServices As String
  9.   Dim strRequestCriteria As String
  10.   Dim strServiceCriteria As String
  11.   Dim strSQL As String
  12. '
  13. ' If no item is selected, then dispaly warning message and exit sub
  14. '
  15.   If IsNull(lstRequests) Then
  16.   MsgBox vbCrLf & "You must select a Request" & _
  17.          vbCrLf & vbCrLf, vbExclamation, "Grid Accounting & Billing System"
  18.  
  19.    Exit Sub
  20.  End If
  21.  
  22.     'intJobID = [Job ID]
  23.     ServiceUser = lstRequests
  24.     ServiceUsed = lstServices
  25.     ServiceProvider = lstServices
  26.     strCriteria = "([Job ID]=" & intJobID & ") And ([Service User]= '" & ServiceUser & "') AND ([Service Used]= '" & ServiceUsed & "' AND ([Service Provider]= '" & ServiceProvider & "')"
  27.  
  28.    strSQL = "INSERT INTO [tblJobs]([JobID], [Service Used], [Service Provider], [Service User], [Start Date], [Duration]) " & _
  29.    "VALUES (forms![FrmJobs].[Job ID], forms![frmJobs].lstServices.Column(1), forms![frmJobs].lstServices.Column(2), forms![frmJobs].lstServices.Column(2), forms![FrmJobs].lstRequests.Column(4), forms![FrmJobs].lstRequests.Column(6),1);"
  30.  
  31.    DoCmd.RunSQL strSQL
  32. End Sub
Apr 18 '07 #11
MMcCarthy
14,534 Expert Mod 8TB
Change to ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveRecord_Click()
  2. On Error GoTo Err_cmdSaveRecord_Click
  3. Dim strSQL As String
  4.  
  5.    strSQL = "DELETE * " & _
  6.       "FROM [tblRequest] " & _
  7.       "WHERE [tblRequest].[Request ID]=" & Me![lstViewRequest].Column(0)
  8.    DoCmd.RunSQL strSQL
  9.    Me![lstViewRequest].Requery ' requery the list
  10.  
  11. Exit_cmdSaveRecord_Click:
  12.  
  13. Err_cmdSaveRecord_Click:
  14.        Exit Sub
  15.    MsgBox Err.Description
  16.    Resume Exit_cmdSaveRecord_Click
  17.  
  18.     End Sub
  19.  
In code the columns start at 0

Mary
Apr 18 '07 #12
Shokoth
29
yay. thank you very much. it works.

PS: wen an update or a delte query is run, message box comes up confirming this, is there a possible way to avoid or stop these messages from comming up
Apr 18 '07 #13
MMcCarthy
14,534 Expert Mod 8TB
You're welcome. The following additions will turn the warning off for the duration of the query.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveRecord_Click()
  2. On Error GoTo Err_cmdSaveRecord_Click
  3. Dim strSQL As String
  4.  
  5.    DoCmd.SetWarnings False
  6.    strSQL = "DELETE * " & _
  7.       "FROM [tblRequest] " & _
  8.       "WHERE [tblRequest].[Request ID]=" & Me![lstViewRequest].Column(0)
  9.    DoCmd.RunSQL strSQL
  10.    Me![lstViewRequest].Requery ' requery the list
  11.  
  12.    DoCmd.SetWarnings True
  13.  
  14. Exit_cmdSaveRecord_Click:
  15.  
  16. Err_cmdSaveRecord_Click:
  17.        Exit Sub
  18.    MsgBox Err.Description
  19.    Resume Exit_cmdSaveRecord_Click
  20.  
  21.     End Sub
  22.  
Mary
Apr 18 '07 #14
Shokoth
29
Thank you.
Sorry to be a bother, but i have one more question.
i have a login page, where members can log, and their user name and password is fetched from the members table.
I want to have 2 different authorisation level.
One members authorisation, so wen they login, only their records will show up, no others, and they cannot access it as well, and one admin level, where obviously they can do whateva they please.

Any suggestions?
Apr 18 '07 #15
You're welcome. The following additions will turn the warning off for the duration of the query.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveRecord_Click()
  2. On Error GoTo Err_cmdSaveRecord_Click
  3. Dim strSQL As String
  4.  
  5.    DoCmd.SetWarnings False
  6.    strSQL = "DELETE * " & _
  7.       "FROM [tblRequest] " & _
  8.       "WHERE [tblRequest].[Request ID]=" & Me![lstViewRequest].Column(0)
  9.    DoCmd.RunSQL strSQL
  10.    Me![lstViewRequest].Requery ' requery the list
  11.  
  12.    DoCmd.SetWarnings True
  13.  
  14. Exit_cmdSaveRecord_Click:
  15.  
  16. Err_cmdSaveRecord_Click:
  17.        Exit Sub
  18.    MsgBox Err.Description
  19.    Resume Exit_cmdSaveRecord_Click
  20.  
  21.     End Sub
  22.  
Mary
Hi,
i have used this code and found it very useful so thankyou very much.
however when i click to delete the item from the list, a Message box appears asking me to enter a prameter 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?

any help gratefully received,
Thanks
Feb 14 '08 #16
Scott Price
1,384 Expert 1GB
Hello, hooijdonk.

Please refrain from posting your questions in another member's thread in the future. It results in your question not being viewed nor understood. This is also called thread hijacking and isn't allowed per the site rules. Go ahead and start a new thread explaining your situation and if needed include a hyperlink to the old thread but copying the URL in your browser's URL window, at the top of the Message window on this site you will see an icon that looks like a globe. Clicking on it will show the URL tag dialog box. Paste your URL in the second window of the dialog box and click OK.

Thanks,
Scott MODERATOR
Feb 14 '08 #17

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

Similar topics

8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
1
by: David J | last post by:
Hi, I am trying again, and seriously struggling. My problem is that I am trying to set the selected value in a listbox equal to a value of a textbox, when you op the listbox. I am not getting that...
2
by: jim Bob | last post by:
Hi, I have a form with a list box that shows the contents of a table and managed to create an add record button with the wizard. (DoCmd.GoToRecord , , acNewRec) Now i want to make a delete...
1
by: acord | last post by:
Hi, I am having problem to get a value of the selected item from a dropdown listbox. Here is the JS function; function getSelectedItem(objSelect) { alert("in getSelectedItem"); alert...
2
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
2
by: compwizard | last post by:
hello all, Just a quick query I currently have an hires form where clients place orders, When an item is selected this would drop the item within a listbox and save within a itemhireline...
1
by: angelicdevil | last post by:
i have listbox 1 which displays status , based on selection of status listbox 2 displays usernames. and based on username selected the textbox displays the email id. its working fine till...
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: 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
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:
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
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.