|
|
 |
| 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 tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

May 18th, 2007, 11:02 AM
|
|
Registered User
|
|
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

May 21st, 2007, 07:59 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,064
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
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
|

May 21st, 2007, 08:00 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,064
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |