Crosstab query table in a form

  • Thread starter Thread starter Paul Gregory
  • Start date Start date
P

Paul Gregory

I would like to use a table generated by a crosstab query in a form. To use
an unbound object is not acceptable as the number of columns created by the
query changes each time it is run and so columns either end up missing from
the form or have no data and thus display an error.

How would I achieve this.

Thanks
 
As long as you can live with a read only form you can do this by dynamically
Hiding/Unhiding textboxes on your form (as a Datasheet) based on the columns
returned by your query as you open/re-query the form.

Create a form with a bunch of text boxes named something clever like text1,
text2, text3,... The text boxes will need attached labels which could be
named label1, label2, label3,... Create as many text boxes and labels as
you think you will *EVER* need. Make the forms DefaultView a Datasheet.

When you need the form open it and set its Record Source to your canned
CrossTab query or a sql statement that you generate on the fly.

In the OnLoad or OnCurrent event of the form you will need to run some code
that looks at your forms' recordset and sets the all of the TextBoxes Source
and the associated Label Captions to the Field names of the reordset. It
will also have to hide any textboxes that are not used. The code might look
like:

Set rst = Me.Form.RecordsetClone
For i = 0 To rst.Fields.Count - 1
If i < 30 Then
Me("Text" & i + 1).ControlSource = rst.Fields(i).Name
Me("Label" & i + 1).Caption = rst.Fields(i).Name
Me("Text" & i + 1).ColumnHidden = False
Me("Text" & i + 1).ColumnWidth = 1500
End If
Next
For i = i + 1 To 30
Me("Text" & i).ColumnHidden = True
Next
Set rst = Nothing

In this case I planed for a max of thirty fields. Done this way the data in
the form is Read Only. As long as you can live with this you are good to
go. If you want to allow the users to update the data then you will have to
do a LOT more work, using at least one temp table (I have typically used two
temp tables one for the data and the other for the meta data). You will be
completely responsible for writing the code that inserts/updates the data in
your normalized tables from any changed data in your form.

Ron W
 
I want the table to be read-only so that's not a problem.

Thanks. I'll give this a go and get back to you.
 
That worked perfectly. Thanks.

One other quick question.

Is it possible to have the text in the cells centred rather than right
aligned?

Thanks again.
 
Sure

Me("Label" & i + 1).TextAlign = 2
Me("Text" & i + 1).TextAlign = 2

Will Center the text in the Label and the TextBox. You could also set the
properties in the PropSheet for all of the text boxes and labels in design
mode too.

Ron W
 
Thanks again.

Paul

Ron Weiner said:
Sure

Me("Label" & i + 1).TextAlign = 2
Me("Text" & i + 1).TextAlign = 2

Will Center the text in the Label and the TextBox. You could also set the
properties in the PropSheet for all of the text boxes and labels in design
mode too.

Ron W

might used will
 
Back
Top