3

I am trying to write some code that uses SQL to delete rows from several tables.

A user would type type numbers into a textbox that are separated by a comma which is used in the WHERE clause of a SQL DELETE statement.

I have managed to split the string into a variant array and now I want to insert it into my SQL statement.

How do I insert the variable into the SQL statement and have it run through every element of the array?

EDIT: A bit more digging has taught me about For Each Next statements. This is probably what im looking for.

4 Answers 4

7

I suggest you build your query in VBA, then your list of numbers can be an IN statement:

 sSQL = "DELETE FROM table WHERE ID In (" & MyList & ")"

Where MyList = "1,2,3,4" or such like.

As you can see, you do not need an array and a textbox would be more suitable than a combobox. If you wish to allow your users to select by say, a name, then a listbox is useful. You can iterate through the selected items in the listbox and build a string from IDs to be used in the Delete statement. ( MS Access 2007 - Cycling through values in a list box to grab id's for a SQL statement )

You can then execute the sql against an instance of a database. For example:

 Dim db As Database

 Set db = CurrentDB
 db.Execute sSQL, dbFailOnError

 MsgBox "You deleted " & db.RecordsAffected & " records."
4
  • Sorry I got my terminology mixed up and used comboxbox when I did actually mean textbox.I did not know about the where in statement and i will use that in the future. I have to do other things to these numbers which I need to use an array for so your answer isnt suitable in this case. I have managed to solve the problem myself. Ill mark as accept though because it is a very good answer and taught me something new.
    – VBwhatnow
    Commented Jul 23, 2012 at 11:38
  • It is very easy indeed to create an array from a list in VBA. List = "1,2,3"; Ary = Split(List,","), but I doubt that you can use an array in an sql statement.
    – Fionnuala
    Commented Jul 23, 2012 at 11:45
  • I used a for each next statement to do it.
    – VBwhatnow
    Commented Jul 23, 2012 at 11:56
  • That is the long way round. :)
    – Fionnuala
    Commented Jul 23, 2012 at 11:56
0

A generic approach

WHERE 
','+Array+',' like '%,'+col+',%'

It will consider all the numbers available in your Array

0

You could make it simple and elaborate a string, something like

stringBuilder sb = StringBuilder("DELETE FROM YOURTABLE WHERE ");
foreach(string st in stringArray){
    sb.append("YOURFIELD='" + st + "'");
    //If it is not the last element, add an "OR"
    if (st != stringArray[stringArray.length -1]) sb.append(" OR ");
}

//Now, you have a string like
//DELETE FROM YOURTABLE WHERE YOURFIELD='hello' OR YOURFIELD='YES'

//... do something with the command
0
0

This method will fail if you want to run SQL query on two (or multiple) columns using array values from two different arrays. .e.g

where col1=array1(i) and col2=array2(i)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.