Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / February 2005

Tip: Looking for answers? Try searching our database.

Code to fix corrupt table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dixie - 11 Feb 2005 22:57 GMT
I have come across some code to walk through the records of a corrupt table
and copy it record by record to a new table.  It comes from the MVPS.Org
site and is as follows.
-----------------------
(Q) How can I recover records from a corrupt table?

(A) First, using Access while in the database window, copy the table's
structure only from the corrupt table, if possible. Then using the code
below, copy each individual row from the old table to the new one until it
encounters an error. The error routine displays the error, skips one row,
moves to the next and Resumes at Addit where it continues to move data from
the old table to the new table one row at a time.
'  ********* Code Start ***********
' This code was originally written by Norm Chezem

' It is not to be altered or distributed,

' except as part of an application.

' You are free to use it in any application,

' provided the copyright notice is left unchanged.

'

' Code Courtesy of

' Norm Chezem

'

Function CopyRes()

 Dim db As Database

 Dim OldRes As Recordset

 Dim NewRes As Recordset

 Dim ErrMsg1 As String

 Dim RecCount As Long

 On Error GoTo err_Proc

 Set db = CurrentDb()

 Set OldRes = db.OpenRecordset("tbl_Reservations")

 Set NewRes = db.OpenRecordset("tbl_New_Res")

 RecCount = 0

 OldRes.MoveFirst

Do While Not OldRes.EOF

Addit:

 NewRes.AddNew

 NewRes![ResID] = OldRes![ResID]

 'CONTINUE COPYING ALL ROWS FROM OLD TBL TO NEW

 NewRes.Update

 RecCount = RecCount + 1

 DoEvents

 If RecCount Mod 10000 = 0 then

   MsgBox RecCount 'Show progress every 10,000 rows

 End If

Loop

 MsgBox RecCount 'Show total successful record count

 OldRes.Close

 NewRes.Close

 db.close

Proc_Exit:

 Exit Function

Err_Proc:

 MsgBox "<Error>" & Error$

 OldRes.MoveNext 'Skip this corrupt row

 Resume Addit    'Continue at Addit

End Function

'  ********* Code End ***********

I have worked out that the NewRes![ResID]=OldRes![ResID] probably has to be
customised to reflect my unique record name.

What happens when I run this is I get an error for each row that occurs
whenever I have a [Required] field and it tells me that it can't be null as
it is a requred field.

I have the procedure in a module and I am simply typing CopyRes in the
Immediate window.  I have changed the table names in it to suit my
situation.  Can anyone see what I am doing wrong?

dixie
pietlinden@hotmail.com - 12 Feb 2005 04:24 GMT
huh?  This code is missing a serious chunk!  Okay, assuming that the
code is copying field by field into the new table, you need to do
something like

'--add a new blank record to the destination table
rsDest.AddNew

'---populate all the fields in the new record from values from the
current record in the source table
for intCounter = 0 to rsSrc.Fields.Count-1
     rsDest.Fields(intCounter) = rsSrc.Fields(intCounter)
next intCounter
'---save the values in the new record
rsDest.Update

Okay, so then you have to call that for each record in the source
table, so you wrap it in a Do Until rsSrc.EOF thing and away you go.
dixie - 12 Feb 2005 07:49 GMT
The code is supposed to be complete and is at
http://www.mvps.org/access/tables/tbl0018.htm - I just altered the names of
the tables involved and then the line:
NewRes![ResID] = OldRes![ResID] which I took to be the uniqe ID number for
> huh?  This code is missing a serious chunk!  Okay, assuming that the
> code is copying field by field into the new table, you need to do
[quoted text clipped - 13 lines]
> Okay, so then you have to call that for each record in the source
> table, so you wrap it in a Do Until rsSrc.EOF thing and away you go.
dixie - 12 Feb 2005 07:59 GMT
Sorry about the formatting of that last message.  It just occurred to me
Pieter after I reread your message that the single line:
NewRes![ResID]=OldRes![ResID] is just an example of doing one of the fields
of the table.  So, I do have to include all the fields as lines at that
stage of the code.  There are a lot of fields in this table.  I like your
code that seems just to automatically add each field.  I presume I need to
dim intcounter as something before I can use that code - dim intCounter as
integer?

I'll have a play with it with a smaller table.

Thanks

dixie

> huh?  This code is missing a serious chunk!  Okay, assuming that the
> code is copying field by field into the new table, you need to do
[quoted text clipped - 13 lines]
> Okay, so then you have to call that for each record in the source
> table, so you wrap it in a Do Until rsSrc.EOF thing and away you go.
dixie - 15 Feb 2005 03:27 GMT
Well, I must admit I've given up on making this work easily.  I had a
corrupted table last week with over 15,000 records which I had to search
through for 2 corrupted records, then copy block by block the good messages
as I couldn't delete the corrupted ones.  This is just so slow.  If I could
have had this working, I am sure I could have pressed a button and made a
nice cup of tea.

dixie

> huh?  This code is missing a serious chunk!  Okay, assuming that the
> code is copying field by field into the new table, you need to do
[quoted text clipped - 13 lines]
> Okay, so then you have to call that for each record in the source
> table, so you wrap it in a Do Until rsSrc.EOF thing and away you go.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.