query with variable column

  • Thread starter Thread starter leon
  • Start date Start date
L

leon

I have table on which i want to run a query but one of the columns
that should be selected should be variable. When running the query it
should ask a column name or it should pick up a value from a form.

The query should look something like this :

select account, balance, [variable]
from tbl_accountbalances

Is this possible at all?

I Don't mind if i have to run the query from a form or have to put the
query in VBA.
 
What do you mean that it will be variable? What are the choices? What
logic will be used to select the column?

You will most likely pull all the columns to your query, but use some logic
to determine which item displays on your form.

Give us an example or two.

Rick B


I have table on which i want to run a query but one of the columns
that should be selected should be variable. When running the query it
should ask a column name or it should pick up a value from a form.

The query should look something like this :

select account, balance, [variable]
from tbl_accountbalances

Is this possible at all?

I Don't mind if i have to run the query from a form or have to put the
query in VBA.
 
I have table on which i want to run a query but one of the columns
that should be selected should be variable. When running the query it
should ask a column name or it should pick up a value from a form.

The query should look something like this :

select account, balance, [variable]
from tbl_accountbalances

Is this possible at all?

I Don't mind if i have to run the query from a form or have to put the
query in VBA.

What are the possible values of the variable? It sounds like the root
of the problem may be that your table is not properly normalized: do
you perhaps have a one to many relationship embedded within each
record, storing data (your variable!) in fieldnames?

If you are stuck with this table design, you will indeed have to use a
Form and VBA to actually construct the SQL string in code. That string
can then be used as the recordsource for a Form or Report in order to
display the data. Air code:

Dim strSQL As String
strSQL = "SELECT [Account], [Balance], [" & _
Me!cboField & _
"] FROM tblAccountBalances;"
<set a report's recordsource to this query>

This assumes you have a form with a combo box named cboField which
offers the user a choice of fieldnames.
 
Back
Top