Solved

Create a dynamic array in Access VBA and perform actions on it

Posted on 2010-09-09
5
827 Views
Last Modified: 2013-11-27
I would like help performing the following actions in VBA:

1. Create a dynamic array with two columns. Let’s say column A and column B

2. Add a row to the array from the current values of two variables: ProjectID and CStr(frm.Hwnd) to columns A and B respectively.

3. Search the array and retrieve the value of column B based lookup of column A

4. Delete a row in the array that has a specific value in column A

Thank you for any help on this,
Tim
0
Comment
Question by:TimJar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 33636632
A Dynamic Array is one that is re-dimmed as needed:

Dim arr() as String

'/set multiple columns
Redim Preserve arr(ubound(arr)+1, 1)

arr(0, ubound(arr)) = Me.ProjectID
arr(1, ubound(arr)) = frm.Hwnd

To retrieve this:

Msgbox arr(0, <your array index>)

If you need to locate that value, use a Loop:

Dim i As Integer

For i = 0 to ubound(arr,2)
  Msgbox arr(0, i) & " : " & arr(1, i)
Next i

This might help:
http://patorjk.com/programming/tutorials/vbarrays.htm
0
 
LVL 4

Expert Comment

by:r0bertdenir0
ID: 33637709
From your description - you are not just storing but searching & deleting.
A collection might be a better choice for your problem.
Maybe you should tell us a bit more about what you're trying to do...
 
0
 

Author Comment

by:TimJar
ID: 33639663
I am using Allen Browne's method of opening multiple instances of a form (frmProjects). The problem is if I click a link to open an instance of frmProject, and a form with that ProjectID is already open, it will open another instance and I have two forms open referencing the same ProjectID. I would like to keep a log of ProjectID and the corrusponding frm.Hwnd so I can check if a instance is already open and if so set focus to that open form instead of opening a new one.

If I store the data in a table it would have two fields: ProjectID and Hwnd. Then, before I open a new instance, I will dlookup("Hwnd","tableName","ProjectID=" & "ProjectID I'm trying to open") to see if I should go to that form based on the frm.Hwnd or open a new one.

I would have to append to this table when I open a new instance and delete a record when I close an instance.

It seems to me it would be better to use an array for this instead of a table. I haven't thought about how to use a collection
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 33639938
Sometimes it is useful to write data to a temp table, so it can examined in the interface.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 33639945
If you are going to use DLookup, I think you will need to save the data to a table.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
run FileCopy code from a string 6 52
Win 10 Automation Error with .doCmd in Access 2 83
Email question 19 87
MS Access vba Print  PDF First page only 16 27
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 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