How to connect tables with SQL command - sql

I have a form like this with 6 table inside. I wanted to connect a table called "Identity" with the other 5 tables which change accordingly to the number shown by combobox so that while the identity shown exactly the same the number of score that be displayed will be different. I got the textbox to show the value of the 5 table but there are no connection between them and the identity table.
What I want is that when the record of identity table is on 1 and the combobox shows 1 then it will show the values from table semester1 and when combobox shows 2 then it will show the values from table semester2 and so on, while on the otherside if we keep the same number in combobox when we move to the next record it will show the next record of identity table and it's connection to the semester table.
Other than that I also want to all the record stay on the same number so if on semester 1 is already had 3 record the other semester must also have that amount, I think it's gonna be some code on the navbar's add button and enable to save.

If you want to set up parent/child data-binding then you need to bind the child BindingSource to the parent BindingSource rather than to the child DataTable. Select the child BindingSource in the designer and open the Properties window. You should see that the DataSource property refers to your DataSet and the DataMember refers to the child DataTable. Instead, set the DataSource to refer to the parent BindingSource and the DataMember to refer to the DataRelation to that child.

Related

Cannot reference Access subform Form object until the first row of parent form has been expanded

When the parent form is opened in datasheet view, I cannot reference the Form object of a subform control until the first row of the parent form has been expanded.
I would like to know a good workaround to this. I need to be able to requery this kind of subform even if the first row of the parent form datasheet has not yet been expanded.
Steps to reproduce the problem:
Create two tables:
CREATE TABLE Parent (
PK Text PRIMARY KEY
);
CREATE TABLE Sub (
PK Text PRIMARY KEY,
FK Text REFERENCES Parent (PK)
);
Insert some data:
INSERT INTO Parent (PK) VALUES ('A');
INSERT INTO Parent (PK) VALUES ('B');
INSERT INTO Sub (PK,FK) VALUES ('AA','A');
INSERT INTO Sub (PK,FK) VALUES ('BB','B');
Create two forms based on these tables and set the default view to Datasheet.
Add Sub form to Parent form and link on sub.FK=parent.PK.
Give the subform container control the name child.
Open the Parent form.
Expand the first row of the datasheet and then collapse it.
Attempt to requery the subform as follows:
Forms!Parent!child.Form.Requery 'This succeeds.
Close and re-open the Parent form.
Expand the second row of the datasheet and then collapse it.
Attempt to requery the subform again using the same code.
This time it fails with the following error:
Run-time error '2455':
You entered an expression that has an invalid reference to the property Form/Report.
From this, I have to conclude that in datasheet view, a subform Form object is not reference-able until the first row of the parent form is expanded.
Not sure if this is correct, but it I think that this is caused by the repeated SubForm control. Each of them is a different Form Object, although they have the same name.
The difference between the first and the second record is the Focus. If you expand the first record, first control of SubForm gets focus. For the second record you have to set focus yourself. If you click on a record of the second records Subform the .Requery works. If you click on first record after this (not expand) your :Requery fails too.
Another thing to try. After expanding first records subform, expand the second one (without setting focus to a control). Now add a record to table Sub with B as FK and then .Requery. You will see that second records subform is not requeried, just the first!
Solution: Requery the control not the form with
Forms!Parent!child.Requery
That affects all records.

VBA Access Force Form Update to show new records added

I have an Access database with two Forms.
Form 1 (Single Form) is designed to browse records from Table 1.
Form 2 (Continuous Form) is designed to browse records from Table 2.
On Form 2, I created an "Import" button, linked to an Event Procedure.
This procedure selects current record from Table 2 and inserts it Into Table 1.
Problem is, the newly inserted record is not shown dynamically on Form 1.
Form 1 needs to be closed and reopened in order for the change to be visible.
Is there a better way to do this "refresh"? From code?

Delete master record and make it available for old entry only, not new one

I am having a problem as described below.
I am developing a form which contains vb.net datagrid with several columns.
Few columns are of comboboxcolumn type which are bound to datasource of SQL Database.
Now when new entry starts, user selects data from different cell of combobox and so on.
Then all rows data from grid is saved to database.
Now if I delete a master record of one table which is the datasource of one column of datagrid,
When I open existing entry list, that particular cell is shown empty because its master data is not available in table.
How do I show deleted master data in existing entry, but not in the new entry?(Obviously it will not be available for new entry)
I would not delete the data... I would have a boolean column for redundancy. Then instead of deleting mark column as redundant = true.
The datagrid I would have with an item and edit template. In the item template would have a label which will be bound to record value. In edit template would have a combobox which would be bound to datasource where redundant = false.

VB.NET Winforms DataGridView Select new row on Sorted DataGridView

Alright I have a DataGridView, where the user can click on the column headers to sort. When they add a new row while a sort is applied, the record isn't created until the moment they validate the row(which they cannot do till they exit the newRow). How can I make the row be selected once it is sorted?
The DataGridView is databound.
The selection mode for the grid is full row.
I'm using VB.NET with SQLite database backend.
I suspect I need to use the RowsAdded event, or DataBindingComplete events. The records in question do have a unique GUID attached but it is NOT visible in the DataGridView.
This c# question seems sort of along the lines of what I want: Select newly added Row - DataGridView and BindingSource. However the question is how do I make it fire the row validate (and thus the binding to DB) without leaving the row.
Store the primary key (id) of the added record into a new field(insert command returns that), and loop through the gridview rows and select the row with the primary key. :)

How can I bind a multi-row update or insert statement to a form?

I have a table Prices:
ID -- primary key, autonumber long integer
PriceDate -- Date
Price - Currency
Quantity - Number, DECIMAL subtype
UnitPrice - Number, DECIMAL subtype (an update statement is run to keep this in synch with price and quantity, but it's just a convenience for indexing... probably it'll be replaced with an expression in my queries)
ItemNote - Text
NewStores_ID - long integer key, lookup to another table of stores
NewItems_ID - long integer key, lookup to another table of items
To enter prices for a given store on a given day, I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet. For reasons unexplained, this proves difficult.
I can create a subform binding everything but the store and price to a temp table TempPrices with the same structure as the original. Then I run the SQL statement
INSERT INTO Prices
(PriceDate,Price,Quantity,UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
SELECT
PriceDate,Price,Quantity,Price/Quantity AS
UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
FROM Temp_Prices;
This will feed all the new rows into the main table. But, when I want to set the store and date only once, I run into problems. I've tried using named parameters for date an store in the insert statement... which can cause a pop-up prompt, but I cannot bind it to a form control. I've tried binding an update statement for those fields in the temp table to a form... but it doesn't even show an option to bind a multi-row update.
How can I get this to work with a minimum of clumsy hackery? It seems like there ought to be a simple solution, and if I were using something like PHP or JDBC I'd just run an extra query.
Edit: changed storage type for Quantity and UnitPrice to Number, Decimal subtype in place of double float. Just so people won't cry about using a float in any proximity to currency. It doesn't pose a problem in my use, but there are enough people who have a knee-jerk reaction to that.
Edit 2: Form/Subform
I'm trying to structure this as a master form with a either fields for entering store name and date, or a subform for the same, then a subform mapping to the temporary table for entering pricing data. There is an action button to run the insert/update queries to dump the temp table into my main prices table and clear out the temp table. However, the problem is that I can't figure out how to get the date/store fields in the master (or subform) to bind to an insert/update value applied to all the new rows at once.
Edit 3: SQL Statements (for clarity)
INSERT INTO
PRICES(NewStores_ID,PriceDate,NewItems_ID,Brand,Price,Quantity,
UnitPrice,ItemNote)
SELECT
#MyStore_ID,#MyPriceDate,NewItems_ID,Brand,Price,Quantity,
Price/Quantity,ItemNote
FROM TempPrices;
UPDATE TempPrices SET PriceDate=#MyPriceDate,NewStores_ID=#MyStoreID;
For these queries, I cannot bind parameters for #MyStore_ID or #MyPriceDate to fields in any form. The queries don't show up as options when trying to link them to a form. I can run them and get popup boxes to input parameters, but that's not what I want.
This is the target I'm aiming at:
"I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet."
However, if you have a reason why you need to do it with a temp table and DML statements, then this suggestion will not be useful.
I created a Prices table with only 4 fields, then a query on that table which I used as the Record Source for a form, "fsubPrices":
SELECT p.ID, p.NewStores_ID, p.PriceDate, p.Price
FROM Prices AS p
ORDER BY p.NewStores_ID, p.PriceDate;
The form has text boxes (txtNewStores_ID, txtPriceDate, and txtPrice) bound to the similarly-named query fields. I set Enabled=Yes for txtPrice, and Enabled=No for the other two.
Then I created an unbound form "frmPrices", and in the form header added a combo box "cboStores" and a text box "txtPriceDate". The combo has Bound Column = 1 with this query for its Row Source:
SELECT l.Store_ID, l.Store_name FROM tblkupStores AS l ORDER BY l.Store_name;
Then I added fsubPrices as a subform control to the detail section of frmPrices. The tricky part is setting the Link Master/Child Fields. There is a "wizardy dialog thing", but it will only allow you to select from the available fields, and I needed to link controls on the main form with fields on the subform. To do that, I had to type what I wanted directly into the subform control's property sheet:
Link Child Fields......NewStores_ID;PriceDate
Link Master Fields.....cboStores;txtPriceDate
The result is ... choose a store and date combination in the main form ... any matching records are displayed in the subform. You can navigate to the "new record" in the subform to add records for that store/date combination. But the txtNewStores_ID and txtPriceDate controls don't show the updated values until the new record is saved.
I hope this is close to what you want. It's actually fairly quick and easy to create; not so easy to describe.
You can't really do that. You are always going to have one row being worked with at a time.
What you can do is simulate it by changing the form's Default View from "Single Form" to "Continuous Form" or perhaps "Data sheet" and making is a child(sub) form of a master form.
Then you can put the store and date on the Master form, and linking to the child form using the NewStores_ID and PriceDate fields.