Use databinding controls to add records to a dataset - vb.net

I imagine this is a terribly Noobish question, and I hate to ask, but I've been trying to solve this problem all day,
I need to add rows to a DataSet using databound controls in VB.net.
I've set up the data bindings themselves, they're bound the the correct controls, and the BindingSource uses the correct DataSet. The DataSet is filled from the DataAdapter correctly, and the binding source works, as the navigation controls all work fine.
Here's the noob part: How do I use the controls to add new data to the DataSet?
I've been grappling with this all day. I've tried Google, this board, other boards, MSDN, everything I could think of, and nowhere did I find a simple tutorial on how to do it. Either I'm as thick as two short planks, or it's not as simple as I assumed it would be.
Could anyone help me with this please? It's driving me mad.
I guess there must be some kind of end-edit involved which would enable me to insert, update and delete records in the DataSet (as you would use with DataGridViews)

You can't add new data to the DataSet directly, you can add rows to the DataTables that reside in your DataSet.
You should have a button "Add item" or something like that, and in the event handler add the row to the data table. Ensure that the user can edit your edited row with your bound control and add a button "Save" which would do a TableAdapter.Update() on changed DataTable.
If you use a DataGridView, setting the property AllowUserToAddRows = true would add an empty, "dirty" row in which user can type data to add new records to the DataTable.
This two should get you started:
Walkthrough: Saving Data to a Database (Single Table)
Editing Data in Datasets

Related

Can Access ensure a new form record displays all fields?

I have a database where I don't want some fields showing depending on data in other fields. I'm still new to VBA, having learnt how to do things I need via the internet (there's not much call for it in my job so like to try it out on side projects) for the things I need and have managed to create some code that hides certain fields that aren't needed, depending on what's been entered in another field and that works okay, if not perfectly (I'd like it to only work on the current record and not all of them at once but will worry about that later). My problem is, if I'm entering information onto a record and any of those fields become invisible exactly as I would want them to, then if I have more records to complete and load a new record, those hidden fields are also hidden on the blank record before any data has been entered and I want each new record to show all fields from the outset.
Another thing I've noticed is that if I close the database, next time I go into it the hidden fields have unhidden themselves again so I know I'm missing something important.
Here's a screenshot of a bit of the code where I want 2 other fields (What_reason and Date_sent_to_new_owning_School) to be visible depending on whether the answer in the current field after update is "Standard" or "Non-standard":
I'm sorry if this is really entry-level stuff but I AM entry level and trying to learn. This bit does work, albeit not perfectly as I'd like it to only work on the record I'm in at the time, and not go through and hide that field in all the other records at once (which it's doing).
I've searched everywhere but can't find the answer and although I've tried, I'm nowhere near good enough at VBA to try and use common sense to work it out. Is this something that can be done? I'm okay with computers generally and with Access too but I'm aware there's an awful lot I don't know and this is why I'm trying to do new things and learn stuff that I've not used before. I have tried all day to get this to work but am admitting defeat and am hoping somebody here will be able to help me. I'll probably need 'idiot level' advice if that's possible, I know my limitations. :)
Do you know how to use the Event tab in the Property Sheet? You can set all of your fields to [field].Visible = True on either: On Current, On Load, or On Open
Screenshot of the Property Sheet and for the field that determines the visibility of all of the other fields; you can use the Event: After Update so that way when you click/tab away from that field, it'll make those changes for you!
Property setting affects ALL instances of control. Control will be visible/not visible for all records depending on conditions of current record. Therefore, dynamically hiding controls on form set in Continuous or Datasheet will NOT give the desired result of
only work on the current record and not all of them at once
Db is not going to 'remember' dynamic setting - code needs to be executed when form opens and/or navigating records - so it is needed in OnCurrent event as well as control's AfterUpdate.
Conditional Formatting can dynamically enable/disable textbox/combobox by record although control is still visible.

vb.net datagridview and bindingsource

Im in the process over converting some legacy VB6 code over, in particular a TDBGrid linked to an ADODC data control.
Everything is going ok, ive got my columns and bindignsource reading sweet as a nut and performing what its supposed to do, correctly - but im having a problem converting this type of method over.
In the vb6 app, while a user scrolls through the grid, the grid fetchstyle (similar to cellformatting) go looking at the equivalent row of data in the adodc.
In .net, I cant seem to get that functionality to be the same, unless I add that field from the database, into a column in the grid and make it invisible (which I dont really want to be doing if i can help it) then read the cell via cellformatting and then perform some action, such as changing the cellstyle backcolor to something.
Is it possible to refer to a row in the bindingsource that would be the same row in the grid that a user is at, without having that field from the bindingsource - in a DGV column?
or do i just have to put up with placing more columns in the DGV than I want and just live with it?
Thanks!
I think you want the BindingSource.Current
object.

Ms Access 2007 ComboBox

I am using Access for a quick and dirty (ADP) interface for an SQL (Express 2012) database so data entry can begin before the MVC web app interface is complete.
There is one field I want to be varchar, I would like this field to either allow the user to type in a value or select from a distinct list of values previously used in that field.
I have that part down, but the problem is when it happens, I have to refresh the recordset to see the new item in the list, so if they choose add a new record, then the last item added is not visible in the list.
So I can get the distinct list, populate the box, allow for new entry, and save that to the DB, do I have to write a code behind to repopulate the recordset, do I need to write a code behind to maintain the list paralleled to the recordset, or is there just a property I am missing?
Thanks
(Added screen showing event)
As suggested, using the on current event on the form and the after update solved the problem.
Clicking the form section detail selected the detail sections property page not the form. Selecting the form from the drop down on the property page displayed the events I was suggested to use.
Many thanks to those contributing.
As the first suggestion of this was from Remau, with assist in locating that event from hansup, I will mark remau's post as answer. Thank you to both.
Don't requery the form, just requery the combo. The best bet is probably the current event which will work if more than one person is doing data entry. It will also work if people are editing the table as well as entering data. Events that only fire when a record is added will not pick up changes to the combo contents.
Private Sub Form_Current()
Me.MyCombo.Requery
End Sub

Using MS access Database as data source with DataGridView Cannot Seem to Update or Refresh DataGridView using conventional methods

I have searched thoroughly for an answer to my question concerning a direct way to update a dataviewgrid with the call of a subroutine. but i had to luck, so im going to post the question here, with a little bit of background code and detail to help further your understanding on whats going on.
So i have 2 forms currently one being frmMain, this form contains three buttons
btnAddItem - brings up frmAdd
btnSearch - no code yet
BtnPrint - no code yet
frm main also has a dataviewgrid - currently named dvgPEBGrid
i have this bound to a access database with the datasource named PEBTableBindingSource
now on to frmAdd
i have some fields that eventually get updated to the database
i successfully do this but after the form closes i cannot get the data grid to update
i have tried a lot of things and what usually happens is my data grid just clears all data and i can not get the grid to return data until i close program and reset. any suggestions would be greatly appreciated.

TableAdapter Update fail

I have a VB windows Form project that connects to several DataTables in an Access DB. On my form I have several textboxes, comboboxes and datetimepickers that are bound to those sources. With one table in particular when I try to update the tableadapter after editing one of the textboxes the update method fails. No exceptions are thrown, but the update method returns a zero value and the data is not updated. The update will work when just the comboboxes or datetimepickers are edited, but as soon as a textbox is edited also, update will not work.
Other DataTables within the project are set up in the same manner but are not having update issues. I can't figure out what about this one particular table is causing an issue. Can anyone give me any thoughts on where or what to troubleshoot?
Thanks!
try adding another textbox or other control pointed to the same source and see if you can get that one to update. Could be something funky with the control's settings that you're missing, and if you start over, it would be fine.