How do I add an auto-populated form record (created from another record) to a table in access? - vba

I am populating a form with information from two other forms to create a new client record. From form 1 I get the CaseID which will tie the new client record back to the Case table. Form 2 lets me select a client from a list and pull information for some fields into the new record. These data populate Form 3 which appears to show a new record (then number at the bottom of the form is 1 more than currently exists in the table) But the ClientID field is blank - this is the unique key for the client records table. I cannot seem to get this field to increment and thus can't get the record to save to the table.
Based on various searches I've tried forcing the record to save but nothing happens... no errors nor new records. I've tried
If me.dirty then
me.dirty = false
end if
and
DoCmd.RunCommand acCmdSaveRecord
I assume I'm simply not truly initiating a new record even though the form suggests it is being created. What is the appropriate way to add the record, including which control I need to use to initiate the action (e.g., attach to the On_click() event of a button). Note that, sometimes the record will be perfectly fine as populated, and sometimes the user may need to edit one or more fields before saving the record. It is also plausible that occasionally a user will decide the record should not be added and thus will need to close without saving.

It turns out that the ClientID field in the test database I was provided is NOT an autonumber field as it will be in the live dB version. So I simply had to look up the max value in the table and add one. I will have to test the autoincrement when I get the true Dev environment set up with the actual database structure duplicated instead of the mockup I have been working on to create the work flow.

Related

vb.net dataview grid won't add record and doesn't update after data is modified independently

I have a dataview grid bound to a datasource at run time. The datasource is filled from an access database via a DataAdapter. The data fills and displays correctly, and updates to existing rows seem to work OK but I have two problems:
When I type something in a new row and then press return or switch to a different row, I want the DataAdapter to add that row then and there to the database so I can retrieve the Autonumber index of the new record from Access and use that to add an associated record in a different table (Entries, a many to many linking table). This isn't happening. In the RowLeave event I have adapter.Update(dsSentences) and then I check for the new row, but the RowCount doesn't reflect its presence even though the newly added data is visible in the grid, and the adapter.Update doesn't seem to have triggered the Insert query that I specified in the DataAdapter. So nothing is added.
(edit: OK, so the new row has not yet been added when this event is fired. Which event should I then use to commit the data and retrieve the Autonumber primary key for my new record? I've tried UserAddedRow but that one fires before you've entered any data into the new row.)
THe second problem is that I need to update the data independently and then have the grid reflect those changes. How do I do that? Is there some call that will force the grid to get the updated data from the DataAdapter via the Dataset? Any help would be much appreciated. I'm almost ready to dtop the whole idea of binding data and do it all through code, Data binfing is supposed to save time but I'm finding it labyrinthine and unpredictable.
FWIW here's the query I'm using to fill the grid:
PARAMETERS nIdCollection Long;
SELECT tblSentences.IdSentence, tblSentences.SentenceText, tblSentences.SentenceParsed, Not IsNull([tblSentences]![SentenceParsed]) AS HasParsed, Entries.IdEntry
FROM tblSentences INNER JOIN Entries ON tblSentences.IdSentence = Entries.IdSentence
WHERE (((Entries.IdCollection)=[nIdCollection]))
ORDER BY Entries.SortValue;
As you can see, it requires a record in Entries. After I've entered a new record in tblSentences, before there are any entries the IdEntry will be null assuming it shows up at all. That's why I need to intercept directly after the Insert, add the record to Entries and requery to keep everything in order. You could do it all in an SQL stored procedure but I have to use Access.
Edit: After a lot of googling I've come to the conclusion that what I'm trying to do = add a record to a table through an additional INSERT query apart from the one handled by the DataAdapter, every time a new row is added - simply can't be done if you are using data binding. I am going to have to delete all my code and start from scratch populating the grid through code (unbound). I think it's the only way to do what I want. I will leave this here as a warning to anyone else not to make my mistake of trying to use Data binding when your data is coming from more than one table. Bad mistake.

My recordset returns only one record even though there are more one record in Microsoft Access

I am creating a form that loads all the information from the Activity_Table into the form. So basically when the user chooses the name of the Company from the Company_Name drop down, and selects the site from the Site_Name combo box, it is supposed to return the values from that site and put them into the form.
Basically one site can appear more than once in the Activity_Table. Yet, whenever the user selects the name of the site, it only returns one record. To make sure of this, I used the .Recordcount method to check how many records that will be retrieved.
So when you check the Activity table, the Site "Accra Mall - 630kva Gen 2 " appears 4 times, yet when you check the recordcount, it only says it appears Once. So because of this, I cannot move either forward or backwards to either the next or previous record.
To make sure that there was nothing wrong with my sql, I run the query in the Query Design and it produced all 4 records of "Accra Mall - 630kva Gen 2".
So there is nothing wrong with the sql statement. Can anyone help me out please?
This is when I choose the name of the site
We can see that it returns only One record here
Yet this is the amount of records it's supposed to produce(This was when I tried the sql code in the query design tab. It returns 4 records, Not 1). So the recordcount is supposed to be 4. Not One.
this is when I tried the sql to in the previous picture.
If I replicated your problem correctly then the answer is to set Data-Entry to no. Check your form properties. Under the data tab there is a data-entry property. set data-entry to no. From microsoft at https://support.microsoft.com/en-us/office/dataentry-property-f4236759-27f6-4fcd-abb0-4aa4acd8fe87 we get an explanation:
You can use the DataEntry property to specify whether a bound form opens to allow data entry only. The Data Entry property doesn't determine whether records can be added; it only determines whether existing records are displayed. Read/write Boolean.

How can I jump a form to a specific record in Access?

I have an Access Form which pulls records from a table. The user will interact with the records, sometimes in chronological order, and sometimes by jumping direct to a specific record.
The table uses a field called ID as the primary key which auto-numbers when a record is created. This is what the team will use to jump directly to a record.
I need to create a textbox where the ID number can be inputted and then a button to carry out some code to jump to the specified record if it exists.
I have attempted to put bits of code together what I have found online but so far have had no such luck in making anything work.
I would be very grateful if anybody could advise me and provide a code sample to make this work! :)
Make use of the RecordSetClone and Bookmark properties.
Use the RecordSetClone property to obtain a copy of the form's recordset that you can manipulate without affecting the "live" recordset used by the form.
Locate the appropriate record in the cloned recordset (perhaps using the FindFirst method), and obtain the Bookmark which uniquely identifies such record in the recordset.
Set the Bookmark property of the form to the Bookmark value obtained from the cloned recordset in order to change the current record displayed by the form to the record found.

Access Query not updating fast enough

Im building a key inventory mgmt system. I've created a query that show's me keys currently not in use by identifying which keys have been returned, aren't lost or have never been rented. I copied this query into the look up field for key_id in my keyActivity table (used to record key sign outs). The issue is that the query does not update to provide available keys until the table keyActivity is closed and opened again
Example: I open keyActivity, indicate that key_id = 5 is lost. When I go to a new record and select the key to sign out, key_id = 5 is presented as being available. It is not until I close the table, open it again, that key = 5 is removed from the list.
Here you can see key 5 is indicated as lost in id 5 but in id 7 when selecting a key, 5 is available when it shouldn't be.
Is there anyway to fix this or set this up to work as intended. I plan on using forms to present all the information. Is there a form solution perhaps?
The suggestion you would be better off with a Form to change table data. It can be easily requery-ed to update the table according to the changes you make and to display the udpated data accordingly. Please also read on the given references for further info.
In terms of data updating and locks in a multi user environment this article could be helpful.
"Access is NOT a database server. It's a desktop database. It has been pushed to the limit to support mutli-user environments, but only in the sense that you can share the "back end" database across a network."
...
...
"Even the record locking is performed by the Front End. All of the front end
database applications share the "lock file" (a file with the same name as
the database file, but with the extension LDB); but that file is simply a
mechanism that the front ends use to determine which front end can make
changes to the database."
....
Here is a difference between requery and refresh:
Me.Requery forces the entire recordset (underlying data) for the form to reload. This means ALL of the records in your current form will reload. Your current position will be lost, so if you're sitting on record 10 of 100, you'll find yourself back on the first record. Me.Requery is essentially the same as closing and reopening the form. Any new records added by other concurrent users will be available. Likewise any records that have been deleted will disappear. Requery essentially "re-runs the query" that pulled the data into the form in the first place. You can also use requery to update the data in a list box or combo box.
Me.Refresh saves the current record that you're working on. It will also retrieve any changes (but not additions or deletions) to any records shown in the current form. Any calculations on the form (unbound fields) are recalculated. Refresh does NOT reload the recordset. You do not lose your position in the form (you stay on the current record). Any new records added by other users will not be shown.
Reference
MS Access - Write to Table Immediately After Changing Value in Form

MS Access FE / SQL BE Need AutoNumber to populate on form after saving

So, I have an Access 2007 FE with an SQL BE. I am using a bound form for both adding a new record, or updating an existing record.
My problem is this: Unlike a local Access table, using SQL tables cause the AutoNumber field ("SeqID") to not be available until after the record is saved. I have a "save" button on the form, which does various validation steps on a new record, then adds a new record to the table upon successful validation. At that point, I'm able to get the AutoNumber for that record in my code, but the problem is that I cannot get the form to update/display it in the SeqID field. Because it's a bound form, I cannot simply update the SeqID.Value. I need to have this field updated because it is used for other things later on, such as audit trails for changed fields, etc.
Any help would be greatly appreciated.
Try something on these lines. It works for me:
Private Sub AText_AfterUpdate()
Me.Dirty = False
Me.Recalc
End Sub
If the experiment works, it is just a matter of fitting the steps into the flow of your code.
When using SharePoint or SQL server, or in fact most server based databases, the autonumber ID is not generated until record save time. With a form + sub form, Access will ALWAYS do a automatic record save when focus moves to the sub form, and thus you don't have a problem attaching child records to these parent forms + records.
In the case of a bound form, the ONLY way to get the autonumber ID is to force the record save. The simple code is this:
If isnull(me!ID) = true then
' we don't have a record primary key yet, save record
Me.Dirty = false
End if
At this point, you can then use the PK autonumber
MsgBox "pk = " & me!id
So, a simple me.Dirty = false will save the record, and will force generation of the ID and code that follows is then free to use + grab the PK autonumber at will.