[ASP] Access and Excel - preventing duplicate records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    [ASP] Access and Excel - preventing duplicate records

    Hi everyone.

    With ASP I register in DB Access rows of one file excel; this procedure works but problem: Access record double rows of file excel.

    You can exclude from registration double rows ?

    I attach the file excel record to access.

    I register with this logic:

    1) all rows;
    2) where rows no data register date of today;
    3) if same row with a date and the other undated eliminate row undate.

    Register rows excel file number: 1, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14
    Discard rows 2, 3 and 5
    In the field date rows 6, 7, 8, 9, 10, 11, 12, 13, 14 record date of today.

    Code:
    <!--#include virtual="/include/ConnMySQL.asp"-->
    
    <%
    
      Set Rs = Server.CreateObject("ADODB.Recordset")
      Rs.Open "SELECT DISTINCT * from [Foglio1$]", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("/public/UploadFolder/Book-2.xls")&";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
    
    Do Until Rs.Eof 
    
    if IsDate(Rs("DATA")) then
    strData = formatDBDate(Rs("DATA"), "mysql")
    
       strSql = "INSERT INTO Tabella (F1, F2, DataF3) VALUES ( " & Rs("f1") & ", '" & Rs("f2") & "', " & strData & ") "
    
    else
    strData = formatDBDate(Date(), "mysql")
    
       strSql = "INSERT INTO Tabella (F1, F2, DataF3) VALUES ( " & Rs("f1") & ", '" & Rs("f2") & "', " & strData & ") "
    end if  
    objconn.execute(strSql)
    
    response.write strSQL &"<br />"
    
        Rs.MoveNext   
        Loop
        
     
      Rs.close()    
      set Rs = nothing  
      
      objconn.Close()
      Set objconn = Nothing 
    
    %>
    Help please.
    Viki1967
    Attached Files
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Sorry, I had a hard time trying to understand what your question is. Are you still looking for a solution?

    Jared

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      Yes, I looking for a method to ensure duplicate rows are not inserted in the table Access:

      Code:
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 333, 'CCC', '2008-03-06') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 333, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 333, 'CCC', '2008-03-11') ==> NO
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 334, 'CCC', '2008-03-06') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 334, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 335, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 336, 'CCC', '2008-03-11') ==> YES 
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 337, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 338, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 339, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 340, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 341, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 342, 'CCC', '2008-03-11') ==> YES
      INSERT INTO Tabella (F1, F2, DataF3) VALUES ( 343, 'CCC', '2008-03-11') ==> YES
      Regards,
      viki

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        The easiest method I have found to not duplicate records is to search for the record I am looking for:
        Code:
        Query = "Select * FROM Tabella WHERE F1 = 333 AND F2 = 'CCC' AND DataF3 = '2008-03-06'"
        objRS.open query, objConn, adOpenDynamic, adLockOptimistic
        
        if objRS.eof then 'no matching record was found, so I need to add it
           objRS.addNew
           objRS("F1") = 333
           objRS("F2") = "CCC"
           objRS("DataF3") = "2008-03-06"
           objRS.update
        else 'record was found
           response.write "This record is already in the db"
        end if
        Does this make sense?

        Jared

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Many thanks, I don't have it any problem:

          Code:
          <!--#include virtual="/include/ConnMySQL.asp"-->
          
          <%
          
            Set Rs = Server.CreateObject("ADODB.Recordset")
            Rs.Open "SELECT * FROM [Foglio1$]","Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("/public/UploadFolder/Book-2.xls")&";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
          
          Do Until Rs.Eof
          
          if IsDate(Rs("Data")) then
             strData = formatDBDate(Rs("Data"), "mysql")
          else
             strData = formatDBDate(Date(), "mysql")
          end if
          
          Query = " SELECT * FROM Tabella WHERE F1 = " & Rs("F1") & " AND F2 = '" & Rs("F2") & "' AND DataF3 = " & strData & " "
          Set objRS = Server.CreateObject("ADODB.Recordset")
          objRS.open Query, objconn
          
          response.write Query & "<br><br>"
          
          if objRS.eof then
             strSql = "INSERT INTO Tabella (F1, F2, DataF3) VALUES ( " & Rs("f1") & ", '" & Rs("f2") & "', " & strData & ") "
             objconn.execute(strSql)
             response.write strSQL &"<br />"
          
          else
             response.write "This record is already in the db"
             
          end if
          
            objRS.close()    
            set objRS = nothing 
          
            Rs.MoveNext   
            Loop
          
            Rs.close()    
            set Rs = nothing  
            
            objconn.Close()
            Set objconn = Nothing 
          
          %>

          Comment

          Working...