tags:

views:

99

answers:

4

Hello

I am looking for a method to display virtual rows in an MS Access application, that is, rows that don't actually exist in any table, but only in memory.

I could probably create a temporary table as I need it, fill it with my 'virtual row' values and then display the temporary table's content in the continuous form. Yet, if possible, I'd like to such a thing without using any temporary tables.

Thanks for any help

Rene

edit: When I refer to a 'row in memory', I mean to say that I have a few variables (possibly stored in an array of arrays) that I want to display on the gui/form the same way as rows stored in a table.

A: 

You could add entries to a list box with multiple columns. Not exactly a continuous form but close. Ensure the Row Source Type of the list box is Value List.

Me.SomeListBox.RowSource = ""
Me.SomeListBox.AddItem "record1;field2;field3"
Me.SomeListBox.AddItem "record2;field2;field3"
Tony Toews
A: 

UNION allows you to include virtual rows and virtual fields (columns) can be defined by including:

data As fieldname

FROM can be any table or query in the database, but it must contain at least one row otherwise you'll get an zero-row result set.

So:

SELECT -1 As ID, True As Virtual, "All" As Stuff FROM Table1
UNION
SELECT 0 As ID, True As Virtual, "None" As Stuff FROM Table1

You do not have to include any real rows, but you can:

UNION 
SELECT  ID, False As Virtual, Stuff FROM Table1

This method is often used to add "All" or "None" to listboxes or comboboxes.

Remou
@onedaywhen I tested, you do not need distinct in the version of access that I am using, you must not use UNION ALL or you will get a row for each entry in the table.
Remou
I didn't spot you were using UNION. I thought you were hard-coding the assumption that ID could not be 0 or -1 in the table. So why would you favour UNION over UNION ALL?
onedaywhen
...just I tested using a 100K row table. UNION took 22 seconds, UNION ALL took 3 seconds.
onedaywhen
UNION ALL is certainly faster, but when you are creating virtual rows it will create a row for each virtual row. I am not sure what you think I am doing in the example, but what I think I am doing is creating two virtual rows with an ID that will sort at the top, which seems likely to be what the OP wants.
Remou
(That is, it will create as many virtual rows as there are rows in the table) In general, I favour UNION ALL.
Remou
When using UNION ALL you also need to use DISTINCT in the SELECT clause. This will prevent the duplicate rows. Do you now favour UNION ALL in this case? Whether it needs to be sorted is a moot point, in lieu of any explicit request in the question I think it is better to not sort because it is far quicker.
onedaywhen
...and sorting the resulting ADO recordset takes a fraction of a second for the 100K rows ;)
onedaywhen
First: I don't understand how this answer was voted down. It seems an apropriate answer to my question. Yet, How can I bind such a virtual column to a text box on my form?
René Nyffenegger
It seems from more recent posts that you wish to edit the entry. Union queries cannot be updated. I have noted some ideas in your recent posts.
Remou
+1  A: 

ADO allows you to create disconnected recordsets, and an ADO recordset can be assigned as the form's recordset, so that sounds like the way to go.

But I'd caution that I don't use ADO for any functionality that is provided by DAO, and while DAO has nothing like disconnected recordsets (though for some operations you can use a transaction to fake it), I've never needed them myself, so I'm really reporting second-hand information.

However, this is likely a profitable thing for you to investigate, at least insofar as I understand your question.

David-W-Fenton
A: 

While you can do this using UNION:

SELECT 0 AS ID, True AS Virtual, 'None' AS Stuff 
  FROM Table1
UNION 
SELECT ID, False, Stuff 
  FROM Table1;
UNION
SELECT -1, True, 'All'
  FROM Table1;

...its probably better to do this using UNION ALL with the DISTINCT keyword:

SELECT DISTINCT 0 AS ID, True AS Virtual, 'None' AS Stuff 
  FROM Table1
UNION ALL
SELECT ID, False, Stuff 
  FROM Table1;
ORDER BY ID
UNION ALL
SELECT DISTINCT -1, True, 'All'
  FROM Table1;

Why prefer UNION ALL? UNION preforms an implicit sort on the data, which you may not want; if you do use an explicit sort then use one! (As the documentation will tell you, an implicit sort is not guaranteed so don't rely on it.)

In order to performs the sort, only the first 255 characters of MEMO data will be considered and in extremis can lead to whole rows being removed from the resultset :(

The implicit sort for UNION also has a relatively huge impact on performance: I tested using a 100K row table and the UNION approach took 21 seconds, while the UNION ALL took only 3 seconds.

onedaywhen