473,466 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error checking while inserting records in access

1 New Member
Hi to all,

I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate record.

My code inserts records one by one using addnew-updatebatch. If there is a duplicate in the db, it will display "already exists" and if it is not in the db it will display "record added".

Below is my asp code I found on the net and changed it to my needs. The script is almost working but it does not display the texts properly. Any help would be appreciated.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Import Data From Excel into Access</title>
  4. </head>
  5. <body>
  6. <%
  7. on error resume next
  8. Dim cnnExcel, rstExcel
  9. Dim cnnAccess, rstAccess 
  10. Dim strExcelFile
  11. Dim strAccessFile,xIndex
  12.  
  13. strExcelFile = Server.MapPath(".") & "/database/Companies.xls"
  14. strAccessFile = Server.MapPath(".") & "/database/test.mdb"
  15.  
  16. ' Open Excel Connection
  17. Set cnnExcel = Server.CreateObject("ADODB.Connection")
  18. cnnExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  19. "Data Source=" & strExcelFile & ";" & _
  20. "Extended Properties=""Excel 8.0;HDR=NO;"""
  21. ' Response.Write "Excel connection opened<BR>"
  22.  
  23. ' Load ADO Recordset with Excel Data
  24. Set rstExcel = Server.CreateObject("ADODB.Recordset")
  25. rstExcel.Open "Select * from [Sheet1$A2:Q10000]", cnnExcel, adOpenStatic 
  26. ' Response.Write "Excel Recordset loaded<BR>"
  27.  
  28. ' Open Access Connection
  29. Set cnnAccess = Server.CreateObject("ADODB.Connection")
  30. cnnAccess.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  31. "Data Source=" & strAccessFile & ";" & _
  32. "Persist Security Info=False"
  33. ' Response.Write "Access connection opened<BR>"
  34.  
  35. ' Load ADO Recordset with Access Data
  36. Set rstAccess = Server.CreateObject("ADODB.Recordset")
  37. rstAccess.Open "coup", cnnAccess, adOpenKeyset, adLockOptimistic, adCmdTable
  38. ' Response.Write "Access Recordset loaded<BR>"
  39. %><table border="1">
  40. <tr>
  41. <%
  42. 'Synchronize Recordsets and Batch Update
  43.  
  44. Do Until rstExcel.EOF
  45. If IsNull(rstExcel.Fields(0).Value) Then Exit Do
  46. Response.Write("<tr>")
  47. Response.Write("<td>&nbsp;" & rstExcel.Fields(0).Value & "</td>")
  48. Response.Write("<td>&nbsp;" & rstExcel.Fields(1).Value & "</td>")
  49. Response.Write("<td>&nbsp;" & rstExcel.Fields(2).Value & "</td>")
  50. Response.Write("<td>&nbsp;" & rstExcel.Fields(3).Value & "</td>")
  51. Response.Write("<td>&nbsp;" & rstExcel.Fields(4).Value & "</td>")
  52. Response.Write("<td>&nbsp;" & rstExcel.Fields(5).Value & "</td>")
  53. Response.Write("<td>&nbsp;" & rstExcel.Fields(6).Value & "</td>")
  54. Response.Write("<td>&nbsp;" & rstExcel.Fields(7).Value & "</td>")
  55. Response.Write("<td>&nbsp;" & rstExcel.Fields(8).Value & "</td>")
  56. if err.Number <> 0 then
  57. Response.Write "<td>&nbsp;record exist." 
  58. Response.Write "&nbsp;</td>"
  59. err.clear 
  60. else
  61. With rstAccess
  62.  
  63. .AddNew
  64.  
  65. .Fields("other") = rstExcel.Fields(0).Value
  66. .Fields("name") = rstExcel.Fields(1).Value
  67. .Fields("amount") = rstExcel.Fields(2).Value
  68. .Fields("percent") = rstExcel.Fields(3).Value 
  69. .Fields("startdate") = rstExcel.Fields(4).Value
  70. .Fields("enddate") = rstExcel.Fields(5).Value 
  71. .Fields("limit") = rstExcel.Fields(6).Value
  72. .Fields("products") = rstExcel.Fields(7).Value 
  73. .Fields("minimum") = rstExcel.Fields(8).Value
  74. Response.Write "<td>&nbsp;record added</td>"
  75.  
  76.  
  77. End With
  78.  
  79. end if
  80. Response.Write("</tr>")
  81.  
  82. rstExcel.MoveNext
  83. Loop
  84. Response.Write("</tr></table>")
  85. rstAccess.UpdateBatch
  86. ' Response.Write "Excel data written to Access<BR>"
  87.  
  88.  
  89. 'Clean up
  90. rstExcel.Close
  91. Set rstExcel = Nothing
  92. rstAccess.Close
  93. Set rstAccess = Nothing
  94. cnnExcel.Close
  95. Set cnnExcel = Nothing
  96. cnnAccess.Close
  97. Set cnnAccess = Nothing
  98.  
  99. Response.Write "Import from Excel to database completed<BR>"
  100.  
  101. %>
  102.  
  103.  
  104. </body>
  105. </html>
Sep 3 '08 #1
2 3090
omerbutt
638 Contributor
it does not display which text can you specify the line on which error is occouring and do wrap your code part with the code tags that makes your post readable
regards,
omer
Sep 4 '08 #2
jhardman
3,406 Recognized Expert Specialist
I agree with Omer, you need to specify what you mean by "does not display the texts properly". Does it not display them at the correct time? Does it not show them at all? Please clarify.

Jared
Sep 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: middletree | last post by:
I had some text links at the top of all my pages (in one include file), which worked just fine. But I was asked to make it so that people in a certain department, (this is an Intranet app) would...
3
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
1
by: Ramesh | last post by:
hi I am trying to insert 50,000 random records in the SQL server. I have created COM for inserting records. When i started inserting, after 44152 records it displayed following error message. "...
0
by: Mamatha | last post by:
Hi I want to insert data into the database from VB.NET,before inserting i want to check those records for eleminate of duplication. I tried with thte dataset but i didn't get the correct...
11
by: pradeepss | last post by:
I am developing an application which grab a comma delimited file and inserts them into a database. but while inserting it gives me an error. the code is below. Do While Not EOF(iDrop) Line...
10
by: Phil Latio | last post by:
I am inserting data into user table which contains 5 fields, sounds simple enough normally but 2 of the fields are designated as UNIQUE. If someone does enter a value which already exists, how do I...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.