Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I havent found any solutions to this problem yet, so I figured I'd post it up and see if anyone has had a similar problem. I'm basically trying to add multiple new items to the same table in our Access SQL Database based on checkbox form data. The user selects a number of categories under which to classify an item (which is also being added in another table). Here's the code I have to retrieve the data and store it in a static size array that is equal to the maximum possible choices (I had errors without putting it into a known-length array):

Dim VidName, VidUrl, VidDescription, VidCategory, VidKeywords, VidSRC, VidType, Hidden, Approve, RunTime, mode_a, i, VidNail, myArray(5) 

VidCategory=Request("Category")

mode_a=split(VidCategory,",")

For i=LBound(mode_a) to UBound(mode_a)

    myArray(i) = mode_a(i)

Next 

Then later in the page, after the video data is inserted into the video table, I need to call the video category table and insert the video id with each corresponding category id(s):

SQL = "SELECT tbl_categories.category_name, tbl_categories.category_id"
SQL = SQL & " FROM (tbl_video_categories INNER JOIN tbl_video ON tbl_video_categories.video_id = tbl_video.video_id) INNER JOIN tbl_categories ON tbl_video_categories.category_id = tbl_categories.category_id"
SQL = SQL & " GROUP BY tbl_categories.category_name, tbl_categories.category_active, tbl_categories.category_id"
SQL = SQL & " HAVING (((tbl_categories.category_active)=1))"
SQL = SQL & " ORDER BY tbl_categories.category_id "


Set rs2 = LoadData(SQL)

If rs2.RecordCount > 0 Then

    rs2.MoveFirst

    Dim j

    j = 0

*' Send is a shorthand function defined elsewhere, basically just Response.Write These sends are for testing purposes only. *

    Do While Not rs2.EOF

        Send myArray(j)

        Send "<br>"

        Send rs2("category_id")

        Send "<br>"

        Send rs2("category_name")

        Send "<br>"
        Send "<br>"

        If rs2("category_name") = myArray(0) Then

            Send "ZERO"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(1) Then

            Send "ONE"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(2) Then

            Send "TWO"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(3) Then

            Send "THREE"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If
        If rs2("category_name") = myArray(4) Then
            Send "FOUR"
            Send " <br/>"
            SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

            SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

            PerformSQL(SQL)


        End If

        rs2.MoveNext

        j = j + 1

    Loop

End If

Set rs2 = Nothing

There are no errors, but No matter how I have tried to change it (using Elseifs, changing the order, stepping through myArray with a counter, etc) It only ever adds the first category in the array to the table, even if there are more than one selected. If you step through the myArray(j) and rs2.categories, they both have data (assuming some checkboxes were clicked), and if all categories are selected and in myArray, they even print out on the same iterations. So the data is there, but the "Send Zero" If statement is the only one that is ever true, and it only executes once for some reason, even though Do While not rs2.EOF executes 5 times (there are currently 5 categories).

Any Help on this would be greatly appreciated!

Edit: rs is another record set defined on the page, and you can assume it has the correct data for the sql insertion (aka the sql code all works)

*Edit 2: I tried ipr101's suggestion. with the first 4/5 checkboxes selected, sending myArray(n) and rs2("category_name"), this was the output:*

Iteration Number: 0

Array: Risk Management

RecordSet: Risk Management

ZERO

Iteration Number: 1

Array: EVMS

RecordSet: EVMS

Iteration Number: 2

Array: PMP Exam Prep

RecordSet: Program Planning

Iteration Number: 3

Array: Lectures

RecordSet: PMP Exam Prep

Iteration Number: 4

Array:

RecordSet: Lectures

As you can see, it only executed the first if, but the recordset and array still had values in future iterations, but did not execute the other if statements

Answer: Ok I figured out how to make this work. Basically I was trying to overcomplicate things. Instead of sending the category name from the form and trying to compare it to the DB values and then trying to add the video_id and category id, I just send the category id (which is unique) as the checkbox value(s), and then execute the sql in the first for loop I had:

For i=LBound(mode_a) to UBound(mode_a)

    SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"

    SQL = SQL & "VALUES (" & rs("video_id") & "," & mode_a(i) & ")"

    PerformSQL(SQL)

Next

Thanks all for your input and suggestions, they were very helpful!

share|improve this question
I'd do a 'Send' of the rs2("category_name") and myArray(n) values before each failing if statement. Then at least you'll be able to see why the code isn't dropping into the if as expected. I vaguely remember an asp bug that caused the value of a recordset field to be lost after it was accessed for the first time. – ipr101 Jul 12 '11 at 14:37
I tried your suggestion ipr101. I posted the result above – David Dwyer Jul 12 '11 at 15:53

1 Answer

Did you try this?

If rs2("category_name") = myArray(1) OR rs2("category_name") = myArray(2) OR rs2("category_name") = myArray(3) OR rs2("category_name") = myArray(4) Then
    SQL = "INSERT INTO tbl_video_categories (video_id, category_id)"
    SQL = SQL & " VALUES (" & rs("video_id") & "," & rs2("category_id") & ")"

    PerformSQL(SQL)
End If
share|improve this answer
I just tried your code stealthyninja, and the outcome was the same. it will only execute when rs2("category_name") = myArray(0), and will only add one line to the database, to whichever category name is in the myArray(0) slot. In fact, if you omit the myArray(0) check, as I think you had but just as a typo, it wont add anything to the database at all, even if the rs2("category_name") clearly does equal a value in myArray(). It's very strange... – David Dwyer Jul 12 '11 at 16:17
There might be spaces / tab characters in your data. Try with If Trim(rs2("category_name")) = Trim(myArray(1)) Then as well. – stealthyninja Jul 12 '11 at 16:23
apparently the main issue was trying to compare the category name when I should have just been passing and using the category id. I posted the solution i found in the OP since I dont have enough rep to answer my own question for another 4 hours...Thanks for the help ninja! – David Dwyer Jul 12 '11 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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