Solved

How do I write a "variable as variant" array to a microsoft access mdb file?

Posted on 2006-07-20
9
236 Views
Last Modified: 2010-04-30
So far im able to write string variables into a microsoft mdb file.
My question is, what do i need to change in my code to write a variant array into an mdb?

Dim conn As New ADODB.Connection
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test\MyDataBase.mdb;"
    rs.Open "MyTable", conn, adOpenDynamic, adLockOptimistic
    rs.AddNew
    rs.Fields("Test1").Value = StrDocName        'I write a string variable here into Field column  "Test1" as a new value
    rs.Fields("Test2").Value = StrStreamPath     'I write a string variable here into Field column "Test2" one as a new value
    '    rs.Fields("Test3").Value = VariantArray        'Now I want to write a variant array into the field, how can i do this?
    rs.Update
    rs.Close
    Set rs = Nothing
   
    conn.Close
   
0
Comment
Question by:sbronsge
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 1

Accepted Solution

by:
maonas earned 400 total points
Comment Utility
You cannot define a field in MS Access with array data type.You can define only Text, Number, Memo, Number, Date/Time...data type for an associated field.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Hi sbronsge,

>     '    rs.Fields("Test3").Value = VariantArray        'Now I want to write a
> variant array into the field, how can i do this?

You cannot write an array to a field--a field contains a single data element, while arrays are n-dimensional structures
with potentially many members.  You can write a particular member to a field, though:

rs.Fields("Test3").Value = VariantArray(5) 'writes the member with index 5

Regards,

Patrick
0
 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
mdb Recordsets store string objects or doubles. If I'm malinformed in saying this, stop me now, otherwise continue. They do not store objects or references, and storing a reference as a string in one will not keep the array valid, and it will still be garbage collected when your program ends.

Your term "VariantArray" does not point to all those variants that you are wishing to store. What it points to is the memory location of first Variant in that array, which in turn points to the next, and so on. Storing this variable would do nothing but store a reference to the memory location of the first variant (which, as I said, would dissapear after garbage collecting)

The only way to do this would be to create a variable number of fields and store string representations of the contents of the array. The more complicated the variants in the array, the more fields you'll need to store each one. That's all you can do. I'll give you a start by saying you'll probably need something like this:

    rs.Fields("Test1").Value = StrDocName
    rs.Fields("Test2").Value = StrStreamPath
    For Cnt = 1 to UBound(VariantArray)
           rs.Fields("Test" & (2 + Cnt) ).Value = Str( VariantArray(Cnt) )
    Next Cnt
    rs.Update
    rs.Close


Let me know if there's any further guidance I can give you.

Alain
0
 

Author Comment

by:sbronsge
Comment Utility
thx for the updates.

So far I tried all suggestions, but best way is to implement a counter like Alain told.
But I have a problem with this code.
Its about:


    For Cnt = 1 to UBound(VariantArray)
           rs.Fields("Test" & (2 + Cnt) ).Value = Str( VariantArray(Cnt) )
    Next Cnt


Str( VariantArray(Cnt) )    > in my Watch window I see the correct value for my variant array: " 12121212".
but when I select "Value" and see the watch window, I got an error: Item cannot be found in the collection corresponding.....
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
The Watch window will not be able to view the value being after it's placed in the database because viewing a value requires a method call to the database with the proper key, and the watch does not do method calls, it merely surveys memory locations. My bet is if you end your program and open up the database is MS Access, it will all be there.

Alain
0
 

Author Comment

by:sbronsge
Comment Utility
it works thanks Alain and Matthewspatrick!
0
 

Author Comment

by:sbronsge
Comment Utility
guys how do I give alain and patrick points for their answers. I want to give them points?
0
 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
Post in the Community Support area, and tell them that you accidentally gave points to the wrong person, they'll fix it for you.

http://www.experts-exchange.com/Community_Support/
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now