Edit Rows - Prototype - sql

I need help on both the visual and the programmatic end of this one. Ok, so I'm allowing the user to edit certain rows pertaining to his unique ID. When the user clicks edit, it shows all of the rows in the user table that contain his specific ID (see below for the table layout). Furthermore, a checkbox is generated for each class the user appears in (again based on the user_id field).
tbl_user
user_id | user_name
1 | Evan
tbl_class
user_id | class_id
1 1
1 2
This is what the edit form currently looks like:
| TEXT-BOX | For user_name
| CHECKBOX value="1" | For EACH class found
| CHECKBOX value="2" | For EACH class found
When the form is submitted, I was going to check to see if the values of any of the boxes were unchecked; if they were, I would remove the corresponding tbl_class row (wherever the class_id matched up).
Two problems I've encountered using this method:
If the checkbox is unchecked, its value does not come through the form (so it seems)
This isn't so much a problem as it is an annoyance- is there a better way to go about doing this? I really hate the design.
I would appreciate help with either of the above questions.
Thank you!
Evan

http://postimage.org/image/e1sny3dcx/
I have uploaded an image at above link, of what i think a more user friendly design.
On Edit user details page, user can edit his user name through text box.
Also 'Available Class ids' are shown in list box with multiple select option and 'Selected class Ids to Remove' will contain the Ids user wishes to remove. The button '>>' this in between transfers the selected class id from 'Available class id' list box to 'Selected Class Ids to remove' list box , while button '<<' does the vice verse.
It also give you flexibility if you tomorrow wants to identify the new class ids to add.
you can simply add on some event to the Available Class Ids List.
The design gives you advantage to clearly identify through form elements which are the class id's you want to remove.I would recommend this approach in case there a more class_id's pertaining to a user_id.

When the user unchecks one of the boxes, you could use JavaScript to create a hidden input in the form with a name of something like "classRemoved" and a value of the class id that was unchecked. That way, when the form is submitted, you have a list of all of the unchecked classes.
Instead of the check box method for removing classes, maybe a more elegant and modern way of doing it would be to have each class on its own row with a red "X" button next to it. If the "X" button is clicked, the user is warned that the class will be removed and upon confirmation, an AJAX call is made to your database to remove the class from the table and then the HTML row containing the class is hidden; no need to refresh the page.

Related

Filter data in datasheet based on second datasheet

I have a form with two subforms in it in the following format:
on Subform1, Element1 has Data1. Value of Data1 points to Data1 on Subform2 which has a value of Value1
Right now both subforms are showing all data in each table. What I want to do is filter Subform2 based on the row selected in Subform1.
In this example, Element3 is selected, so the pair Data3 Value3 shows in Subform2.
I've tried accomplishing this by altering the SQL on Subform2, but nothing I do seems to do the trick. I don't know if I'm looking in the right place, or if I should look somewhere else.
If there's anything else I should provide, please don't hesitate to point it out. I want to provide enough information to come to a solution.
You can do that by changing the recordsource of Subform2 on the OnCurrent event of subform1. The steps to do that are as follows:-
Open the form in design view
Go to the properties of subform1
Go to the event tab
Select eventprocedure from the oncurrent combo
double click on the button next to the event to go to the vba window
insert the following code
Private Sub Form_Current()
Me.Parent.Subform2.Form.RecordSource = "Select data,value From TableName Where data=" & Me.Data
End Sub
Open your query for subform2 in design view. Then set the criteria to =forms![mainform]![subform1].form![element].
then in VBA you need to requery subform2 when the selected record in subform 1 is changed. Go in to the on current event of subform1 and use the following:
private sub Form_Current()
forms![mainform]![subform2].requery
end sub
N.B. you may need to change the name of mainform, subform and the column name which I called element.
Results and explanation using methods in other answers
Okay, so I ended up coming to a solution for my specific problem, and hopefully by sharing here, it will help others in the future.
The other solutions to this question assume that your form hierarchy is as follows:
MainForm->Subform1->Subform2
As in, the second subform is in and owned by the first form. This will work for most applications, but not when both Subform1 and Subform2 are Datasheets.
The hierarchy in my case, and the hierarchy for the people I hope to help in the future is as follows:
MainForm->Subform1
MainForm->Subform2
As in, the second Subform is NOT owned by the first Subform.
With this hierarchy, the code in the other solutions doesn't work, unfortunately. However, there is a simple workaround:
Use hidden textbox control as a "link" between Subform1 and Subform2
(The following method uses the example names found in my original question)
From design view, create a Text Box in the MainForm, not in the Subform1 or Subform2.
On the Property Sheet for the newly created Text Box control, under the Data tab under Property "Control Source", put the following code:
=[Subform1].[Form]![Element1]
Obviously, replace Subform1 with your first subform, and typically Element1 will be Primary Key for that table.
Next, on the Property Sheet for the Text Box control, under the Format tab change Property "Visible" to No.
Next, we're going to change the "Link Master Fields" and "Link Child Fields" properties on Subform2, which can be found on the Data tab of the Property Sheet:
For the "Link Master Fields" property, put in the Name of the Text Box control you made. An example of a default name will be Text5. I renamed my Text Box to CurrentElementKey, but name it whatever you want. So in my "Link Master Fields" property, I put CurrentElementKey.
For the "Link Child Fields" property, since we put the Primary Key for Subform1 in the Text Box, we need to put the Foreign Key it relates to in Subform2. I can't tell you exactly what this will look like, because it will vary for your scenario. For example, you might have had Element1 be Element1PK on the first Subform, and it's Foreign Key on Subform2 as Element1FK. So you'd put Element1FK in "Link Child Fields".
If you have any questions, or require further explanation, please comment on this answer, and I'll do my best to help.

How to generate adidional fields inside Yii form

I have:
Table "user"
id, username, password
Table "freedate"
id, user_id, start_date, end_date
Each user can have 1 or more (unlimited) number of "freedate" entries.
Now i have form with 2 text fields (username and password), and 2 date pickers (one for start date and another one for end date).
How can i enable user to inserd aditional datepicker pairs so they can enter as much of "freedate" entires as they need to?
I was wondering about insertind aditional button inside form, that would somehow create aditional fields when pressed, but u have no idea how to do it.
I don't need working example (even tho one would help ofc). What i need i suggestion from your own expirience if you had similar problem.
You can use javascript as noted above. Though that can get tricky to get the new fields associated with the datepicker.
You can also submit after each pair is entered. On returning back to form after save insert a new set of start/end date fields to the end of the form. This way they always have a freedate pair they can enter. (a bit slower overall)
You need javascript to generate these fields on the fly. Or a fixed amount of extra fields could be hidden and shown one by one using javascript as the user clicks the button. You would need to keep track of the number of fields being shown in a javascript var.
You need to write custom javascript in order to do that. It isn't hard, you would need to do something along these lines:
You need to create a button and, when that button gets clicked (onClick event or jquery click() function) you can add the html for your field (<input type=.... /> and so on to your form) and remember to use the correct name for these fields so that they get sent correctly when the user submits the form.

Want to change foreColor of some of the items in a ListBox

I am developing an attendance count system, where all the registered student in a class are shown in a listbox. But I want to show name of the students who are currently present in a may be GREEN color, and all other students in RED color by default. I can get the index of a each items but controlling only some of the items seems difficult to me.
say list is looking this way,
Ataur
Warda
Bappy
Devid
Lina
now only ataur and bappy are present, [ I received a string data from my hardware that confirms that they are in present ] from the database I can get the their id and from the listBox i can get the index as well. Now how do I change the forColor of only these two not others?
Pardon me if my question seems silly, I am very new at VB.net
You cannot do that by default with windows forms.
You have to draw your own list box item.
http://www.codeguru.com/forum/archive/index.php/t-301047.html

Gray out a form row's (detail's) button conditionally with VBA code

I have a standard form in MS-Access which lists a bunch of orders, and each row contains order no, customer, etc fields + a button to view notes and attached document files.
On request from our customer we should gray out the button btnAnm (or check or uncheck a checkbox) depending on a calculation from two queries to two other tables (a SELECT COUNT WHERE and a check if a text field is empty).
I've tried btnAnm_BeforeUpdate(...) and btnAnm_BeforeRender(...) and put breakpoints in the subs, but none of them trigger. The same if I use the control Ordernr instead of btnAnm.
I'd like a function in the Detail VBA code to be triggered for each "Me." (row) so to speak, and set the row's control's properties in that sub.
What do I do? I've looked at the help file and searched here.
*Edit: So I want to do something that "isn't made to work that way"? Ie. events are not triggered in Details.
As an alternative, could I base the value of a checkbox on each line on a query based on the 'Ordernr' field of the current row and the result of a SELECT COUNT from another table and empty field check?
Do I do this in the query the list is based on, or can I bind the extra checkbox field to a query?
A description of how to do this (combine a COUNT and a WHERE "not empty" to yes/no checkbox value) would be perfectly acceptable, I think! :)*
You cannot do much with an unbound control in a continuous form, anything you do will only apply to the current record. You can use a bound control with a click event so that it acts like a button.
Presumably the related documents have a reference to the order number that appears on your form, which means that you can create a control, let us call it CountOrders, with a ControlSource like so:
=DCount("OrderID","QueryName","OrderID=" & [OrderID])
The control can be hidden, or you can set it up to return true or False for use with a textbox, you can also use it for Conditional Formatting, but sadly, not for command buttons.
Expression Is [CountOrders]>0
You can also hide the contents and add a click event so that is acts in place of the command button. Conditional Formatting will allow you to enable or disable a textbox.
As I understand your question, you have a continuous form with as command button that appears on each row - and you'd like to enable/disable the button conditionally depending on the contents of the row.
Unfortunately you can't do that. It seems that you can't reference the individual command buttons separately.
Having wanted to do something similar in the past I came up with two alternate ways of setting up my interface.
Put a trap into the onClick code for the Button. Which is icky, because it is counter intuitive to the user. But it gets you that functionality now.
Move the command button (and editable fields) up into the form header, and make the rows read only. Your users then interact with the record only in the header, and select the record they want work with in the list below. As I recall this is known a Master-Detail interface.

My MS Access control displays no text for appended items following an append query

Relevant Tables
#One#
+----------+ +--------------+
|Quotations| ---> |PurchaseOrders|
+----------+ | +--------------+
<One> | <Many> |
| v #Many#
| +-----------+ +------------+
v |QuotedItems| ---> |OrderedItems|
+-----------+ +------------+
<Many> {One} {Many}
Form/Subform
PurchaseOrders - Master Form, many-to-one with OrderedItems
OrderedItems - Datasheet-style subform, many-to-one with PurchaseOrders
Quotations- A table that is one-to-many with PurchaseOrders and used for querying
QuotedItems - A table that is one-to-many with OrderedItems and used for querying
Combo Box
The Item control resides in the OrderedItems subform. It is a combo box that runs the following ad hoc query:
SELECT [ID], [Code]
FROM [QuotedItems]
WHERE [QuotedItems].[Quotation] = Forms.PurchaseOrders.Quotation;
The combo box query runs the way I expect it to; there are no problems here. The [ID] column is hidden from view (column-width is zero).
Problem
The problem comes when I try to append values from the QuotedItems table (filtered on the current quotation ID selected in the form) at the click of a button.
I created an append query to achieve this called CopyQuotedItems2OrderedItems:
INSERT INTO OrderedItems ( PurchaseOrder, Item, Quantity )
SELECT PurchaseOrders.ID, QuotedItems.Item, QuotedItems.Quantity
FROM ( Quotations
INNER JOIN
PurchaseOrders
ON Quotations.ID = PurchaseOrders.Quotation
)
INNER JOIN
QuotedItems
ON Quotations.ID = QuotedItems.Quotation
WHERE (((Quotations.ID)=[Forms].[PurchaseOrders].[Quotation]));
The Copy Quoted Items button in the PurchaseOrders form then runs the following code:
Private Sub CopyQuotedItems_Click()
DoCmd.OpenQuery "CopyQuotedItems2OrderedItems"
End Sub
The append works as it should. However, the second field of the INSERT statement - which ties in to the Item control does not display anything, even after refreshing. The only way to make the item's Code visible is to select the combo box and choose an item from it.
1000 words...
Before clicking Copy Quoted Items (note that combo box has two entries in this case):
alt text http://img251.imageshack.us/img251/9529/beforeappendwithcombo.png
After Clicking Copy Quoted Items:
alt text http://img651.imageshack.us/img651/8175/afterappendclick.png
Questions
Firstly, why can't I see my Code by default after running the append query? Is it because the value inserted by the append query is not bound to the combo box in some way?
Secondly, am I barking up the wrong tree? If so, how else can I get it to display
the Code column automatically?
I am not sure I get you, but have you tried a Requery rather than a refresh?
<Me or form/subform name>.CodeCombo.Requery
It may be necessary to requery the form or subform, but I do not think so.
EDIT re Further Information
The problem here is that you are using look-up fields in tables. This is an anti-feature and will, IMHO, continue to make your life difficult. Because of this, you are not updating the item field with an item code but an item id: 2 rather than 30105-250G. Furthermore, 30105-250G is a value made up of parts. There are several approaches to fixing this, the easiest is probably to match up the model id again and extract the code field. However, I strongly recommend that you get rid of all look-up fields and work with relational design ideas.
It looks like Access has not changed much from wayyyyyyy back in the 90s. ;}
<>
We used to write procs which "faked it out" to force the control to refresh. I don't have the code any longer; we used the old sendkeys equivalent brute-force.
I'm willing to be wrong, but in your situation that's the only thing we had which reliably worked.