Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 1st, 2005, 03:51 PM
delislea delislea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 delislea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 5 sec
Reputation Power: 0
Removing an item from a listbox

Hello to every Access brain around. I've been trying to do something really simple but I have not been able to find the solution. I have two listbox on a form about trainings the first gives employee's name to choose and the second displays them (tblNamesChosen) with a code On Form Current linking both table (form and 2nd listbox) by the trainingID. The problem is : I would like to be able to delete one name from the txtNamesChosen listbox with a button like you see all the time "< "....so here's the code I've been trying:

Private Sub Command48_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim VarItem As Variant
Dim strSQL As String
Dim intID As Integer
intID = 0 & TrainingID
Set db = CurrentDb()
strSQL = "SELECT tblNamesChosen.* FROM tblNamesChosen;"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

For Each VarItem In Me!txtNamesChosen.ItemsSelected()
rs.MoveFirst
If rs!Names.Value = Me!txtNamesChosen.Column(0, VarItem) Then
With rs
rs.Edit
rs.Delete
rs.MoveNext
End With
End If
Next VarItem

Set db = Nothing
Set rs = Nothing
txtNamesChosen.Requery
Me.Refresh
End Sub

It does delete a name, but whatever I try it's always the first one or the last one never the one selected (if not in that position). Any help would be much much appreciated...I look all over the web to seek a solution. This is my last hope. Thanks!!!

Reply With Quote
  #2  
Old March 2nd, 2005, 09:54 AM
BBJo BBJo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 21 BBJo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 10 m 15 sec
Reputation Power: 0
I came across this article on msdn and thought this might help you in some way (maybe not at all)

http://msdn.microsoft.com/archive/d...osoftAccess.asp

and find the article Creating Combo Boxes and List Boxes in Microsoft Access on the left site.

Maybe you can work out from that example how to delete from the box.

Sorry I can't help more than this

Reply With Quote
  #3  
Old March 3rd, 2005, 03:17 AM
BBJo BBJo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 21 BBJo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 10 m 15 sec
Reputation Power: 0
I came across this article when searching for something else. This seems to be what you are looking for. Sorry for wasting your time if this is irrelevant.

http://www.tek-tips.com/faqs.cfm?fid=4246

Reply With Quote
  #4  
Old March 3rd, 2005, 11:51 AM
delislea delislea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 delislea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 5 sec
Reputation Power: 0
Hello BBjo, I really appreciate all the trouble you're going throught for me. The problem is that these list box are alone in a form and they just moving the items from one listbox to another, my listbox is link to a table so I have to go in the table, make sure that the item is the one link to the right trainingID of the form and then delete it. But be sure that I looked to everyting you send me and try to ignite a spark in my brain to solve this. Right now I'm still at . Thanks!!

Reply With Quote
  #5  
Old March 4th, 2005, 03:04 PM
delislea delislea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 delislea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 5 sec
Reputation Power: 0
Almost there

I'm almost there :

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim VarItem As Variant
Dim strSQL As String
Dim strNames As String
Dim intID As Integer
Set db = CurrentDb()
intID = TrainingID
Me.txtNamesChosen.SetFocus
strNames = Me.txtNamesChosen.ItemData(Me.txtNamesChosen.ListI ndex)
strSQL = "SELECT tblNamesChosen.* FROM tblNamesChosen WHERE tblNamesChosen.Names = " & strNames & " AND tblNamesChosen.TrainingID = " & intID & ";"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
' Delete record
Do
rs.Delete
rs.MoveNext
Loop Until rs.EOF

Set db = Nothing
Set rs = Nothing
Me.Refresh

but I get a Syntax error (missing operator) in query expression and the line in red is highlighted...Any ideas
If I only put the WHERE tblNamesChosen.TrainingID = " & intID & ";" it's working great...it deletes all the record with the same training ID, but I really want only the name selected to be deleted. Thanks!!

Reply With Quote
  #6  
Old March 4th, 2005, 03:55 PM
delislea delislea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 delislea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 5 sec
Reputation Power: 0
Got it!!!!!

The ' has to surround text variable'

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim VarItem As Variant
Dim strSQL As String
Dim strNames As String
Dim intID As Integer
Set db = CurrentDb()
intID = TrainingID
Me.txtNamesChosen.SetFocus
strNames = Me.txtNamesChosen.ItemData(Me.txtNamesChosen.ListI ndex)
strSQL = "SELECT * FROM tblNamesChosen WHERE tblNamesChosen.Names= '" & strNames & "' AND tblNamesChosen.TrainingID = " & intID & ";"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
' Delete record
Do
rs.Delete
rs.MoveNext
Loop Until rs.EOF

Set db = Nothing
Set rs = Nothing
Me.Refresh

ALLELUIA!!!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Removing an item from a listbox


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway