By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,224 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,224 IT Pros & Developers. It's quick & easy.

Deleting a selection from a combobox

100+
P: 173
hi,

has any1 got any examples i can look at for the following:




[PHP]A delete button that deletes the item selected in the combobox [/PHP]




i need it urgently???????please

or even any code!!!!
Dec 6 '06 #1
Share this Question
Share on Google+
18 Replies


nico5038
Expert 2.5K+
P: 3,072
Assuming the combobox named "cmbX" has a recordsource like:

select code, description from tblX

You can code a button with:
Expand|Select|Wrap|Line Numbers
  1. '  check selection made
  2. IF len(nz(me.cmbX)) > 0 then
  3.     ' yes, delete
  4.     currentdb.execute ("delete * from tblX where code=" & me.cmbX)
  5.     me.cmbX.requery
  6. endif
  7.  
Getting the idea ?

Nic;o)
Dec 6 '06 #2

100+
P: 173
ive tried your idea Nico....

the combobox is called Combo2
the row source of Combo2 is:

[PHP]SELECT [tab_main].[username] FROM tab_main ORDER BY [username];[/PHP]

the code im using for the delete button is:

[PHP]Private Sub Command11_Click()
On Error GoTo Err_Command11_Click

If Len(Nz(Me.Combo2)) > 0 Then

CurrentDb.Execute ("delete * from tab_main where code=" & Me.Combo2)
Me.Combo2.Requery
End If
Exit_Command11_Click:
Exit Sub

Err_Command11_Click:
MsgBox Err.Description
Resume Exit_Command11_Click

End Sub[/PHP]

but im getting the following error message:

Syntax error (missing operator) in query expression 'code=Adam Beaky'.

Adam Beaky is an example selected from combobox
Dec 8 '06 #3

nico5038
Expert 2.5K+
P: 3,072
Hmm, I assumed the code was numeric, for a string embedding in single quotes is mandatory like:

"SELECT * FROM x WHERE code='" & me.combobox & "'")

Or when you have names wih an embedded quote use:

"SELECT * FROM x WHERE code=" & chr(34) & me.combobox & chr(34))

Nic;o)
Dec 8 '06 #4

100+
P: 173
Ok Nico ive changed it to the following:

[PHP]Private Sub Command11_Click()
On Error GoTo Err_Command11_Click

If Len(Nz(Me.Combo2)) > 0 Then

CurrentDb.Execute ("SELECT * FROM tab_main WHERE code='" & Me.Combo2 & "'")

Me.Combo2.Requery
End If
Exit_Command11_Click:
Exit Sub

Err_Command11_Click:
MsgBox Err.Description
Resume Exit_Command11_Click

End Sub[/PHP]

am i doing sumat wrong?

im getting a error message saying Cannot execute a select query..

is it possible if you could please modify this code for me?
Dec 8 '06 #5

NeoPa
Expert Mod 15k+
P: 31,660
Ok Nico ive changed it to the following:

[PHP]Private Sub Command11_Click()
On Error GoTo Err_Command11_Click

If Len(Nz(Me.Combo2)) > 0 Then

CurrentDb.Execute ("SELECT * FROM tab_main WHERE code='" & Me.Combo2 & "'")

Me.Combo2.Requery
End If
Exit_Command11_Click:
Exit Sub

Err_Command11_Click:
MsgBox Err.Description
Resume Exit_Command11_Click

End Sub[/PHP]

am i doing sumat wrong?

im getting a error message saying Cannot execute a select query..

is it possible if you could please modify this code for me?
Yes Beany ;).
But it's a simple fix.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2. On Error GoTo Err_Command11_Click
  3.  
  4. If Len(Nz(Me.Combo2)) > 0 Then
  5.  
  6.     CurrentDb.Execute ("DELETE * FROM tab_main WHERE code='" & Me.Combo2 & "'")
  7.  
  8.     Me.Combo2.Requery
  9. End If
  10. Exit_Command11_Click:
  11.     Exit Sub
  12.  
  13. Err_Command11_Click:
  14.     MsgBox Err.Description
  15.     Resume Exit_Command11_Click
  16.  
  17. End Sub
(See post #2 for what Nico originally suggested.)
Dec 8 '06 #6

100+
P: 173
Neo ive inserted the code what you modified (select -> delete(thanks))....

but when i select a name from the combobox and click the delete button i get the following message:

[HTML]Too few parameters. Expected 1[/HTML]

why is that?
Dec 9 '06 #7

nico5038
Expert 2.5K+
P: 3,072
This implies that you use an unknown fieldname.
You will have to change the "code" into the fieldname from your table.

Nic;o)
Dec 9 '06 #8

100+
P: 173
Sorry about this, but i dont really understand what your suggesting?


(i bet you must be sick of me):)
Dec 9 '06 #9

MMcCarthy
Expert Mod 10K+
P: 14,534
Sorry about this, but i dont really understand what your suggesting?


(i bet you must be sick of me):)
In your table tab_main do you have a field named code?
Dec 9 '06 #10

100+
P: 173
No Mary, there is no field named 'code'..........

ive got the following fields:

[PHP]userID, number, username,cost_centre,phone_model,imei,sim_no,puk_c ode,date_issued,notes,tariff, roam, call_int[/PHP]
Dec 9 '06 #11

MMcCarthy
Expert Mod 10K+
P: 14,534
No Mary, there is no field named 'code'..........

ive got the following fields:

[PHP]userID, number, username,cost_centre,phone_model,imei,sim_no,puk_c ode,date_issued,notes,tariff, roam, call_int[/PHP]
Which one corresponds to the value in the Combo2 combo box?
Dec 9 '06 #12

100+
P: 173
IT WORKS.........................

i changed code to username................................ and it works...........

can i kiss you Mary? :)
thank you very much............

last thing.... is there a way of adding a warning message to this?

ie are you sure you want to delete this user?
Dec 9 '06 #13

MMcCarthy
Expert Mod 10K+
P: 14,534
Try this ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2. On Error GoTo Err_Command11_Click
  3. Dim rslt As Integer
  4.  
  5.    If Len(Nz(Me.Combo2)) > 0 Then
  6.       rslt=Msgbox("Are you sure you wish to delete " & Me.Combo2 & " from the table?", vbYesNo)
  7.       If rslt=vbYes Then
  8.          CurrentDb.Execute ("DELETE * FROM tab_main WHERE username='" & Me.Combo2 & "'")
  9.          Me.Combo2.Requery
  10.       End If
  11.    End If
  12.  
  13. Exit_Command11_Click:
  14.  
  15.     Exit Sub
  16.  
  17. Err_Command11_Click:
  18.  
  19.     MsgBox Err.Description
  20.     Resume Exit_Command11_Click
  21.  
  22. End Sub 
  23.  
Mary
Dec 10 '06 #14

100+
P: 173
perfecto............

THANK YOU VERY MUCH............... GOD Bless you........

you know ur 5hit!!!!!!
Dec 10 '06 #15

MMcCarthy
Expert Mod 10K+
P: 14,534
perfecto............

THANK YOU VERY MUCH............... GOD Bless you........

you know ur 5hit!!!!!!
You can also use a message box like this around your insert statement in your other question to allow the user to cancel the operation.

Mary
Dec 10 '06 #16

100+
P: 173
excellent..... is it just a simple copy and paste job lol (with the obvious changes?)
Dec 10 '06 #17

NeoPa
Expert Mod 15k+
P: 31,660
excellent..... is it just a simple copy and paste job lol (with the obvious changes?)
That really depends on what's 'obvious'.
The answer should be yes, but try it and see.
Repost if you can't get it to work for yourself. You should have picked up quite a lot about all this by now :).
Dec 10 '06 #18

100+
P: 173
true true lol lol im genious now :) :)
Dec 10 '06 #19

Post your reply

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