insert CSV into access

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

  1. #1

    Default insert CSV into access

    Hi,

    I use the following code to insert the data from my CSV into an access
    database. But now I want the script to UPDATE the courses id they
    already exist (so when the COURSENUMBER is known in the table),
    otherwise he has to add just a new record.

    How can I do this?

    Thanks!
    Joost

    <%
    'create instance of the Connection object
    Set objRS = Server.CreateObject("ADODB.Recordset")
    SQLstmt = "SELECT * FROM blaat"
    objRS.Open SQLstmt, Conn , adOpenStatic , adLockOptimistic
    'declare our variables for the file handling
    Dim objFSO , strURL , objFile
    'create an instance of the file system object
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    'this is the csv file
    strURL = Server.MapPath("test.csv")
    'open the file
    Set objFile = objFSO.OpenTextFile(strURL)
    'while we are not at the end of the file
    Do While Not objFile.AtEndOfStream
    'store the contents of the file in strText
    strText = objFile.readLine
    'split the strText
    arrText = split(strText, ";", 6)
    objRS.AddNew
    objRS("coursenumber") = arrText(0)
    objRS("place") = arrText(1)
    objRS("time") = arrText(2)
    objRS("day1") = arrText(3)
    objRS("day2") = arrText(4)
    objRS("day3") = arrText(5)
    objRS.Update
    Loop
    'close and destroy objects
    objRS.Close
    objFile.Close
    Set objRS = nothing
    Set Conn = nothing
    Set objFile = Nothing
    Set objFSO = Nothing
    Response.Write("status ok")
    %>
    Joost Guest

  2. Similar Questions and Discussions

    1. INSERT Error from Access 97
      I get the follow syntax error when trying to run the following SQL: INSERT INTO USERS (username, fullname, salt, password) VALUES ('stephen7',...
    2. Help with Flash insert to access
      I am hoping someome can help me. I have a flash form that is to inderst to an Access database via an asp insert form. I had this survey...
    3. Need Utility to insert a CSV to ACCESS Table
      Hi Guys, first of all thanks for your help. Can someone please help me find a ColdFusion component or custom tag which will allow me to import a...
    4. Insert to Access database error*** Please Help
      When trying to insert into access database with a form created by the standard MX2004 Insert and form wizard, get the "Syntax " error as below. Line...
    5. access insert statement
      have you tried? was there an error? I'm not sure that Access has the "@@identity".... I'd suggest trying and posting the specific errors. --...
  3. #2

    Default Re: insert CSV into access

    > Set objRS = Server.CreateObject("ADODB.Recordset")
    > SQLstmt = "SELECT * FROM blaat"
    Stop doing this, for starters. Why do you need to select the entire table
    back to the ASP page, just to add rows?
    > Do While Not objFile.AtEndOfStream
    Stop doing this, also. How about:

    arrLines = split(objFile.ReadAll(), vbCrLf)
    for i = 0 to ubound(arrLines)
    arrText = split(arrLines(0), ";", 6)
    ...
    next
    > objRS.AddNew
    > objRS("coursenumber") = arrText(0)
    > objRS("place") = arrText(1)
    > objRS("time") = arrText(2)
    > objRS("day1") = arrText(3)
    > objRS("day2") = arrText(4)
    > objRS("day3") = arrText(5)
    > objRS.Update
    Ugh. As you're going through the loop, assuming CourseNumber is, in fact, a
    number:



    sql = "SELECT courseNumber FROM blaat " & _
    " WHERE courseNumber=" & arrText(0)

    set rs = conn.execute(sql)

    if rs.eof then
    sql = "INSERT blaat(coursenumber, ...) " & _
    "VALUES(" & courseNumber & ", ...)"
    else
    sql = "UPDATE blaat SET ... " & _
    "WHERE courseNumber = " & arrText(0)
    end if
    conn.execute sql, , 129


    Aaron Bertrand - MVP 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