LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Deleting records from access using ADO in through Excel

Hi Chris,

First of all, you don't need the Recordsets at all if you're using this
method to delete the records. The SQL statement is executed against the
Connection object, so opening the Recordset is unnecessary overhead. So you
can change your code to this:

Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=P:\test.msd"

If testcondition1=whatever then
sSQL = "DELETE FROM Table1 WHERE [Month]='7'"
cn.Execute sSQL, lRecordsDeleted, adCmdText
End If

If testcondition2=whatever then
sSQL = "DELETE FROM Table2 WHERE [Month]='7'"
cn.Execute sSQL, lRecordsDeleted, adCmdText
End If

'/ make sure you close your Connection object when you're done
cn.Close
Set cn = Nothing


The error message you're getting indicates that perhaps the Field "Month" in
Table2 is not a Character or Text field. Try it without the single quotes
to see if it works.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Chris Dunigan wrote:
Thanks for all your help. All is becoming clearer!!
I am still having one problem that i wondered if you's help me with.

I have the following code:

-----
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=P:\test.msd"

If testcondition1=whatever then
rs.Open "Table1", cn, adOpenKeyset, adLockOptimistic, adCmdTable
sSQL = "DELETE FROM Table1 WHERE [Month]='7'"
cn.Execute sSQL, lRecordsDeleted, adCmdText
End If
rs.Close

If testcondition2=whatever then
rs.Open "Table2", cn, asOpenKeyset, adLockOptimistic, adCmdTable
sSQL = "DELETE FROM Table2 WHERE [Month]='7'"
cn.Execute sSQL, lRecordsDeleted, adCmdText
End If
rs.Close
-----

In the majority of cases i would expect both testcondition1 and
testcondition2 to be met, therefore there will be deletions from
Table1 an Table2. The code initially works, when testcondition1 is
met the data is deleted from Table1, however when the code gets down
to delete data from Table2 i get the following error:

----
Run-time error '-2147217913(80040e07)':
Data type mismatch in criteria expression
----

This error occurs when the line cn.Execute sSQL, lRecordset, adCmdText
is tried.

Do you have any ideas what is going wrong??

Many thanks in advance.
Chris


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting NEARLY multiple records in excel rwbana Excel Discussion (Misc queries) 4 May 8th 08 05:39 PM
Import Access records into Excel dksaluki Excel Discussion (Misc queries) 1 December 20th 07 07:08 AM
How do I display duplicate records in Excel without deleting? jkc1 Excel Worksheet Functions 1 August 23rd 06 07:18 PM
Linking Access Records to Excel MayraEllen Excel Worksheet Functions 0 August 21st 06 07:37 PM
Access records updating from Excel. Dirk Batenburg Excel Programming 2 October 8th 03 03:06 PM


All times are GMT +1. The time now is 08:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"