How to select a column of data from table with WHERE closure and display - yii-extensions

How to select a column of data from table with WHERE closure and list as combo-box in yii?

Have a look at the CHtml methods dropDownList() and activeDropDownList().
You can set the data you insert into the dropdownlist to be the results from an active record query, for example;
echo CHtml::dropDownList('myDropdown', '1', CHtml::listData(myModel::model()->findAllByAttributes(array('myField'=>1)),'id','name'));
The above command would select all the results from the myModel model where myField = 1 (using their 'id' as the value and their 'name' as the displayed text) and insert them into a dropdownlist with name 'myDropdown' and the initial selected value would be 1.
If you're using this in a form, for example created by gii, then you can use the $form to create the dropdown. This actually uses activeDropDownList(), so the syntax is a little different, here's an example:
echo $form->dropDownList($model,'myOtherField', CHtml::listData(myModel::model()->findAllByAttributes(array('myField'=>1)),'id','name'));
the bit that's actually populating the dropdownlist with the data from the active record query is the CHtml::listData() method which is taking a result set from the active record query and formatting it into an array that will be accepted into the $data param in dropDownList() or activeDropDownList().

Related

How to update rows in SQL that contain certain json data?

I am trying to update password values but at the same time preserve all the other values.
For example, the only change in the image below would be "johndoePassword" to "*****".
How do I write a SQL statement that would do this dynamically in several rows?
I have looked into JSON_MODIFY (Transact-SQL) but can't figure out how to apply that to several existing rows and how to get to the child key/value pairs.
SELECT Id, RouteValues
FROM MyDatabase
WHERE RouteValues like '%password%'
Here is what I came up with:
UPDATE dbo.MyDatabase
SET RouteValues=JSON_MODIFY(RouteValues, '$.model.password', '*****')
WHERE ISJSON(RouteValues) > 0 AND RouteValues like '%password%'

Report Builder - 3 Parameters all in one Query. Forward dependencies are not valid

I have an issue with building my report, I have 3 params and all 3 are in a query, they are supposed to get their values from the "drop down menus" before running the reports and are used in a where cluase to get specific stuff from the database. However I can seem to get it to work.
Example Query
Select * from [Table]
Where ID = #ID and DateFrom = #DateFrom and DateTo = #DateTo
order by ID
This is the query, I tried changing the orders of the params but it doesnt work.
Error:
The report parameter 'ID' has a DefaultValue or a ValidValue that depends on the report parameter "ID". Forward dependencies are not valid.
Based on the error you reported...
"Error: The report parameter 'ID' has a DefaultValue or a ValidValue that depends on the report parameter "ID". Forward dependencies are not valid."
I suspect your ID parameter's valid values are taken from a dataset query. The dataset query uses a parameter called #ID . You cannot populate values for a parameter if the parameter you are trying to populate is required by the query.
If you are trying to get a list of available ID's to populate the ID parameter drop down then you need to create a separate dataset for this. The dataset query would be something simple like.
SELECT DISTINCT ID FROM [Table] ORDER BY ID
You can then change your ID parameter's "Available Values" dataset to point to this new dataset.
If this does not help, show you report design (the parameters at least), the parameters properties and the dataset queries for each one. Your issue should be clear once all that is visible.

How do I populate a MS Access Query with a value of a Text Box

I have two properties and each property has it's own table for orders. So I am trying to generate a query to show me all the orders for the property that is select from a text box.
So the query is as follows:
Select * from Orders_Property1
This query works.
However, I now want the query to return the result based on the property selected on a text box. I trying:
Select * from [Forms]![frm_ORDERS]![txt_PROPERTY]
I am getting a Syntax error. Any idea where I am going wrong?
You can't do that.
Fields and tables must be static in Access SQL.

MS Access Query-By-Form Issue

I have a form (fCen1-20) containing two combo boxes. The first combo box is called Lookup Value and the dropdown contains the field Lookup_Value which serves as the primary key for every table in the database. The second combo box is called Category and the dropdown contains the fields Category, Code, and Table.
I would like for the user to select the Lookup Value and Category and for those selections to inform a query which returns the value of the selected Category for the selected Lookup Value. The complicating factor is that each Lookup Value is associated with over 1500 unique categories of information which are each assigned a unique code -- the code serves as the field name.
For your reference, I have pasted my code, along with my rationale, below:
SELECT [Forms]![fCen1-20]![Category 1].Code
' Rationale: Get the value for the Code associated with a given category
FROM [Forms]![fCen1-20]![Category 1].Table
' Rationale: Reference the Table where the selected Category/Code is housed
ON [Forms]![fCen1-20]![Category 1].Table.Lookup_Value = _
[Forms]![fCen1-20].[Lookup Value];
' Rationale: Select only those records in the table
' for which the Lookup_Value field matches the Lookup Value
' selected in the form
When I run this code, I'm given a "Syntax error in FROM clause" error. Any suggestions on how to make this work? Please let me know if you'd like any additional detail or clarification. Thanks!
If you use this in a query, it will probably work assuming the form fCen1-20 is open in Form View.
SELECT [Forms]![fCen1-20]![Category 1]
The value returned will be from the bound column of the currently selected combo box row. The fact that [Category 1] includes 3 columns does not matter. The db engine only sees the column which is "bound". (Check the combo's Bound Column property on the Data tab of the combo's property sheet.) The bound value is the only combo value available in a query.
You can not append a column name to the combo name to retrieve the values from those columns, so these will both fail:
[Forms]![fCen1-20]![Category 1].Code
[Forms]![fCen1-20]![Category 1].Table
That was my explanation for why I believe your approach is not working. However, I don't know what to suggest instead. In general, if you use a table's primary key as a combo's bound value, you can use that bound value with a DLookup expression in a query. As an example, assuming all values are numeric ...
SELECT fld1, fld2, etc
FROM YourTable
WHERE some_field = DLookup(
"lookup_field",
"AnotherTable",
"pkey_field = " & [Forms]![fCen1-20]![Category 1]
);
Unfortunately I don't know whether that suggestion is useful for your situation because I don't clearly understand what you're trying to accomplish.

Help with Query design in MS-Access

CredTypeID is a number the CredType is the type of Credential
I need the query to display the Credential in a drop down list so I can change the credential by selecting a new one.
Currently I have to know the CredTypeID number to change the Credential.
I just want to select it from a drop down list.
Currently to change Betty Smith to an RN I have to type “3” in the CredTypeID. I just want to be able to select “RN” from a drop down list.
Here is the table layout and sql view (from access)
SELECT Lawson_Employees.LawsonID, Lawson_Employees.LastName,
Lawson_Employees.FirstName, Lawson_DeptInfo.DisplayName,
Lawson_Employees.CredTypeID, tblCredTypes.CredType
FROM (Lawson_Employees
INNER JOIN Lawson_DeptInfo
ON Lawson_Employees.AccCode = Lawson_DeptInfo.AccCode)
INNER JOIN tblCredTypes
ON Lawson_Employees.CredTypeID = tblCredTypes.CredTypeID;
This should do the trick, will work in datasheet view and auto-set up the field as the type of dropdown you want if you add the field to any new forms.
Open the Lawson_Employees table in
design view.
Click on the CredType field and at
the bottom of the screen switch to
the "lookup" tab
Change DisplayControl to "Combobox
Change the Rowsource to be the
following query:
SELECT CREDTYPEID,CREDTYPE FROM tblCredTypes ORDER BY CREDTYPE ASC
Set columncount=2
Set Columnwidths to "0;"
Set LimitToList = Yes
Make sure BoundColumn is set to 1
If you have already added the Lawson_Employees.CredTypeID field to a form, delete it and then re-add it to get it to automatically set it up so you can select by the friendly label instead of the id.
If you are entering the data via a form, then you create a drop down list that uses two columns for it's value list (CredTypeID and CredType) and then set the width of the first column to zero. Hey presto, a field that access treats as having a value of CredTypeID, but displays with CredType.
I don't think you can use this trick directly in the query results themselves, though.