Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
| Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 18th, 2007, 11:02 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code works in Excel VBA but not Access VBA

I am running Office 2003 which has VBA Retail 6.4.8869 running for both Excel and Access. The following code works just fine in Excel but gives a "Compile Error: Can't assign to array" error in Access. It is a DDE request that returns an array.

Function doRequest(serverName, topic, request) As Variant()
Dim chan As Integer
chan = Application.DDEInitiate(serverName, topic)
doRequest = Application.DDERequest(chan, request)
Application.DDETerminate chan
End Function

In Excel, I get an array returned. In access I get the compile error associated with the DDERequest line.

If I change the code to the following, I get two different behaviors out of Excel and Access (note the change is that I just specify a pure variant vs. a dynamic variant array for doRequest).

Function doRequest(serverName, topic, request) As Variant
Dim chan As Integer
chan = Application.DDEInitiate(serverName, topic)
doRequest = Application.DDERequest(chan, request)
Application.DDETerminate chan
End Function

If I make this change, I don't get the compile error in Access. However, Excel still returns a full array. However, Access only returns a string of the values.

It is the same VBA on the same computer. Any ideas why this is happening?

  #2 (permalink)  
Old May 21st, 2007, 07:59 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am not sure I see the difference between this code:

Function doRequest(serverName, topic, request) As Variant()
Dim chan As Integer
chan = Application.DDEInitiate(serverName, topic)
doRequest = Application.DDERequest(chan, request)
Application.DDETerminate chan
End Function

and this code:

Function doRequest(serverName, topic, request) As Variant
Dim chan As Integer
chan = Application.DDEInitiate(serverName, topic)
doRequest = Application.DDERequest(chan, request)
Application.DDETerminate chan
End Function

Be that as it may, Access wants you to Dim the array, like this:

Dim MyArray(i)

Where i is some known value. You could do this by either counting the value before you dim the array, or, normally what I will do is this sort of thing, which won't work for you, but you see how it wants to work:

i = 1
Do Until [some condition]
   ReDim Preserve MyArray(i)
   Add some value
   i = i + 1
Loop

Did that help any?




mmcdonal
  #3 (permalink)  
Old May 21st, 2007, 08:00 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Alternatively, you can also take the string that is returned that you are getting now, and use Split() to get it manually into an array.



mmcdonal


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRD vba code no longer works MWA_Work Crystal Reports 0 March 7th, 2008 09:46 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
Excel VBA to SQL & back to VBA edesousa Excel VBA 1 June 1st, 2004 02:39 AM
ACCESS 2000 VBA CODE TO "SAVE AS" EXCEL ckentebe Access 0 May 25th, 2004 02:13 PM
Excel sometimes loses VBA code James Diamond Excel VBA 0 May 25th, 2004 03:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.