473,399 Members | 2,146 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,399 software developers and data experts.

How to delete entry in listbox, and prevent textbox re-population?

137 100+
This thread has two questions, however they are linked around the same area and solving one may solve the other:

A) I've created a form for adding different records, complete with a subform for applying requirements to each record.
Within the subform, when you add a data requirement - via a textbox - I've also made a listbox to display what requirements the user has added so far. This also means that should a user make a mistake, they can select the option in the list, and click a delete button to remove the entry.
However, I can't get the delete function to work - code such as the following has not worked:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3.     strSQL = "DELETE * " & _
  4.         "FROM [DataRequirements] " & _
  5.         "WHERE [DataRequirements].[Data Requirement]=" & Me![List6].Column(0)
  6.     DoCmd.RunSQL strSQL
  7.     Me![List6].Requery
  8.  
  9. End Sub
  10.  
DataRequirements is the name of the Subform.
Data Requirement is a column name within the subform.
List6 is the name of the listbox.

Any idea how I can delete the entry in the listbox - this should in theory delete the entire row from the subform's table.

B) As well as this, one thing I've noticed is if I select an option in the listbox to delete, when I do so this re-populates the entry into the textbox used to add the requirements. This means that if I do this, and then close the form, it ends up duplicating that entry because the text has appeared again in the textbox - is there a way to prevent the listbox selection from re-populating the textbox?

Any help on either of the two questions would be muchly appreciated. I've had to quickly teach myself MS Access having never used it before, so am not an expert yet - my knowledge of Access is intermediate while my knowledge of VBA is slightly worse, so I should be able to understand most of your answer.

Thanks.
Nov 29 '10 #1

✓ answered by mshmyob

Ok I have made some minor changes.

To reflect how things in your app should be I have checked for duplicates BEFORE adding to the database. You should never allow a person to add duplicates if they are not allowed.

I have therfore done the following:

1. I left my listbox code the way I had it originally
2. I changed the code for when you add your data requirements to check for duplicates immediately and not allow them
3. I fixed problem "B" by removing the "Control Source" property for your listbox (since it was bound to DataRequirment.

Code for checking for duplicates - Your Add Requirment button:

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Command43_Click_Err
  2. Dim lngDupCheck
  3.  
  4.     On Error Resume Next
  5.     If IsNull(Me.Ref_ID) Then
  6.     MsgBox "You cannot add data requirements without creating a test.", vbExclamation + vbOKOnly
  7.     Else
  8.     If IsNull(Me.Data_Requirement) Then
  9.     MsgBox "You cannot add a blank data requirement.", vbExclamation + vbOKOnly
  10.     Else
  11.     ' check for duplicate data requirement and reject if so
  12.     If IsNull(DLookup("Data_ID", "DataRequirements", "[DataRequirements].[Data_Requirement]= '" & Me.Data_Requirement & "' AND [DataRequirements].[REF_ID]= " & Forms!TestCreator.Ref_ID)) Then
  13.         DoCmd.RunCommand acCmdSaveRecord
  14.         DoCmd.GoToRecord , , acNewRec
  15.         Me.List6.Visible = True
  16.         Me.Label10.Visible = True
  17.         Me.Command8.Visible = True
  18.     Else
  19.         MsgBox ("Duplicate Data requirment already exists")
  20.         Me.Undo
  21.     End If
  22.     End If
  23.     End If
  24.  
  25.     If (MacroError <> 0) Then
  26.         Beep
  27.         MsgBox MacroError.Description, vbOKOnly, ""
  28.     End If
  29.  
  30.  
  31. Command43_Click_Exit:
  32.     Exit Sub
  33.  
  34. Command43_Click_Err:
  35.     MsgBox Error$
  36.     Resume Command43_Click_Exit
  37.  
I have also reattached the data base for you.

cheers,

18 2625
mshmyob
904 Expert 512MB
Looking quickly your SQL statement looks wrong.

You are using a SQl Delete command and therefore you need to indicate the TABLE you are deleting from NOT the subform.

Also the WHERE clause needs to indicate the column name in the table not the subform control name (unless they are both the same).

Change your subform name to your table name and the subform control name to the actual column name in the table.

Then let us know if that works or what error you get.

cheers,
Dec 1 '10 #2
Adam Tippelt
137 100+
Thanks for the reply mshmyob.

I've just realised a mistake I put in my initial post - I wrote the wrong descriptions. What it should have read is:

DataRequirements is the table.
Data Requirement is a column within the table.
List6 is the listbox on the form.

With this in mind, the SQL already holds true to what you were suggesting in the first sentence.

Though I'm still not sure about the WHERE clause. The SQL is supposed to indicate that if the option is clicked/highlighted in the listbox, then that's the row that gets deleted from the table - I don't think the syntax there is right, but I don't know what to use.

Currently when I click the button, nothing happens. It doesn't work, but at the same time doesn't produce any errors. It just simply does nothing.

Any other help would be muchly appreciated.

Thanks.
Dec 8 '10 #3
ADezii
8,834 Expert 8TB
  1. Is the [Data Requirement] Field a String?
  2. Is the Delete Button in the Main or Sub-Form?
Dec 8 '10 #4
Adam Tippelt
137 100+
1. [Data Requirement], now renamed as [Data_Requirement] is a text field, so yes it's a string.

2. Delete button is in the sub-form, as I only want it to delete the data requirement attached, instead of the whole record.
Main form is connected to a separate table, named TestLibrary.
Dec 8 '10 #5
ADezii
8,834 Expert 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. "WHERE [DataRequirements].[Data_Requirement]='" & Me![List6].Column(0) & "'"
Dec 8 '10 #6
Adam Tippelt
137 100+
Aha, that's got the functionality working - can you explain why what you did worked?

It's working, however there is an issue:

If a user inputted the same requirement twice, and then realised this and went to delete one, it won't let you delete just the one you've selected, but instead deletes all of them with indentical names. Can I stop it from doing this?
Is it a case of referencing a different column so that it checks the unique ID number in the table matches the listbox one, instead of the data requirement? The listbox only displays the data requirement column, however a unique ID column does exist.

Thanks.
Dec 8 '10 #7
ADezii
8,834 Expert 8TB
Aha, that's got the functionality working - can you explain why what you did worked?
Since the [Data_Requirement] Field is TEXT, it needs to be Delimited by "'s" in the WHERE Clause Expression.

Expand|Select|Wrap|Line Numbers
  1. "WHERE [DataRequirements].[<UniqueID>]=" & Me![List6].Column(?)"
Dec 8 '10 #8
mshmyob
904 Expert 512MB
That is correct. All records matching your Where clause will be deleted. If you wish to delete a single record you must use some kind of identifier that is unique to the record - usually the Primary Key. Deleting a specific record by the Primary key is the preferred way to go.

cheers,
Dec 8 '10 #9
Adam Tippelt
137 100+
Yes I would like to change it to delete via the Primary Key. However, the list box only displays the non-primary column, and nothing else, so is it still possible to do? I can't remember entirely, but as the listbox always seems to pull through the Primary Key, the ID column should have been included in the setup, but hidden from view.

As the only displayed column is column 0, how would I reference a hidden column, if that sort of thing is possible? Or would I want a different approach?

Thanks.
Dec 9 '10 #10
mshmyob
904 Expert 512MB
You are correct that the first column is usually based on the primary key.

Could you post the rowsource for your combo box.

Just click on the rowsource and press Shift F2 to zoom and then copy and paste.

cheers,
Dec 9 '10 #11
Adam Tippelt
137 100+
Might just point out that it's not a combo box, it's a listbox.

The rowsource is a query I built, so zooming on it only produces the name of the query, which isn't much help, so I've pasted in the SQL for the query:

SELECT DataRequirements.Data_Requirement
FROM DataRequirements
WHERE (((DataRequirements.Ref_ID)=[forms]![TestCreator]![Ref_ID]));

As I can see now, it's not pulling through the Primary Key (Script_ID) at all, which is probably why it can't find it. However just adding it into the SELECT row makes the form unusable, because it throws up a message box asking for an input value of Script ID, which is now where I'm stuck at.

Cheers.
Dec 10 '10 #12
mshmyob
904 Expert 512MB
Could you attach this part of your database (the query, tables related to this form , and the form itself) so I can look at it?

cheers,
Dec 10 '10 #13
Adam Tippelt
137 100+
I've attached the areas of the database concerning this issue.

Please note that I do have two 'delete buttons' that I'm working on, one is delete single and one is delete all - this thread is concerning the delete single option.

Let me know if I need to explain anything.

Thanks.
Attached Files
File Type: zip Delete Single Sub-Record Problem.zip (119.2 KB, 96 views)
Dec 15 '10 #14
mshmyob
904 Expert 512MB
Your Delete button should be like so:

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Err_Command8_Click
  2. Dim strSQL As String
  3. Dim lngItem As Long
  4.  
  5. 'Delete Record Button.
  6. 'Allows user to delete single data requirements that they want to remove.
  7. lngItem = Me.List6.ListIndex
  8.  
  9.     strSQL = "DELETE * " & _
  10.         "FROM [DataRequirements] " & _
  11.         "WHERE [DataRequirements].[Data_Requirement]= '" & Me.List6.ItemData(lngItem) & "'"
  12.      strSQL = strSQL & " AND [DataRequirements].[REF_ID]= " & Forms!TestCreator.Ref_ID
  13.     DoCmd.RunSQL strSQL
  14.     Me![List6].Requery
  15.  
  16. Exit_Command8_Click:
  17.  
  18. Err_Command8_Click:
  19.         Exit Sub
  20.     MsgBox Err.Description
  21.     Resume Exit_Command8_Click
  22.  
Notice I changed your SQL string only.
1. You need to reference the value selected from your list box using the "listindex and itemdata" methods.
2. You need to further filter your WHERE clause by including the "Ref_ID" since many records could have the same "data requirement".

Hope this helps.

cheers,
Dec 15 '10 #15
Adam Tippelt
137 100+
That half solved the problem...

Ref_ID is a foreign key to the TestLibrary, whereas Data_ID is the primary key. Using that setup means that deleting a requirement doesn't delete identical requirements connected to other tests, however if you've accidentally written the same requirement twice for one test, you still end up deleting both.

What you wrote did provide me with something I was missing though (lngItem), and what you've suggested has helped me find an answer:

I changed the listbox query so that it also pulled through the Data_ID. Then I altered the code to:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2. On Error GoTo Err_Command8_Click
  3. Dim strSQL As String
  4. Dim lngItem As Long
  5.  
  6. lngItem = Me.List6.ListIndex
  7.  
  8.     strSQL = "DELETE * " & _
  9.         "FROM [DataRequirements] " & _
  10.         "WHERE [DataRequirements].[Data_Requirement]= '" & Me.List6.ItemData(lngItem) & "'"
  11.         strSQL = strSQL & " AND [DataRequirements].[Data_ID]= " & Me.List6.Column(1)
  12.     DoCmd.RunSQL strSQL
  13.     Me![List6].Requery
  14.  
  15. Exit_Command8_Click:
  16.  
  17. Err_Command8_Click:
  18.         Exit Sub
  19.     MsgBox Err.Description
  20.     Resume Exit_Command8_Click
  21.  
  22. End Sub
  23.  
I also changed the listbox so that it pulled through both columns, but with the ID column at width 0cm, so that it remains hidden.

This seems to work how I want it to, so thank you for your help, couldn't have got there without your suggestions.
Now all I'm stuck on is problem B)....
Dec 20 '10 #16
mshmyob
904 Expert 512MB
Ok I have made some minor changes.

To reflect how things in your app should be I have checked for duplicates BEFORE adding to the database. You should never allow a person to add duplicates if they are not allowed.

I have therfore done the following:

1. I left my listbox code the way I had it originally
2. I changed the code for when you add your data requirements to check for duplicates immediately and not allow them
3. I fixed problem "B" by removing the "Control Source" property for your listbox (since it was bound to DataRequirment.

Code for checking for duplicates - Your Add Requirment button:

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo Command43_Click_Err
  2. Dim lngDupCheck
  3.  
  4.     On Error Resume Next
  5.     If IsNull(Me.Ref_ID) Then
  6.     MsgBox "You cannot add data requirements without creating a test.", vbExclamation + vbOKOnly
  7.     Else
  8.     If IsNull(Me.Data_Requirement) Then
  9.     MsgBox "You cannot add a blank data requirement.", vbExclamation + vbOKOnly
  10.     Else
  11.     ' check for duplicate data requirement and reject if so
  12.     If IsNull(DLookup("Data_ID", "DataRequirements", "[DataRequirements].[Data_Requirement]= '" & Me.Data_Requirement & "' AND [DataRequirements].[REF_ID]= " & Forms!TestCreator.Ref_ID)) Then
  13.         DoCmd.RunCommand acCmdSaveRecord
  14.         DoCmd.GoToRecord , , acNewRec
  15.         Me.List6.Visible = True
  16.         Me.Label10.Visible = True
  17.         Me.Command8.Visible = True
  18.     Else
  19.         MsgBox ("Duplicate Data requirment already exists")
  20.         Me.Undo
  21.     End If
  22.     End If
  23.     End If
  24.  
  25.     If (MacroError <> 0) Then
  26.         Beep
  27.         MsgBox MacroError.Description, vbOKOnly, ""
  28.     End If
  29.  
  30.  
  31. Command43_Click_Exit:
  32.     Exit Sub
  33.  
  34. Command43_Click_Err:
  35.     MsgBox Error$
  36.     Resume Command43_Click_Exit
  37.  
I have also reattached the data base for you.

cheers,
Attached Files
File Type: zip Delete Single Sub-Record Problem2.zip (103.9 KB, 109 views)
Dec 20 '10 #17
Adam Tippelt
137 100+
Hmmm I don't know why I didn't think about that before...It's hard to tell whether a situation might arise where duplicates WOULD be required...however that could easily be solved by changing the reject box to a confirmation box to check if the user wanted to add a duplicate....hmm ideas ideas ideas....

Either way, you've fixed both my problems now. I really must remember to check control sources first before asking questions!

Thank you very much for your help.
Dec 20 '10 #18
mshmyob
904 Expert 512MB
You're welcome.

cheers,
Dec 20 '10 #19

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

Similar topics

2
by: Jonathan Dixon | last post by:
I have a list box called "listbox1" and a text box call "testbox1" I have read all the msdn stuff about drag and drop. Can somebody throw me a bone and tell me how to drag from the listbox into...
2
by: Sparky Arbuckle | last post by:
I've created 2 listboxes with buttons that transfer items between the two. The idea is that one of the listboxes will have a collection of items to be deleted from the database. I haven't found any...
5
by: Kent Briggs | last post by:
VB 2005 Express. I have a readonly multiline textbox with a vertical scrollbar. I'm programatically adding text to it with textbox1.appendtext(mytext) and it scrolls automatically. However, I only...
3
by: BrendanMcPherson | last post by:
How can I get a listbox to change a textbox on some selections. I have a listbox which only needs to be filled in on some selections. Is there a way I can have on some selections the textbox go...
1
by: gayano | last post by:
HI all, I have 2 forms 1. MainForm 2. SubForm Then I have a listbox in the subform and textbox in the mainform. (when i click on the listbox item) (i dont want multi selections) I want...
3
by: Mike | last post by:
Is there a way to determine if a user deleted text in a asp:textbox? I have a textbox were users can enter in a product number, then they click a button to see if the product numbers(s) exists in...
3
by: DaWofl | last post by:
Iam reading text from a textfile iam splitting it by # at te end of the line so get al lines into my listbox like this aryTextFile = LineOfText.Split("#") For i = 0 To...
0
by: Solai Malai | last post by:
Hi Friends, I have a ListBox in my windows Application which is Data Bounded and i want to Filtered the Data bounded List box with the value of the textbox. Help me Thank you in Advance With...
3
by: danon | last post by:
Hi every one can any one tel me how to get some thing from a listbox to a textbox by double click using visual basic 2008 i have already i databasse fealed many thanks
9
by: weirdguy | last post by:
Hello, Just for anyone information, there is a similar title "Search in Listbox" but it is via Combo Box. In case, anyone need it, I put a link to here. Please let me know if I break any rules...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.