CheckBoxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tsubasa
    New Member
    • Aug 2008
    • 64

    #1

    CheckBoxes

    I am trying to add a checkbox to my dynamic table of records based on a query search. After I populate the table I want to be able to select records via a checkbox then write the associated information of the records to another table called labels. Can you help me on this, my code is below. Thank You!
    -gabe
    Code:
    <%
    ' Declare our variables... always good practice!
    Dim strURL     ' The URL of this page so the form will work
                   ' no matter what this file is named.
    
    Dim cnnSearch  ' ADO connection
    Dim rstSearch  ' ADO recordset
    Dim strDBPath  ' path to our Access database (*.mdb) file
    
    Dim strSQL     ' The SQL Query we build on the fly
    Dim strSearch  ' The text being looked for
    
    ' Retreive the URL of this page from Server Variables
    strURL = Request.ServerVariables("URL")
    
    ' Retreive the term being searched for.  I'm doing it on
    ' the QS since that allows people to bookmark results.
    ' You could just as easily have used the form collection.
    strSearch = Request.QueryString("search")
    'strSearch = Replace(strSearch, "'", "''")
    
    ' Since I'm doing this all in one page I need to see if anyone
    ' has searched for something.  If they have we hit the DB.
    ' O/W I just show the search form and quit.
    
    %>
    <p>&nbsp;</p>
    <form action="<%= strURL %>" method="get">
    <input name="search" value="<%= strSearch %>" size="20" />
    <input type="submit" />
    </form>
    
    <%
    If strSearch <> "" Then
    	' MapPath of virtual database file path to a physical path.
    	' If you want you could hard code a physical path here.
    	strDBPath = Server.MapPath("database.mdb")
    
    
    	' Create an ADO Connection to connect to the sample database.
    	' We're using OLE DB but you could just as easily use ODBC or a DSN.
    	Set cnnSearch = Server.CreateObject("ADODB.Connection")
    
    	' This line is for the Access sample database:
    	cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
    
    	' We're actually using SQL Server so we use this line instead:
    	'cnnSearch.Open "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
    	'	& "Initial Catalog=samples;User Id=samples;Password=password;" _
    	'	& "Connect Timeout=15;Network Library=dbmssocn;"
    
    	' Build our query based on the input.
    	strSQL = "SELECT bin ,nomen, oha, sell, nsn " _
    		& "FROM [inventory] " _
    		& "WHERE nsn LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
    		& "OR oha LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
    		& "OR nomen LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
    		& "OR bin LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
    		& "ORDER BY nsn;"
    
    	' Execute our query using the connection object.  It automatically
    	' creates and returns a recordset which we store in our variable.
    	Set rstSearch = cnnSearch.Execute(strSQL)
    
    	' Display a table of the data in the recordset.  We loop through the
    	' recordset displaying the fields from the table and using MoveNext
    	' to increment to the next record.  We stop when we reach EOF.
    	' For fun I'm combining some fields and showwing you can do more then
    	' just spit out the data in the form it is in in the table.
    	%>
    	<table border="1">
    	<tr>
    	<th>Location</th>
    	<th>Nomenclature</th>
    	<th>NSN</th>
    	<th>U/P</th>
    	<th>OH</th>
    	</tr>
    	<%
    	Do While Not rstSearch.EOF
    		%>
    		<tr>
    		<td><%= rstSearch.Fields("bin").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("nomen").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("nsn").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("sell").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("oha").Value %>&nbsp;</td>
    		</tr>
    		<%
    
    		rstSearch.MoveNext
    	Loop
    	%>
    	</table>
    	<%
    	' Close our recordset and connection and dispose of the objects
    	rstSearch.Close
    	Set rstSearch = Nothing
    	cnnSearch.Close
    	Set cnnSearch = Nothing
    End If
    
    %>
    Last edited by jhardman; Aug 22 '08, 07:56 PM. Reason: put code in code tags. please note button marked #
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Hey Gabe, welcome to bytes.com. I hope we can be of help.

    First of all, if you want a checkbox on each row of the table you pull up from the db, you will need the table to be within a web form (a form is just an object that collects user inputs. you can put checkboxes anywhere, but if they are not within a form, their values will not be passed on) and you will probably want a "submit" button on the end like this:
    Code:
    <form action="pageThatWillHandleTheForm.asp" method="post">
    	<table border="1">
    		<!-- a whole bunch of lines I'm leaving out for now -->
    	</table>
    	<input type="submit" value="Click Here To Submit Changes">
    	</form>
    	<%
    With me so far?

    If you want to add a checkbox to each row, you will need to have some row identifier. for example if each row of the database has a unique field called "ID" you could say this:
    Code:
    		<tr>
    		<td><%= rstSearch.Fields("bin").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("nomen").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("nsn").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("sell").Value %>&nbsp;</td>
    		<td><%= rstSearch.Fields("oha").Value %>&nbsp;</td>
    		<td><input type="checkbox" name="<%= rstSearch.Fields("ID") %>"></td>
    		</tr>
    Now any field in the db that has a unique value on each row can be used here, but I like to use a field with an arbitrary number that no one will ever need to change.

    If you want the user to specify changes he will make (he needs to do more than just select a row) than you may want to put the fields you have within an input:
    Code:
    		<td><input type="text" name="nomen_<%= rstSearch.Fields("ID") %>" value="<%= rstSearch.Fields("nomen") %>"></td>
    Notice that what was the entire content of the table cell is now in the value="..." of the input - this is what shows up in the text box. Also notice that the name of the input is the name of the field PLUS the unique row identifier. This will be important later.

    The rest of what you do is on the page that is the form handler. On the form handler put this code (just to test that the data arrives - this can be deleted or commented out later)
    Code:
    for each x in request.form
    	response.write x & ": " & request.form(x) & "<br>" & vbNewLine
    next
    Once you have this in place, you are ~ half done. Let me know when you get here and I will tell you what to do next.

    Jared

    Comment

    • tsubasa
      New Member
      • Aug 2008
      • 64

      #3
      Thanks for Replying. Let me work on what you have suggested. It will take some time because I am still new to programming and particularly with ASP. Once I have accomplished your recommendations , I will get back to you. Again, Big Thanks from Japan!

      -Gabe

      Comment

      • tsubasa
        New Member
        • Aug 2008
        • 64

        #4
        I have made all the changes as recommended. Below is the modified code.
        Code:
        <%
        ' Declare our variables... always good practice!
        Dim strURL     ' The URL of this page so the form will work
                       ' no matter what this file is named.
         
        Dim cnnSearch  ' ADO connection
        Dim rstSearch  ' ADO recordset
        Dim strDBPath  ' path to our Access database (*.mdb) file
         
        Dim strSQL     ' The SQL Query we build on the fly
        Dim strSearch  ' The text being looked for
         
        ' Retreive the URL of this page from Server Variables
        strURL = Request.ServerVariables("URL")
         
        ' Retreive the term being searched for.  I'm doing it on
        ' the QS since that allows people to bookmark results.
        ' You could just as easily have used the form collection.
        strSearch = Request.QueryString("search")
        'strSearch = Replace(strSearch, "'", "''")
         
        ' Since I'm doing this all in one page I need to see if anyone
        ' has searched for something.  If they have we hit the DB.
        ' O/W I just show the search form and quit.
         
        %>
        <p>&nbsp;</p>
        <form action="<%= strURL %>" method="get">
        <input name="search" value="<%= strSearch %>" size="20" />
        <input type="submit" />
        </form>
         
        <%
        If strSearch <> "" Then
            ' MapPath of virtual database file path to a physical path.
            ' If you want you could hard code a physical path here.
            strDBPath = Server.MapPath("database.mdb")
         
         
            ' Create an ADO Connection to connect to the sample database.
            ' We're using OLE DB but you could just as easily use ODBC or a DSN.
            Set cnnSearch = Server.CreateObject("ADODB.Connection")
         
            ' This line is for the Access sample database:
            cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
         
            ' We're actually using SQL Server so we use this line instead:
            'cnnSearch.Open "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
            '   & "Initial Catalog=samples;User Id=samples;Password=password;" _
            '   & "Connect Timeout=15;Network Library=dbmssocn;"
         
            ' Build our query based on the input.
            strSQL = "SELECT nsn, bin ,nomen, oha, sell, id " _
                & "FROM [inventory] " _
                & "WHERE nsn LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
                & "OR bin LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
                & "OR oha LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
                & "OR sell LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
                & "ORDER BY nsn;"
         
            ' Execute our query using the connection object.  It automatically
            ' creates and returns a recordset which we store in our variable.
            Set rstSearch = cnnSearch.Execute(strSQL)
         
            ' Display a table of the data in the recordset.  We loop through the
            ' recordset displaying the fields from the table and using MoveNext
            ' to increment to the next record.  We stop when we reach EOF.
            ' For fun I'm combining some fields and showwing you can do more then
            ' just spit out the data in the form it is in in the table.
            %>
            <form action="selectedRecords.asp" method="post">
            </form>
            <table border="1">
            <tr>
            <th>Location</th>
            <th>Nomenclature</th>
            <th>NSN</th>
            <th>U/P</th>
            <th>OH</th>
            <th>Pick</th>
            </tr>
            <%
            Do While Not rstSearch.EOF
                %>
                <tr>
                <td><%= rstSearch.Fields("bin").Value %>&nbsp;</td>
                <td><%= rstSearch.Fields("nomen").Value %>&nbsp;</td>
                <td><%= rstSearch.Fields("nsn").Value %>&nbsp;</td>
                <td><%= rstSearch.Fields("sell").Value %>&nbsp;</td>
                <td><%= rstSearch.Fields("oha").Value %>&nbsp;</td>
                <td><input type="checkbox" value="ON" name="<%= rstSearch.Fields("id") %>"></td>
                </tr>
                <%
         
                rstSearch.MoveNext
            Loop
            %>
            </table>
            <br>
            <input type ="submit" value=Submit">
            </form>
            <%
            ' Close our recordset and connection and dispose of the objects
            rstSearch.Close
            Set rstSearch = Nothing
            cnnSearch.Close
            Set cnnSearch = Nothing
        End If
         
        %>

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          Gabe,

          3 things before we go on.

          1- Please enclose your code within code tags. If you select your code with your mouse and click the control marked # that will put in the necessary tags.

          2- I noticed a couple typos in your code that will prevent it from working. Notice Line 71-72 there is a <form> tag and a </form> tag. This second was probably put in by an over-zealous code editor. It needs to be removed. The correct place for the </form> tag is line 101, not line 72.
          And also, watch your quote marks - Line 100 is written incorrectly:
          <input type ="submit" value="Submit">

          3- Test this and tell me what the output is. If there are errors we need to fix them now before we go on.

          Jared

          Comment

          • tsubasa
            New Member
            • Aug 2008
            • 64

            #6
            Jared,
            I have made all of the corrections and I also go the form handler to work. I selected a few records with the checkboxes and I got the following results.

            2015:ON
            2016:ON
            2030:ON

            -Gabe

            Comment

            • jhardman
              Recognized Expert Specialist
              • Jan 2007
              • 3405

              #7
              Originally posted by tsubasa
              Jared,
              I have made all of the corrections and I also go the form handler to work. I selected a few records with the checkboxes and I got the following results.

              2015:ON
              2016:ON
              2030:ON

              -Gabe
              Excellent, this is exactly want you want to see. OK next you want to open the db to see just the records you want to alter:
              Code:
              dim recordsToQuery
              
              for each x in request.form
                 if request(x) = "ON" then
                    recordsToQuery = recordsToQuery & x & ","
                 end if
              next
              'recordsToQuery should now be a list of numbers the user selected
              'i.e. "1234,2345,3456,"
              
              'remove trailing comma
              recordsToQuery = left(recordsToQuery, len(recordsToQuery)-1)
              
              query = "SELECT * FROM [inventory] WHERE id IN (" & recordsToQuery & ")"
              'then check to make sure this works.  Open the db connection just like 
              'previous and list the records just to make sure you pulled up the right records
              Try this, let me know if you are able to pull up just the records the user selected. At this point, we are almost done.

              Jared

              Comment

              • tsubasa
                New Member
                • Aug 2008
                • 64

                #8
                On this particular piece of code, I take it you want me to place it in the Form handler?

                -Gabe

                Originally posted by jhardman
                Excellent, this is exactly want you want to see. OK next you want to open the db to see just the records you want to alter:
                Code:
                dim recordsToQuery
                
                for each x in request.form
                   if request(x) = "ON" then
                      recordsToQuery = recordsToQuery & x & ","
                   end if
                next
                'recordsToQuery should now be a list of numbers the user selected
                'i.e. "1234,2345,3456,"
                
                'remove trailing comma
                recordsToQuery = left(recordsToQuery, len(recordsToQuery)-1)
                
                query = "SELECT * FROM [inventory] WHERE id IN (" & recordsToQuery & ")"
                'then check to make sure this works.  Open the db connection just like 
                'previous and list the records just to make sure you pulled up the right records
                Try this, let me know if you are able to pull up just the records the user selected. At this point, we are almost done.

                Jared

                Comment

                • jhardman
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3405

                  #9
                  Originally posted by tsubasa
                  On this particular piece of code, I take it you want me to place it in the Form handler?

                  -Gabe
                  yes, immediately after the code that listed your inputs.

                  Jared

                  Comment

                  • tsubasa
                    New Member
                    • Aug 2008
                    • 64

                    #10
                    I am trying to get this to work, but I am having a little trouble in trying to open the database. Give me a little time and hopefully I can get this to work.

                    -Gabe

                    Comment

                    • tsubasa
                      New Member
                      • Aug 2008
                      • 64

                      #11
                      On opening the database what should be looking for, or should I be pulling up full records?

                      -Gabe

                      Comment

                      • tsubasa
                        New Member
                        • Aug 2008
                        • 64

                        #12
                        Jared,
                        Here is what I have so far. I applied the code and below is what I got.

                        85: ON
                        634: ON
                        85,634

                        Here is what I did in the form handler portion:
                        -Gabe

                        Code:
                        <%
                        for each x in request.form
                            response.write x & ": " & request.form(x) & "<br>" & vbNewLine
                        next 
                        
                        dim recordsToQuery
                        Dim strDBPath  ' path to our Access database (*.mdb) file
                        strDBPath = Server.MapPath("database.mdb")
                        
                        for each x in request.form
                           if request(x) = "ON" then
                              recordsToQuery = recordsToQuery & x & ","
                           end if
                        next
                        'recordsToQuery should now be a list of numbers the user selected
                        'i.e. "1234,2345,3456,"
                         
                        'remove trailing comma
                        recordsToQuery = left(recordsToQuery, len(recordsToQuery)-1)
                         
                        query = "SELECT * FROM [inventory] WHERE id IN (" & recordsToQuery & ")"
                        'then check to make sure this works.  Open the db connection just like 
                        'previous and list the records just to make sure you pulled up the right records
                        
                        Set cnnSearch = Server.CreateObject("ADODB.Connection")
                        cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
                        
                        response.write recordsToQuery
                        
                        %>

                        Comment

                        • jhardman
                          Recognized Expert Specialist
                          • Jan 2007
                          • 3405

                          #13
                          Originally posted by tsubasa
                          Jared,
                          Here is what I have so far. I applied the code and below is what I got.

                          85: ON
                          634: ON
                          85,634

                          Here is what I did in the form handler portion:
                          -Gabe

                          Code:
                          <%
                          for each x in request.form
                              response.write x & ": " & request.form(x) & "<br>" & vbNewLine
                          next 
                          
                          dim recordsToQuery
                          Dim strDBPath  ' path to our Access database (*.mdb) file
                          strDBPath = Server.MapPath("database.mdb")
                          
                          for each x in request.form
                             if request(x) = "ON" then
                                recordsToQuery = recordsToQuery & x & ","
                             end if
                          next
                          'recordsToQuery should now be a list of numbers the user selected
                          'i.e. "1234,2345,3456,"
                           
                          'remove trailing comma
                          recordsToQuery = left(recordsToQuery, len(recordsToQuery)-1)
                           
                          query = "SELECT * FROM [inventory] WHERE id IN (" & recordsToQuery & ")"
                          'then check to make sure this works.  Open the db connection just like 
                          'previous and list the records just to make sure you pulled up the right records
                          
                          Set cnnSearch = Server.CreateObject("ADODB.Connection")
                          cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
                          
                          response.write recordsToQuery
                          
                          %>
                          It looks good so far, now check to see how the query looks by replacing the last response.write line with
                          Code:
                          response.write query
                          Does that look OK? Now use that query to populate a recordset to make sure you are pulling up the right data:
                          Code:
                          Set rstSearch = cnnSearch.Execute(query)
                          'followed by the same code you used before to list the data from the db
                          Are you pulling up the right records?

                          Jared

                          Comment

                          • tsubasa
                            New Member
                            • Aug 2008
                            • 64

                            #14
                            Good morning Jared,
                            Yes, after entering your code the form handler does display the data just as it would look in the default page after doing a search. The records that are selected via the checkbox appear and I have place the table so they look in a oderly fashion. I also remark out the test portion at the top of the page. Below is the code:

                            -Gabe

                            Code:
                            <%
                            'for each x in request.form
                            '    response.write x & ": " & request.form(x) & "<br>" & vbNewLine
                            'next 
                             
                            dim recordsToQuery
                            Dim strDBPath  ' path to our Access database (*.mdb) file
                            strDBPath = Server.MapPath("database.mdb")
                             
                            for each x in request.form
                               if request(x) = "ON" then
                                  recordsToQuery = recordsToQuery & x & ","
                               end if
                            next
                            'recordsToQuery should now be a list of numbers the user selected
                            'i.e. "1234,2345,3456,"
                             
                            'remove trailing comma
                            recordsToQuery = left(recordsToQuery, len(recordsToQuery)-1)
                             
                            query = "SELECT * FROM [inventory] WHERE id IN (" & recordsToQuery & ")"
                            'then check to make sure this works.  Open the db connection just like 
                            'previous and list the records just to make sure you pulled up the right records
                             
                            Set cnnSearch = Server.CreateObject("ADODB.Connection")
                            cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
                            
                            'response.write query
                            
                            Set rstSearch = cnnSearch.Execute(query)
                            'followed by the same code you used before to list the data from the db
                             %>
                             <table border="1">
                                <tr>
                                <th>id</th>
                                <th>Location</th>
                                <th>Nomenclature</th>
                                <th>NSN</th>
                                <th>U/P</th>
                                <th>OH</th>
                                </tr>
                                <%
                                Do While Not rstSearch.EOF
                                    %>
                                    <tr>
                                    <td><%= rstSearch.Fields("id").Value %>&nbsp;</td>
                                    <td><%= rstSearch.Fields("bin").Value %>&nbsp;</td>
                                    <td><%= rstSearch.Fields("nomen").Value %>&nbsp;</td>
                                    <td><%= rstSearch.Fields("nsn").Value %>&nbsp;</td>
                                    <td><%= rstSearch.Fields("sell").Value %>&nbsp;</td>
                                    <td><%= rstSearch.Fields("oha").Value %>&nbsp;</td>
                                    </tr>
                                    <%
                            
                                    rstSearch.MoveNext
                                Loop
                                %>
                                </table>
                            <%
                             rstSearch.Close
                                Set rstSearch = Nothing
                                cnnSearch.Close
                                Set cnnSearch = Nothing
                            
                            %>

                            Comment

                            • jhardman
                              Recognized Expert Specialist
                              • Jan 2007
                              • 3405

                              #15
                              Originally posted by tsubasa
                              Good morning Jared,
                              Yes, after entering your code the form handler does display the data just as it would look in the default page after doing a search. The records that are selected via the checkbox appear and I have place the table so they look in a oderly fashion. I also remark out the test portion at the top of the page. Below is the code:

                              -Gabe
                              Good. Now the next question is, how do you want to modify the records. Do you want to edit the text? Do you want to delete those records? Do you have a set transformation you want to perform on them?

                              Jared

                              Comment

                              Working...