473,378 Members | 1,427 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,378 software developers and data experts.

update records in a table through form

12
I am new to MS Access & I know almost nothing about vba. ... I hv a table name F with fields F1, F2, F3, F4, F5 where 1st three are number fields & last two are text fields. In a form with textboxes: txtF1, txtF2, txtF3, txtF4, txtF5 and command button cmdUpdate, I want to update F5 in table F where F1=txtF1, F2=txtF2, F3=txtF3 and F4=txtF4 conditions staisfy. .. Kindly help with complete code.
Nov 7 '16 #1

✓ answered by Anathil

I think you are doing this the hard way.

As I understands it, you have a table "Allotment" where all information including the remarks are stored.

When you select the records to be updated they are already in the allotment table, but needs the remark you have just written in the txtRm field in the form. Right?

So you have several records with the same Financial Year, Demand No, Major Head, Minor Head and Sub-Head?
I hope somewhere there is a difference between the records otherwise they are just clones...

But I would go to the query builder (you find it on the ribbon between tables and forms).
Then select the Allotment table (right click in the grey area and select show tables) and select the fields you want to do the search on.

That might be: Financial Year, Demand No, Major Head, Minor Head, Sub-Head.

For each choice you have a coloumn but I will only do the first for you ;)

Field: Financial Year
Table: Allotment
Sort:
Show: (Yes)
Criteria:
Or:

In the criteria field you write: [Forms]![NameOfYourForm].[cmbFY]
This will give you exactly what you have written in the field cmdFY.
If you want "and something like it" you write: Like [Forms]![NameOfYourForm].[cmdFY] & "*"

You do the same for all the other fields you have selected.

This is your search query. You click Run on the ribbon to see if you get what you want.

When this is working to you satisfaction you can convert it to an update query (the 6'th option on the ribbon, at least for me).
You get a new row named "update to"
here you write: [Forms]![NameOfYourForm].[txtRm]

To get the button to run the query for you, you write:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdUpdate_Click()
  2. 'Updates Remark field in table Allotment
  3.  
  4. With DoCmd
  5.     .SetWarnings False
  6.     .OpenQuery "the name of your query"
  7.     .SetWarnings True
  8. End With
  9.  
  10. End Sub
You might want a subform to display the records but that is up to you.

And you should be home free ;)
Unless I totally misunderstood you this is the way I would do it.

6 4611
What have you done so far?

I would use the query builder and make an update query.

Then use VBA to make the click action run the query.
Nov 7 '16 #2
nndkol
12
Thank a lot Anathil for your kind response.
I have a table: Allotment where fields are:
Financial Year, Demand No, Major Head, Minor Head, Sub-Head, Remarks
Here except Major Head all are text.
All date are filled up here by lookup from different tables:
Financial Year, Demand No, Major Head, Minor Head, Sub-Head
I have a form with combo boxes to fetch records from the above 5 tables, a textbox-txtRm and a comman button-cmdUpdate
I select records in the above 5 combo boxes and enter in the textbox. Finally I click the Update button.
I need to update records in the field Remarks(as per textbox txtRm in the form)
in the table: Allotment where records perfectly matched with entries in the 5 combo boxes in the form.
I have made the vb code as follows:


Private Sub cmdUpdate_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("Allotment")
rs.MoveFirst
Do Until rs.EOF

Dim s As String
s = "UPDATE Allotment SET Remarks ='" & Me.txtRm & "' WHERE [FinancialYear] = " & Me.cmbFY And [Demand No] = " & Me.cmbDmnd" And [Major Head] = " & Me.cmbMj And [Minor Head] = " & Me.cmbMn And [Sub-Head] = " & Me.cmbSb"
CurrentDb.Execute s, dbFailOnError
DoCmd.RunCommand acCmdSaveRecord

rs.MoveNext
Loop
MsgBox "Records Updated!"

rs.Close
Set rs = Nothing
Set db = Nothing

End Sub

When I Debug and compile test nothing shows wrong, but when I click Update button, it shows runtime error 2465,
Can't find the field 'I' referred to in your expression. When the I click Debug, Update ... line marked yellow.

The above I have copied from some forum and changed as per my requirement.
I know nothing about vba. I think the update statement is wrong.
Kindly help me.
Nov 9 '16 #3
MikeTheBike
639 Expert 512MB
Hi

You seem to br missing a number of quotation marks and ampersands

I think it should look like this (assuming only one text field)
Expand|Select|Wrap|Line Numbers
  1.     s = "UPDATE Allotment SET Remarks ='" & Me.txtRm & "' "
  2.     s = s & "WHERE [FinancialYear] = " & Me.cmbFY & " "
  3.     s = s & "And [Demand No] = " & Me.cmbDmnd & " "
  4.     s = s & "And [Major Head] = " & Me.cmbMj & " "
  5.     s = s & "And [Minor Head] = " & Me.cmbMn & " "
  6.     s = s & "And [Sub-Head] = " & Me.cmbSb
Also, you looping through a record set, but do not seem use it !!?

So you are going to update the same record with the same information a number of times !!

I'm also not sure why you are using this
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSaveRecord
unless you are updating the form record, which would only need doing once.

It has no bearing on the update query, which will either succeed or fail independently of the form.

HTH

I think that is a start at least.

MTB
Nov 9 '16 #4
I think you are doing this the hard way.

As I understands it, you have a table "Allotment" where all information including the remarks are stored.

When you select the records to be updated they are already in the allotment table, but needs the remark you have just written in the txtRm field in the form. Right?

So you have several records with the same Financial Year, Demand No, Major Head, Minor Head and Sub-Head?
I hope somewhere there is a difference between the records otherwise they are just clones...

But I would go to the query builder (you find it on the ribbon between tables and forms).
Then select the Allotment table (right click in the grey area and select show tables) and select the fields you want to do the search on.

That might be: Financial Year, Demand No, Major Head, Minor Head, Sub-Head.

For each choice you have a coloumn but I will only do the first for you ;)

Field: Financial Year
Table: Allotment
Sort:
Show: (Yes)
Criteria:
Or:

In the criteria field you write: [Forms]![NameOfYourForm].[cmbFY]
This will give you exactly what you have written in the field cmdFY.
If you want "and something like it" you write: Like [Forms]![NameOfYourForm].[cmdFY] & "*"

You do the same for all the other fields you have selected.

This is your search query. You click Run on the ribbon to see if you get what you want.

When this is working to you satisfaction you can convert it to an update query (the 6'th option on the ribbon, at least for me).
You get a new row named "update to"
here you write: [Forms]![NameOfYourForm].[txtRm]

To get the button to run the query for you, you write:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdUpdate_Click()
  2. 'Updates Remark field in table Allotment
  3.  
  4. With DoCmd
  5.     .SetWarnings False
  6.     .OpenQuery "the name of your query"
  7.     .SetWarnings True
  8. End With
  9.  
  10. End Sub
You might want a subform to display the records but that is up to you.

And you should be home free ;)
Unless I totally misunderstood you this is the way I would do it.
Nov 9 '16 #5
nndkol
12
Its working!
Thank a ton from my heart. I know nothing about code, I only try to understand logic and try to find suitable code in the forum.
Finally,
Private Sub cmdUpdate_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("Allotment")
rs.MoveFirst
Do Until rs.EOF

Dim s As String
s = "UPDATE Allotment SET Remarks ='" & Me.txtRm & "' "
s = s & "WHERE [FinacialYear] = " & Me.cmbFY & " "
s = s & "And [Demand No] = " & Me.cmbDmnd & " "
s = s & "And [Major Head] = " & Me.cmbMj & " "
s = s & "And [Minor Head] = " & Me.cmbMn & " "
s = s & "And [Sub-Head] = " & Me.cmbSb

CurrentDb.Execute s, dbFailOnError

rs.MoveNext
Loop
MsgBox "Records Updated!"

rs.Close
Set rs = Nothing
Set db = Nothing

End Sub

... But I don't know what to code if records not found & error message for that.
Kindly see.
With best regards,
Nov 9 '16 #6
nndkol
12
I have also tried Anathil's post. This is also working perfectly. I am really grateful to you for helping me not only solve the issue but also learn a new thing. I don't know how to write error message when the records not found
Nov 9 '16 #7

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

Similar topics

0
by: EKL | last post by:
Hi, I'm making a sort of Customer and Orders database in MS Access 2003. My problem is that I wish to update the table "tblTransaction" based on changes made in the table "tblOrderDetails"....
8
by: RC | last post by:
In my Access 2002 form, I have a combo box and on the AfterUpdate event I use DoCmd.RunSQL ("UPDATE .... to update records in a table. When it starts to run I get a message "You are about to...
1
by: Tom | last post by:
hello i have 2 tables linked by a common id. The first table has records with a specific field with numeric values. eg 3 or 6 or 10 etc. What i want to happen is when i create a data entry form, i...
1
by: Sharon | last post by:
Hello All, Is it possible to update Sql Table through DataGrid. I have a DataGrid which is being populated through a stored procedure, all i wanted to do is to update one field...
1
by: Seemaraj | last post by:
I have doubt in VB.Net on how to update the records in table. I m using MS Access database. I have a table named len_graph with 9 fields like...
1
by: vaiism | last post by:
I have attempted using both Updata and Append Queries to solve this problem, and I can't seem to get it to work. I have three forms that each have an ID#, and a subform with related scores to...
7
by: emajka21 | last post by:
I have been working on creating a HR database. Everything is working fine there but now they want a training database. I basically need a few fields from the employee table and I need the full...
1
by: Esmi | last post by:
I am developing an application in Ms Excel 2003 that has a feature to update records in Ms Access. The problem is the table name I am trying to update contains a space and whenever I run the below...
7
by: ndhvu | last post by:
Tables: Buy_Header and Buy_Detail. - Buy_Header: info. of each buy (buy_id(PK, auto number), date, shop, bought_by, ...) - Buy_Detail: info. of each item from each buy (buy_detail_id(PK, auto...
4
by: Kelly Warden | last post by:
I want to create a form in Access to update my table automatically with the information selected from combo boxes, lists, comments, etc. Also, I want to automate this w/ some VBA or SQL or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
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.