473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Update Button in a Form

Hi,
I used the form wizard to create a bound form on Access. On the bound form, there are about 20 text boxes. One of the boxes is the parent key of the linked table. I'd like the form to do 3 things:

1. Be able to add a new record if the parent key number does not exist when a person type in a parent key and corresponding information in the other text boxes.
2. Have information propagate automatically in the text boxes after I enter the parent key if there are records for the key
3. Update the information on a record if the parent key exists and I add new or change information in the other text boxes

I have been able to do #1 with a simple command key. But I have not been able to figure out how to do #2 or #3. I've tried several codes I found to have the Add command button to also update, but after applying them, I always get the error that "The changes...would create duplicate records in the table." Here's the code I have for my current Add button:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub command94_Click()
  4. On Error GoTo Err_sSave_Click
  5.  
  6.  
  7.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  8.      DoCmd.Close
  9.  
  10. Exit_sSave_Click:
  11.     Exit Sub
  12.  
  13. Err_sSave_Click:
  14.     MsgBox Err.Description
  15.     Resume Exit_sSave_Click
  16.  
  17. End Sub
Thanks for any help!
Jul 30 '10 #1
15 10577
patjones
931 Expert 512MB
What I would suggest is placing an unbound text box on your form, which is what you'll type the search parameter into. Call it txtSearchID. In the After Update event for this box, you can do something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim rst As DAO.Recordset
  3.  
  4. strSQL = "SELECT * FROM [table name] WHERE [key value] = " & Me.txtSearchID 
  5. Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
  6.  
  7. Me.AllowAdditions = True
  8.  
  9. If rst.EOF Then
  10.     MsgBox "No matches!", vbInformation + vbOKOnly, "ID Search"
  11.     Me.DataEntry = True
  12. Else
  13.     Me.DataEntry = False
  14.     Me.RecordSource = strSQL
  15.     Me.AllowEdits = True
  16. End If
  17.  
  18. rst.Close
  19. Set rst = Nothing
  20.  
  21. End Sub

Here you would put [key value] = whatever the name of your primary key column is. I have assumed that this is a number. If it is a string you will need to enclose the search parameter in ' ' like this:

Expand|Select|Wrap|Line Numbers
  1. WHERE [key value] = '" & Me.txtSearchID & "'"

What this code does is opens a recordset containing only records with the ID you specified. It then checks if there are any matches (by checking rst.EOF). If there are no matches, the form clears for data entry of a new record. If there are matches, the form's recordsource is set to the records containing those matches, and you will be able to navigate through, and edit, those records.

Pat
Jul 30 '10 #2
How would you write the after event line? I am not very familiar with the syntax of VBA coding.
Jul 30 '10 #3
patjones
931 Expert 512MB
When you have the form open in design view and go into the property sheet for the text box, there will be an events tab. If you click on the After Update event for the box, select [Event Procedure] and click "...", it will create the event subroutine for you in the VBA window. That's where you'll want to put your code.

Notice that Access is significantly event-driven. This is generally how you work with a control's events.

Pat
Jul 30 '10 #4
I typed in the code for this in after update, then entered a value. I got the error, "complie error. User-defined type not defined." After I click ok, it takes me to the VBA coding page and the term, "rst As DAO.Recordset" is highlighted.
Jul 30 '10 #5
patjones
931 Expert 512MB
Do you have the reference to the "Microsoft Office 12.0 Access database engine Object Library" reference set? In the VBA window go Tools > References...

If you're using Access 2003 or earlier it would be "Microsoft DAO 3.6 Object Library".

Pat
Jul 30 '10 #6
Got everything to work so I deleted all the entries in the table before I put it into operation. But now when I put in a number in the textSearchID field and then click on my Add button, it doesn't save the number as the primary ID; instead the primary number is automatically defaulted to zero. What's more, afterwards when I try to add or update another entry; I get the message "requested changes were not successful because they would create duplicate values in the index, primary key, or relationship." Any idea why the form stopped working after I cleared the table?
Aug 2 '10 #7
patjones
931 Expert 512MB
What code do you have set for your Add button?

Is the ID column an autonumber? It sounds like it might be, in which case you would want to make it just a number so that you can assign the ID as you wish.

In the case where your record is new (i.e. the search value is not found), you need to set the bound ID text box to the value that you tried to search for. This way, even if you don't enter any other data for that record, the record will still be established with an ID.

Pat
Aug 2 '10 #8
ID Column is a regular number; I think it might have something to do with the code in my "Add button." Here's the current VBA code for it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub command94_Click()
  2. On Error GoTo Err_sSave_Click
  3.  
  4. If Me.Dirty Then RunCommand acCmdSaveRecord
  5. Me.[txtSearchID].SetFocus
  6. RunCommand acCmdFind
  7.  
  8.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  9.      DoCmd.Close
  10.  
  11. Exit_sSave_Click:
  12.     Exit Sub
  13.  
  14. Err_sSave_Click:
  15.     MsgBox Err.Description
  16.     Resume Exit_sSave_Click
  17.  
  18. End Sub
I thought this was the exact code I had before it stopped working; other than this code, I just have the textsearchID_Afterupdate code that you provided before
Aug 2 '10 #9
patjones
931 Expert 512MB
I think the code in your Add button is the culprit. Usually you don't need to do anything explicit to save a record in a bound form, because when you make a change to a field in a bound form, the change happens in the corresponding column of the table immediately.

You can see this by entering a new record and then using Access' built-in navigation bar at the bottom of the form to move to a new record. The fields in the form should clear, and when you go to the table and refresh it the record you just entered should be there.

So really there is no technical need for a custom Add button; however the users of your database might expect that functionality. To this end you could program the button to just move to a new record, and if you don't want them to use the built-in navigation buttons you can remove those in form design view.

Pat
Aug 2 '10 #10
I think the problem is because my "txtSearchId" is unabound. So when I hit the Add button, all other fields try to update to the table, but they are unable to because there is no field representing the primary key, "Numbers," on the main form and thus the form thinks the primary key is "0" when in reality it is what is supposed to be in the "txtsearchID" field. I just need a code that updates the primary key "Numbers" column in the table with whatever is in the "txtsearchID" field. This form was working properly but for some reason this VBA code had a disconnect and the form stopped running properly.
Aug 2 '10 #11
patjones
931 Expert 512MB
As I mentioned earlier today in post #8, when you enter your search ID into txtSearchID, and it is not found, you need to the set your bound ID field to whatever value is in the unbound search field. This could be done in the first branch of the If statement that I suggested. For instance:

Expand|Select|Wrap|Line Numbers
  1. If rst.EOF Then
  2.     MsgBox "No matches for that product ID.", vbInformation + vbOKOnly, "Product ID Search"
  3.     Me.DataEntry = True
  4.  
  5.     Me.[name of bound ID box] = Me.txtSearchID
  6.     Me.[name of first data entry box].SetFocus
  7.  
  8. Else
  9.  
  10.     ...
  11.  
  12. End If

In this manner, even if you enter nothing else for that record, the record will be established with the ID and you would at least be able to search for it later on and do further data entry on it.

Pat
Aug 2 '10 #12
Got it to work. I had deleted the bounded ID box and that was why the primary key was always 0. Can you take a look at my code in post #9 and tell me how to get stop the form from automatically closing every time I hit the Add button? I had inserted that function when I thought I wanted the database to be in a different format. I would instead like the form to display a pop up message every time it successfully adds or updates.
Aug 3 '10 #13
patjones
931 Expert 512MB
Well you have the line DoCmd.Close in the button's subroutine, so I would say that is what's causing the form to close.

If you want a message box to pop up, I suggest researching the MsgBox function, and consider using it in the button's code.

Honestly, I'm a little confused by most of the code that you have in the button's click event. What is the purpose of the following two lines?

Expand|Select|Wrap|Line Numbers
  1. Me.[txtSearchID].SetFocus
  2. RunCommand acCmdFind

Pat
Aug 3 '10 #14
Took out the docmd.close command. Now everytime I hit the Update button, a find and replace window comes up insetad of the form saving the data and closing itself.
Aug 4 '10 #15
patjones
931 Expert 512MB
To be truthful, you need to research your code a little and understand what it is actually doing. And if you want anyone on this forum to help you, you need to respond to the questions that are asked.

For instance, in my last post I inquired about two particular lines of code, but you seem to have ignored that question. If you put a little research into the code that you're using, I think you'll find the answer as to why the Find and Replace dialog is popping up.

Pat
Aug 4 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mr. Smith | last post by:
Hi. I have a form with three <textarea> and one <input> element. When either of these are changed I want an "Update" button to be visible at the bottom of the form. I've tried this: On every...
0
by: Dave | last post by:
Hi, I'm creating a form based on a table. This table has fields including UniqueID (auto numbered and different for each entry), Track (3 different types), ClassYear (4 different years), and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
1
by: mpmason14 | last post by:
i want to update my "timesheet" form based upon a second form "add employee" whenever a new employee is added. right now the "add employee" form adds the employee to the table, but it doesn't update...
4
by: J055 | last post by:
Hi I have 2 update buttons in my FormView ('Apply' and 'OK'). I want both buttons to update the data source but the 'OK' button should redirect afterwards. I can see which button is clicked...
0
by: ayneekeaw | last post by:
When I click the update button/link at the Gridview to update the data from drop down list into the SQL. The selected value of drop down list change to default value. How can I fix this problem
0
by: mesut | last post by:
Hi there, I've a question. I would like to create a global update button to update all changed records gridview. e.g. A gridview contains 5 columns an 2 of the columns can be updated by the...
1
by: schuiazza | last post by:
Hi, I am updating a webpage which displays information about electrical networks. The company requires to update gridstation information regarding voltage based on each are i..e south east etc,...
1
by: geeteshss | last post by:
Dear all, actually i spent a whole month on the R&D of datagrid edit ,update,cancel events but recently my guide told me to make it user friendly because no user would like to go on searching rows...
0
by: stimul8d | last post by:
Before i get flamed, this isn't the usual question you see left right and center. I'm dynamically creating usercontrols inside the page_init event and setting the ID's of each control so that the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.