Deleting records in Access table based on multiple items selected in Excel user form List box ?

S2Kool

New Member
Joined
Mar 17, 2016
Messages
7
I am trying to use list box (with multiple items) in excel user form to delete the record in access table.

for example:

I have selected multiple items in excel user form list box (Listbox1), let say Wk1 and Wk2

in access, I have a table called "Rawdata" and there is a column called "Weeks".

how to delete all the records in "Rawdata" table on selecting the criteria in excel list box as WK1 and WK2 ?

appreciate your help.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
S2Kool,


Try something like this:

Code:
[COLOR=#0000ff]Private Sub[/COLOR] CommandButton1_Click()
    
 [COLOR=#0000ff]   Dim[/COLOR] oConn      [COLOR=#0000ff]  As Object[/COLOR]
 [COLOR=#0000ff]   Dim [/COLOR]oRs          [COLOR=#0000ff]As Object[/COLOR]
   [COLOR=#0000ff] Dim[/COLOR] sConn       [COLOR=#0000ff] As String[/COLOR]
  [COLOR=#0000ff]  Dim[/COLOR] strSQL         [COLOR=#0000ff]As String[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] StrDBPath    [COLOR=#0000ff]As String[/COLOR]
  [COLOR=#0000ff]  Dim[/COLOR] myCriteria   [COLOR=#0000ff]As String[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] intLp       [COLOR=#0000ff] As Integer[/COLOR]


[COLOR=#008000]    'Get ListBox Values[/COLOR]
  [COLOR=#0000ff]  For [/COLOR]intLp = 0 [COLOR=#0000ff]To [/COLOR]ListBox1.ListCount - 1
     [COLOR=#0000ff]   If[/COLOR] Me.ListBox1.Selected(intLp) = [COLOR=#0000ff]True Then[/COLOR]
            myCriteria = myCriteria & "'" & Me.ListBox1.List(intLp) & "'" & ", "
[COLOR=#0000ff]        End If[/COLOR]
 [COLOR=#0000ff]   Next[/COLOR] intLp
[COLOR=#008000]    'Take off extra 2 characters----> ", "[/COLOR]
    myCriteria = Mid(myCriteria, 1, Len(myCriteria) - 2)
[COLOR=#008000]    'Define DB Path[/COLOR]
    StrDBPath = "C:\Users\Matt\Documents\Database1.accdb"
[COLOR=#008000]    'DB Connection String[/COLOR]
    sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                 "Data Source=" & StrDBPath & ";" & _
                                 "Jet OLEDB:Engine Type=5;" & _
                                 "Persist Security Info=False;"
                                 
  [COLOR=#0000ff]  Set[/COLOR] oConn = CreateObject("ADODB.Connection")
  [COLOR=#0000ff]  Set [/COLOR]oRs = CreateObject("ADODB.Recordset")
    
[COLOR=#008000]    'Open Connection[/COLOR]
    oConn.Open sConn
    
[COLOR=#008000]    'Define SQL String[/COLOR]
    strSQL = "DELETE * FROM myTable WHERE myTable.Weeks IN (" & myCriteria & ");"
    
[COLOR=#008000]    'Execute Query[/COLOR]
    oConn.Execute strSQL
    
[COLOR=#008000]    'Close Connection[/COLOR]
    oConn.Close
     
[COLOR=#008000]    'Destroy Objects[/COLOR]
  [COLOR=#0000ff]  Set[/COLOR] oConn = [COLOR=#0000ff]Nothing[/COLOR]
   [COLOR=#0000ff] Set[/COLOR] oRs = [COLOR=#0000ff]Nothing[/COLOR]
[COLOR=#0000ff]
[/COLOR]
[COLOR=#0000ff]End Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top