Problem inserting a autonumber value from a recordset to another table as text

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Problem inserting a autonumber value from a recordset to another table as text

    The problem I'm having is I'm trying to insert a number value (UserID)
    obtained from the first SQL1 query from a table called User. In the
    Access database the column UserID is a autonumber. I'm taking that
    value and storing it as a variable in ASP using vbscript.

    Set RSlookup = conn.execute(SQL1)
    UserID = RSlookup("UserID")

    This works because when I test it using a response.write UserID it
    displays the value I want.

    However, when I run SQL2 which tries to insert the Use rid variable
    into another table called Connections as text all of the data inserts
    except the Userid.

    When it test the query by changing the variable to Userid = 1234 the
    insert statemnet works inserting all the data.

    I believe my problem lies with the UserID = RSlookup("UserID"), the
    UserID is not being stored as text or a number.


    Note: Im using Access 2003 database is in 2000 format


    ************************************************** ***************


    SQL1 = "SELECT * FROM User WHERE Name='"&Name&"'"

    SQL2 = "INSERT INTO Connections (UserID, InternalIP, ExternalIP,
    TimeofConnection, DateOfConnection) Values("_
    & "'" & UserID & "', "_
    & "'" & local & "', "_
    & "'" & external & "', "_
    & "'" & timeofuser & "', "_
    & "'" & dateofuser& "')"

    'Queries the table Users and then stores UserID from a recordset into
    a variable called UserID

    conn.open connstring
    Set RSlookup = conn.execute(SQL1)
    UserID = RSlookup("UserID")
    conn.close

    'Inserts Info including the UserID from UserID variable obtain by SQL1
    conn.open connstring
    conn.execute(SQL2)
    conn.close
    Dave Posh Guest

  2. Similar Questions and Discussions

    1. Problem inserting arabic caracters in SQL server table
      I have a problem when trying to insert arabic characters in SQL Server data base from a Coldfusion MX 7 cfml page. All arabic sent characters to...
    2. recordset table names problem
      I just upgraded to CF 7 and now my table names show up as a file path instead of just the table name in Dreamweaver when creating new recordsets. ...
    3. Simple Problem - Inserting current date into table
      Hello Im trying to create a form in which when someone registers, it also adds the current date into a specified field from a hiddenfield...
    4. DELETE table and reset AutoNumber
      I'm using MS-Access and was wondering if there's a way to DELETE a table and reset the primary key which is an AutoNumber field. If I do "DELETE...
    5. Inserting text box into a form, displaying values from recordset
      I'm trying to create a form that allows me to modify the contents of list. When the user clicks on the modify button, it takes them to a page which...
  3. #2

    Default Re: Problem inserting a autonumber value from a recordset to another table as text

    Dave Posh wrote:
    >
    > ************************************************** ***************
    >
    >
    > SQL1 = "SELECT * FROM User WHERE Name='"&Name&"'"
    >
    > SQL2 = "INSERT INTO Connections (UserID, InternalIP, ExternalIP,
    > TimeofConnection, DateOfConnection) Values("_
    > & "'" & UserID & "', "_
    At this point, UserID HAS NO VALUE

    :-)

    Move this code to after the line of code where you give UserID a value.

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  4. #3

    Default Re: Problem inserting a autonumber value from a recordset to another table as text

    > SQL1 = "SELECT * FROM User WHERE Name='"&Name&"'"
    >
    > SQL2 = "INSERT INTO Connections (UserID, InternalIP, ExternalIP,
    > TimeofConnection, DateOfConnection) Values("_
    > & "'" & UserID & "', "_
    > & "'" & local & "', "_
    > & "'" & external & "', "_
    > & "'" & timeofuser & "', "_
    > & "'" & dateofuser& "')"
    Where are all of these values populated??? Do they come from the User
    table, some other part of the ASP script, thin air?

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)


    Aaron [SQL Server MVP] Guest

  5. #4

    Default Re: Problem inserting a autonumber value from a recordset to another table as text

    Here is my full code.

    <%
    dim conn
    dim connstring

    Set conn = Server.CreateObject("ADODB.Connection")
    connstring = "DRIVER={Microsoft Access Driver
    (*.mdb)};DBQ=C:\databasefile\rcontrol.mdb"
    %>

    <%
    Function WriteToDB(IP)
    dim SQL1
    dim SQL2
    dim SQL3
    dim RSlookup
    dim UserID
    dim local
    dim external
    dim Name
    dim Branch
    dim ext


    dim dateofuser
    dim timeofuser

    local = IP
    Name = Request.form("nametxt")
    Branch = Request.form("branchtxt")
    ext = Request.form("exttxt")



    external = Request.ServerVariables("REMOTE_ADDR")

    dateofuser = date()
    timeofuser = time()


    SQL1 = "SELECT * FROM User WHERE Name='"&Name&"'"

    SQL2 = "INSERT INTO Connections (UserID, InternalIP, ExternalIP,
    TimeofConnection, DateOfConnection) Values("_
    & "'" &UserID& "', "_
    & "'" & local & "', "_
    & "'" & external & "', "_
    & "'" & timeofuser & "', "_
    & "'" & dateofuser& "')"

    SQL3 = "INSERT INTO User (Name, Branch, Phone, DateofUser, TimeOfUser)
    Values("_
    & "'" & Name & "', "_
    & "'" & Branch & "', "_
    & "'" & ext & "', "_
    & "'" & dateofuser & "', "_
    & "'" & timeofuser & "')"

    conn.open connstring

    Set RSlookup = conn.execute(SQL1)

    if RSlookup.eof then
    conn.execute(SQL3)
    conn.close

    conn.open
    Set RSlookup = conn.execute(SQL1)
    UserID = RSlookup("UserID")
    conn.close

    conn.open connstring
    response.write userid
    conn.execute(SQL2)

    conn.close

    else

    UserID = RSlookup("UserID")
    conn.close
    conn.open connstring
    response.write userid
    conn.execute(SQL2)
    conn.close

    end if



    dim fs, f
    set fs = Server.CreateObject("Scripting.FileSystemObject")
    set f = fs.CreateTextFile("c:\website\batch\"&Name&".bat", true)
    f.WriteLine("c:\progra~1\radmin\radmin.exe /connect:"&local&"
    /Through:"&external )

    f.Close
    set f=nothing
    set fs=nothing

    response.write "<a href=""batch/"&Name&".bat"">"&Name&"</a></p>"


    end function
    Dave Posh Guest

  6. #5

    Default Re: Problem inserting a autonumber value from a recordset to another table as text

    What's the problem? I didn't make myself clear enough?

    Look, you can't create SQL2 until AFTER you open the recordset on SQL1 and
    assign a value to UserID. Is that clearer? Like this:

    SQL1 = " ... "
    Set RSlookup = conn.execute(SQL1)
    UserID = RSlookup("UserID")

    SQL2 = " ... "
    etc.

    Bob Barrows

    Dave Posh wrote:
    > Here is my full code.
    >
    > <%
    > dim conn
    > dim connstring
    >
    > Set conn = Server.CreateObject("ADODB.Connection")
    > connstring = "DRIVER={Microsoft Access Driver
    > (*.mdb)};DBQ=C:\databasefile\rcontrol.mdb"
    > %>
    >
    > <%
    > Function WriteToDB(IP)
    > dim SQL1
    > dim SQL2
    > dim SQL3
    > dim RSlookup
    > dim UserID
    > dim local
    > dim external
    > dim Name
    > dim Branch
    > dim ext
    >
    >
    > dim dateofuser
    > dim timeofuser
    >
    > local = IP
    > Name = Request.form("nametxt")
    > Branch = Request.form("branchtxt")
    > ext = Request.form("exttxt")
    >
    >
    >
    > external = Request.ServerVariables("REMOTE_ADDR")
    >
    > dateofuser = date()
    > timeofuser = time()
    >
    >
    > SQL1 = "SELECT * FROM User WHERE Name='"&Name&"'"
    >
    > SQL2 = "INSERT INTO Connections (UserID, InternalIP, ExternalIP,
    > TimeofConnection, DateOfConnection) Values("_
    > & "'" &UserID& "', "_
    > & "'" & local & "', "_
    > & "'" & external & "', "_
    > & "'" & timeofuser & "', "_
    > & "'" & dateofuser& "')"
    >
    > SQL3 = "INSERT INTO User (Name, Branch, Phone, DateofUser, TimeOfUser)
    > Values("_
    > & "'" & Name & "', "_
    > & "'" & Branch & "', "_
    > & "'" & ext & "', "_
    > & "'" & dateofuser & "', "_
    > & "'" & timeofuser & "')"
    >
    > conn.open connstring
    >
    > Set RSlookup = conn.execute(SQL1)
    >
    > if RSlookup.eof then
    > conn.execute(SQL3)
    > conn.close
    >
    > conn.open
    > Set RSlookup = conn.execute(SQL1)
    > UserID = RSlookup("UserID")
    > conn.close
    >
    > conn.open connstring
    > response.write userid
    > conn.execute(SQL2)
    >
    > conn.close
    >
    > else
    >
    > UserID = RSlookup("UserID")
    > conn.close
    > conn.open connstring
    > response.write userid
    > conn.execute(SQL2)
    > conn.close
    >
    > end if
    >
    >
    >
    > dim fs, f
    > set fs = Server.CreateObject("Scripting.FileSystemObject")
    > set f = fs.CreateTextFile("c:\website\batch\"&Name&".bat", true)
    > f.WriteLine("c:\progra~1\radmin\radmin.exe /connect:"&local&"
    > /Through:"&external )
    >
    > f.Close
    > set f=nothing
    > set fs=nothing
    >
    > response.write "<a href=""batch/"&Name&".bat"">"&Name&"</a></p>"
    >
    >
    > end function
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows [MVP] Guest

  7. #6

    Default Re: Problem inserting a autonumber value from a recordset to another table as text

    Thanks Bob, works great!
    Dave Posh Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139