Dynamically refer to columns - Microsoft Access 2016

Mading

New Member
Joined
Feb 10, 2016
Messages
7
Hi

Just wondering, as the title suggests how to write a query that - based on what is entered on a form - dynamically refers to a column heading in a table. So:

I have a table called "Required training":

TrainingTeacherAdminSenior Management
Safeguarding111
First Aid100
Fire marshall010

<tbody>
</tbody>

I have a form where I want the end user to be able to select "teacher" (for example), run a report which will be linked to the query, and see a report of all the training that a teacher is required to do. I know how to link the report to the query from within the form, my problem is I don't know how to make the query dynamic so that whatever is selected from the drop-down box on the form will be returned by the query i.e.

Select [column name from form drop-down]
From tblRequiredTraining
Where [colum name from form drop-down] = 1

Any help would be appreciated, let me know if I can do anything to make it clearer

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Sorry, the query I would need is:

Select Training
From tblRequiredTraining
Where [colum name from form drop-down] = 1
 
Upvote 0
Upvote 0
I could be wrong, but I don't think that answers the question.
First, your query example is missing the field from the table you want to compare the column value to; i.e. [column name from form drop-down] has to be a field reference, not a "value from some type of control" kind of reference.
My take is that your query sql statement would be like
Code:
[COLOR=#333333]Select [Training] [/COLOR][COLOR=#333333]From [tblRequiredTraining] [/COLOR][COLOR=#333333]Where [Teacher] = Forms!frmNameOfYourForm.[/COLOR][COLOR=#ff0000]NameOfYourControl[/COLOR]
The red part represents your example of 1. This assumes that you want to run this query AFTER a combo selection is chosen, and use the chosen value. It also assumes the value you're getting from the control is a number and not text. If text, the expression must wrap the value in quotes. You will need to know how to concatenate a WHERE clause for such situations.

BTW,You're not referring to a column heading; you're referring to a control. If the control is a combo box with one column, you don't need to worry about columns. If it has more than one column, the columns collection is zero based, so the second column is 1, not 2. Then you'd need .NameOfYourControl.Column(n) where n represents the zero based column number.

By the time you see this, I will probably have embarked on a 3 day road trip and will be somewhat limited in my ability to respond in a timely manner, but I'll try to keep up.
 
Last edited:
Upvote 0
I could be wrong, but I don't think that answers the question.
I think you and I have different interpretations of the question.

It appears to me that the variable part if which field they are using in the criteria, not the value of a certain pre-determined field.
As such, I do not believe something like this would work:
Select [Training] From [tblRequiredTraining] Where [Teacher] = Forms!frmNameOfYourForm.NameOfYourControl

The point of the link in my post was to show how you can dynamically build SQL code on-the-fly. In that example, it does not show one where which field being used in the criteria is dynamic, but it shows you how you can build the code manually, for whatever part is dynamic/variable. You would just have the drop-down box on the form list the different table fields you might want to include in your query, and then build the SQL code based off of that selection.
 
Upvote 0
you are probably correct in your interpretation. I guess time will tell, assuming this doesn't get solved somewhere else.
 
Upvote 0
Hi Joe and Micron

Many, many thanks for your input - actually I found the solution by combining both of your answers! This worked:

Code:
Private Sub cmdSearchButton_Click()
'
    Dim mySQL As String
    
'   Build SQL code
    mySQL = "SELECT qryCourseTitles.[Course Title], qryCourseTitles.[" & Me.txtSearchValue & "]  FROM qryCourseTitles WHERE" & _
            " qryCourseTitles.[" & Me.txtSearchValue & "] = " & Chr(49) & ";"
            
'   Assign SQL code to pre-assigned query
    CurrentDb.QueryDefs("QuerySearch").SQL = mySQL
    
'   Open query with results
    DoCmd.OpenQuery "QuerySearch"
    
End Sub
Once again, thank you both
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top