PC Review


Reply
Thread Tools Rate Thread

How to delete certain records based on a column value on all tables in a 2003 Access database?

 
 
Athena
Guest
Posts: n/a
 
      17th Jul 2007
Hello,

I have an Access 2003 database with about 49 tables. I want to delete
some records based on a column value ("StudyID") on all the tables in this
database using ADO with VB6. The cosde supposedly should do this. But it
doesn't work. First there is temporary table starting with ~. I had to trap
that. Then if there are tables where there is no "StudyID" field, the code
doesn't work. And secondly if there are no records containing "StudyID=xxx"
then Delete command bombs with an error code 3265. So how can I do this?
Thank you.

Athena

Code
-----------------------------------------------------------------

Private Sub cmdDeleteStudy_Click()

Dim res As Long
Dim sDatabase As String

On Error GoTo ErrHandler

StudyName = TreeView1.SelectedItem.text
sDatabase = TreeView1.SelectedItem.Parent.text

res = MsgBox("Do you want to delete " & StudyName & " from " & sDatabase
& "?", vbOKCancel, "Delete Study")

If res = vbOK Then

res = MsgBox(StudyName & " will be deleted from " & sDatabase & "?",
vbOKCancel, "Confirmation")

If res = vbOK Then

Screen.MousePointer = vbHourglass

' Open new database
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & dBaseName & ";" & _
"Persist Security Info=False"
conn.CursorLocation = adUseClient
conn.Open

Set RS = conn.OpenSchema(adSchemaTables, Array(Empty, Empty,
Empty, "Table"))

Do While Not RS.EOF
If InStr(RS!Table_Name, "~") = 0 Then
sSql = "DELETE * FROM " & RS!Table_Name & " WHERE
StudyID='" & StudyName & "'"
Set RS = conn.Execute(sSql, , adCmdText)
End If
Debug.Print RS!Table_Name
RS.MoveNext
Loop

TreeView1.Nodes.Remove (TreeView1.SelectedItem.Index)
' MDIMain!stbMain.Panels(1).text = "Study " & StudyName & " is
deleted from " & sDatabase & "."
Screen.MousePointer = vbDefault

End If

End If

Exit Sub

ErrHandler:
If Err.Number = -2147217904 Then
Resume Next
Else
MsgBox Err.Number & " " & Err.Description, vbOKOnly + vbCritical
End If

End Sub

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Query - Delete matching records from 2 tables - Access 2000 Chris Stammers Microsoft Access Queries 4 22nd Jan 2009 02:45 PM
Pulling records based of another tables, column Mary Microsoft Access Database Table Design 2 29th Dec 2008 11:26 PM
How to delete some records in all the tables in a database? Athena Microsoft Access 2 16th Apr 2007 11:50 PM
Delete Query of records in a table based on it's finding in two other tables Hendy88@gmail.com Microsoft Access 3 19th Feb 2007 10:08 PM
How to delete records from a table based on an inner join Access 9 =?Utf-8?B?TWFyaW5lcg==?= Microsoft Access Queries 1 30th Dec 2004 11:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:01 PM.