Ruddles
Well-known Member
- Joined
- Aug 24, 2010
- Messages
- 5,851
- Office Version
- 365
- Platform
- Windows
I'm using Access as a DDE server. If I do this, everything works fine:-
vData is an array and I can get the data from it without any problem.
However if I do this:-
vData is now type String and contains "Error 2023". The only differences between the two sets of code (apart from the procedure name) are the bits in red.
I've based my code on the code samples on MSDN (2003: http://msdn.microsoft.com/en-us/library/aa172266(v=office.11).aspx; 2007: http://msdn.microsoft.com/en-us/library/bb242443(v=office.12).aspx). All the other code I've copied off these pages has worked first time except for the sample which uses DDEPoke. I've treble-checked it and I can't see that I've made any typos in adapting it to suit my database.
I've tried the DDEInitiate with and without a trailing space at the end of "database1;SQL" (just in case). I've tried DDERequest with "All", "Data", "FieldNames", "FieldCount" and "FirstRow": all attempts result in an Error 2023.
I've tried stepping through the code to check it's not a sequencing or timing problem but everything seems to happen and complete in the correct order.
Googling the error on the basis that it's an Access error code suggests the problem is: "The file name you specified for the data access page already exists". However people have also reported this error code using Excel as the DDE server.
Has anyone successfully sent SQL to Access using DDEPoke?
Even if you haven't, I'm prepared to listen to (almost) any suggestions!
Code:
Sub Test1()
Dim iChannel1 As Integer
Dim iChannel2 As Integer
Dim sSQL As String
Dim vData As Variant
Shell "MSAccess " & dbPath & "database1.mdb", vbMinimizedNoFocus
iChannel1 = DDEInitiate("MSAccess", "System")
DDEExecute iChannel1, "[OpenDatabase database1]"
[COLOR=red][B] iChannel2 = DDEInitiate("MSAccess", "database1;SQL SELECT * FROM tblCars;")
[/B][/COLOR][COLOR=black] vData = DDERequest(iChannel2, "All")
[/COLOR] DDETerminate iChannel2
DDEExecute iChannel1, "[CloseDatabase]"
DDEExecute iChannel1, "[Quit]"
DDETerminate iChannel1
End Sub
However if I do this:-
Code:
Sub Test2()
Dim iChannel1 As Integer
Dim iChannel2 As Integer
Dim sSQL As String
Dim vData As Variant
Shell "MSAccess " & dbPath & "database1.mdb", vbMinimizedNoFocus
iChannel1 = DDEInitiate("MSAccess", "System")
DDEExecute iChannel1, "[OpenDatabase database1]"
[COLOR=red][B] iChannel2 = DDEInitiate("MSAccess", "database1;SQL")[/B][/COLOR]
[COLOR=red][B] DDEPoke iChannel2, "SQLText", "SELECT * FROM tblCars;"
[/B][/COLOR][COLOR=black] vData = DDERequest(iChannel2, "All")
[/COLOR] DDETerminate iChannel2
DDEExecute iChannel1, "[CloseDatabase]"
DDEExecute iChannel1, "[Quit]"
DDETerminate iChannel1
End Sub
I've based my code on the code samples on MSDN (2003: http://msdn.microsoft.com/en-us/library/aa172266(v=office.11).aspx; 2007: http://msdn.microsoft.com/en-us/library/bb242443(v=office.12).aspx). All the other code I've copied off these pages has worked first time except for the sample which uses DDEPoke. I've treble-checked it and I can't see that I've made any typos in adapting it to suit my database.
I've tried the DDEInitiate with and without a trailing space at the end of "database1;SQL" (just in case). I've tried DDERequest with "All", "Data", "FieldNames", "FieldCount" and "FirstRow": all attempts result in an Error 2023.
I've tried stepping through the code to check it's not a sequencing or timing problem but everything seems to happen and complete in the correct order.
Googling the error on the basis that it's an Access error code suggests the problem is: "The file name you specified for the data access page already exists". However people have also reported this error code using Excel as the DDE server.
Has anyone successfully sent SQL to Access using DDEPoke?
Even if you haven't, I'm prepared to listen to (almost) any suggestions!