+ Reply to Thread
Results 1 to 8 of 8

Thread: getting values from tables

  1. #1
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    getting values from tables

    Hi,

    I am giving discount codes to special customers. So I will be giving a specified codes from the table. The number of the codes will be different each time.

    someone will get 2 codes someone will get 45 or someone will get 75.

    so my query will be like this
    strnum=trim(request.form("noofcodes")

    Select top strnum * from codes

    how do i save them in different variables.

    Can i create the variables on fly and store the codes in them

    can someone tell me how I make this happen

    todd

  2. #2
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    So what I want to do is get the 50 or 75 codes from one table and add it to another table with the USERID

    I have the userid stored into a variable.

  3. #3
    Drunk Barn Owl dr_rock will become famous soon enough dr_rock's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    180
    Rep Power
    4

    Use a count statement to fint the number of rows then use a select statement to pull the codes,
    Code:
    strSQL = "select count(ID) from wherever where something = true;select code from wherever where something = true"
    
    Create an array with userID and Code fields, Ubound rowcount value then run through your recordset of codes and mix with the USERID

    Code:
    Redim arrList(1,rst(0))
    
    Set rst = rst.NextRecordset
    For intCount = 0 to rst.EOF
       arrList(0,intCount) = UserID
       arrList(1,intCount) = rst("code")
      rst.MoveNext
    Loop
    
    Then you can stick it in a table or do whatever you like in the array.

    Does that kind of sound like what you are looking for?

  4. #4
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    I tried it like this

    Code:
    sqltext = "select top 3 * from Codes"
    Set rs = Conn.Execute(sqltext)
    
    strnewadd ="select * from Add_Codes;"
    Set rsnewAdd = Server.CreateObject("ADODB.Recordset")
    With rsnewAdd
    .Source="Add_Codes"
    .CursorType = 2
    .LockType = 3
    End With
    rsnewAdd.Open strnewadd, Conn
    
    For I = 0 to rs.Fields.Count - 1
        redim preserve ARR(I)
      	rsnewAdd.AddNew
    	rsnewAdd("Reg_Id") = sID
    	rsnewAdd("Free_Codes") = rs.Fields(I).Name
    	
    	rsnewAdd.Update
    
    Next
    

    I tried for 3 codes it didnt add the codes in the free_Codes field

    what am i doing wrong

  5. #5
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    Ok this code only adds one code and not 3

    Code:
    sqltext = "select top 3 DiscountCode from Codes"
    Set rs = Conn.Execute(sqltext)
    
    strnewadd ="select * from Add_Codes;"
    Set rsnewAdd = Server.CreateObject("ADODB.Recordset")
    With rsnewAdd
    .Source="Add_Codes"
    .CursorType = 2
    .LockType = 3
    End With
    rsnewAdd.Open strnewadd, Conn
    
    For I = 0 to rs.Fields.Count - 1
       	rsnewAdd.AddNew
    	rsnewAdd("Reg_Id") = sID
    	rsnewAdd("Free_Codes") = rs.Fields(I)
    	rsnewAdd.Update
    
    Next
    
    can someone tell me what i am doing wrong in the loop

  6. #6
    Drunk Barn Owl dr_rock will become famous soon enough dr_rock's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    180
    Rep Power
    4

    How about this?

    Code:
    sqltext = "select top 3 DiscountCode from Codes"
    Set rs = Conn.Execute(sqltext)
    sqltextoutput = ""
    
    For I = 0 to rs.Fields.Count - 1
    
      sqltextoutput = sqltextoutput & "Insert Into Add_Codes (Reg_Id,Free_Codes) values ("&sID&","&rs.Fields(I)&");"
    
    Next
    
    Response.write(sqltextoutput)
    If len(sqltextoutput) > 0 Then Conn.Execute(sqltextoutput)
    
    Last edited by dr_rock; February 2nd, 2009 at 02:16 AM. Reason: table & field names added

  7. #7
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    Ok i used this code and it worked

    Code:
    Set oCodesRs = oConn.Execute("SELECT TOP " & strnum & "* FROM codes;")
    
    If Not oCodesRs.EOF Then
     Do While Not oCodesRs.EOF 
      sCode = oCodesRs("code")
    
      oConn.Execute("INSERT INTO othertable (UserID,Code) VALUES(" & iUserID & ",'" & sCode & "');")
    
     oCodesRs.MoveNext
     Loop
    End If
    
    how do i delete the codes that i inserted from the codes table

    because i dont want to give the same codes to a different person

  8. #8
    Drunk Barn Owl dr_rock will become famous soon enough dr_rock's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    180
    Rep Power
    4

    Code:
    sqltext = "select top 3 DiscountCode from Codes"
    Set rs = Conn.Execute(sqltext)
    sqltextoutput = ""
    
    For I = 0 to rs.Fields.Count - 1
    
      sqltextoutput = sqltextoutput & "Insert Into Add_Codes (Reg_Id,Free_Codes) values ("&sID&","&rs.Fields(I)&");Delete From DiscountCode Where ID = " & rs.Fields("ID") &";"
    
    Next
    
    Response.write(sqltextoutput)
    If len(sqltextoutput) > 0 Then Conn.Execute(sqltextoutput)
    

+ Reply to Thread

Similar Threads

  1. Spliced tables, paged results
    By dr_rock in forum SQL Code Samples
    Replies: 0
    Last Post: September 16th, 2008, 03:19 AM
  2. Pulling from multiple tables...
    By bryceowen in forum SQL Development
    Replies: 2
    Last Post: September 15th, 2008, 08:57 PM
  3. Form for multiple tables and queries
    By nboscaino in forum Microsoft Access
    Replies: 1
    Last Post: August 21st, 2008, 07:55 PM
  4. finding values
    By todd2006 in forum ASP Development
    Replies: 2
    Last Post: June 23rd, 2008, 03:03 AM
  5. Permissions on Tables, Stored Procedures, etc.
    By theChris in forum Microsoft SQL Server
    Replies: 2
    Last Post: March 24th, 2008, 12:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO