Runtime error in MS-Access

Thread Solved

Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

Re: Runtime error in MS-Access

 
0
  #21
Aug 13th, 2008
Hi! Came out with the solution just now. plz check out this.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private db As ADODB.Connection
  2. Private rs1 As ADODB.Recordset
  3. Private rs2 As ADODB.Recordset
  4.  
  5. Private Sub Command_Click()
  6. Call fn_DeleteDuplicateRecords("Attribute", "TmpAttribute")
  7. End Sub
  8.  
  9. Public Function fn_DeleteDuplicateRecords(ByVal strTableName As String, ByVal strDupTableName As String)
  10.  
  11. Dim iCatalog As New ADOX.Catalog
  12. Dim i As Integer, j As Integer
  13. Dim strOpen As String
  14.  
  15. On Error GoTo ErrTrap
  16.  
  17. Set db = New ADODB.Connection
  18. Set rs1 = New ADODB.Recordset
  19. Set rs2 = New ADODB.Recordset
  20.  
  21. db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\ProductClass.mdb;Persist Security Info=False"
  22.  
  23. iCatalog.ActiveConnection = db
  24.  
  25. For i = 0 To iCatalog.Tables.Count() - 1
  26. If iCatalog.Tables(i).Name = "NEWTAB" Then 'If table strDupTableName found then
  27. db.Execute "DROP TABLE NEWTAB "
  28. iCatalog.Tables.Refresh
  29. Exit For
  30. End If
  31. Next
  32.  
  33. db.Execute "CREATENEWTAB", , adCmdStoredProc
  34. rs1.Open " SELECT Attribute.ParentUUID, Attribute.Alias_Attr, First(Attribute.Value_Attr) AS FirstOfValue_Attr, Attribute.Type_Attr FROM " & strTableName & " GROUP BY Attribute.ParentUUID, Attribute.Alias_Attr, Attribute.Type_Attr ", db, adOpenKeyset, adLockReadOnly
  35. rs2.Open " SELECT * FROM NEWTAB ", db, adOpenDynamic, adLockOptimistic
  36.  
  37. While Not rs1.EOF
  38. rs2.AddNew
  39. For i = 0 To rs1.Fields.Count - 1
  40. If i = 2 Then
  41. If Len(rs1.Fields(2)) > 255 Then
  42. For j = 1 To Len(rs1.Fields(2)) Step 255
  43. rs2.Fields(i) = rs2.Fields(i) & Mid(rs1.Fields(i), j, 255)
  44. Next
  45. Else
  46. rs2.Fields(i) = rs1.Fields(i)
  47. End If
  48. Else
  49. rs2.Fields(i) = rs1.Fields(i)
  50. End If
  51. Next i
  52. rs2.Update
  53. rs1.MoveNext
  54. Wend
  55.  
  56. If Not rs1 Is Nothing Then
  57. If rs1.State Then rs1.Close
  58. Set rs1 = Nothing
  59. End If
  60. If Not rs2 Is Nothing Then
  61. If rs2.State Then rs2.Close
  62. Set rs2 = Nothing
  63. End If
  64.  
  65. iCatalog.Tables.Refresh
  66. For i = 0 To iCatalog.Tables.Count() - 1
  67. If iCatalog.Tables(i).Name = "NEWTAB" Then 'If table strDupTableName found then
  68. db.Execute "DROP TABLE" & " " & strTableName 'Drop tables original
  69. iCatalog.Tables.Refresh
  70. Exit For
  71. End If
  72. Next
  73.  
  74. 'sometimes Maybe the Execution of the strOpen query is not completed yet.
  75. 'u r trying to delete the original table only after the new table created is found.
  76. 'so the above loop may result in not deleting the original table.
  77. 'Or u try to get help of waiting till the query is executed by the ADO and get a intimation
  78. 'once the query is finished and the new table with distinct records is created.
  79. iCatalog.Tables.Refresh
  80. For i = 0 To iCatalog.Tables.Count() - 1
  81. If iCatalog.Tables(i).Name = "NEWTAB" Then 'If table strDupTableName found then
  82. iCatalog.Tables(i).Name = strTableName 'rename to strTableNamer
  83. iCatalog.Tables.Refresh
  84. Exit For
  85. End If
  86. Next
  87. MsgBox "DELETED DUPLICATE RECORDS SUCCESSFULLY."
  88.  
  89. ErrTrap_Exit:
  90. If Not rs1 Is Nothing Then
  91. If rs1.State Then rs1.Close
  92. Set rs1 = Nothing
  93. End If
  94. If Not rs2 Is Nothing Then
  95. If rs2.State Then rs2.Close
  96. Set rs2 = Nothing
  97. End If
  98. If Not db Is Nothing Then
  99. If db.State Then db.Close
  100. Set db = Nothing
  101. End If
  102. If Not iCatalog Is Nothing Then
  103. Set iCatalog = Nothing
  104. End If
  105. Exit Function
  106.  
  107. ErrTrap:
  108. MsgBox Err.Number & " : " & Err.Description
  109. Resume ErrTrap_Exit
  110.  
  111. End Function
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Runtime error in MS-Access

 
0
  #22
Aug 13th, 2008
Is this code working on the database I sent you?.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

Re: Runtime error in MS-Access

 
0
  #23
Aug 13th, 2008
No. But i added a query there named CREATENEWTAB

Also check out the field names in your database and the code. i had changed the field names here by appending _Attr to last three fields, because type, value etc may be some keywords or reserved VB words.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Runtime error in MS-Access

 
0
  #24
Aug 13th, 2008
can you send me the database on which the above code is working?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

Re: Runtime error in MS-Access

 
1
  #25
Aug 13th, 2008
Check this
Attached Files
File Type: zip ProductClass.zip (39.2 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Runtime error in MS-Access

 
0
  #26
Aug 13th, 2008
dude... Its working on the 'Attribute' table. I am now trying to make it work for all the tables(8 of them in total). I will get back to you if I face any difficulties.

Thanks a lot.... Thanks once again.

Regards,
Dinil
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Runtime error in MS-Access

 
0
  #27
Aug 13th, 2008
I will get back to you soon
Last edited by dinilkarun; Aug 13th, 2008 at 6:59 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Runtime error in MS-Access

 
0
  #28
Aug 13th, 2008
I have now made the code to work for all the tables.....
Thanks a lot dude.....
Keep in touch.....

Thanks once again....

Regards,
Dinil
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC