473,405 Members | 2,334 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,405 software developers and data experts.

Add and update records in VBA

Hi all,

I'm trying to update a record in a table with data from text boxes on a
form. As a background to this, I managed to add a new record to the
table in a similar manner with the following:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.AddNew
rs("Project_Number") = Me![Project_Number]
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close
This works really well, but now I want to be able to change the data in
an existing record. Firstly I put the three bits of info onto the form
and then the user can change either the project manager or project
name. The project number is protected as this is a unique id for the
record, (although this is not the primary key in case you were
wondering!)

It seems to me that all I should do is something like this:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.Edit
SrchCrit = "[Project_Number] ='" & Me![Project_Number] & "'"
rs.FindFirst SrchCrit
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close

However this just bombs out on the FindFirst method saying it's not
supported for this type of object. I know I have to do some sort of
search on the records because I need to ensure I'm changing the correct
one.

So someone please tell me what stupid amateur mistake I'm making here.

Cheers

Dave.

Feb 17 '06 #1
2 75000
DaveN wrote:
Hi all,

I'm trying to update a record in a table with data from text boxes on a
form. As a background to this, I managed to add a new record to the
table in a similar manner with the following:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.AddNew
rs("Project_Number") = Me![Project_Number]
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close
This works really well, but now I want to be able to change the data in
an existing record. Firstly I put the three bits of info onto the form
and then the user can change either the project manager or project
name. The project number is protected as this is a unique id for the
record, (although this is not the primary key in case you were
wondering!)

It seems to me that all I should do is something like this:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.Edit
SrchCrit = "[Project_Number] ='" & Me![Project_Number] & "'"
rs.FindFirst SrchCrit
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close

However this just bombs out on the FindFirst method saying it's not
supported for this type of object. I know I have to do some sort of
search on the records because I need to ensure I'm changing the correct
one.

So someone please tell me what stupid amateur mistake I'm making here.

Cheers

Dave.

Why don't you search for the item first before you start editting it?
Feb 17 '06 #2

"DaveN" <da*****************@hotmail.co.uk> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi all,

I'm trying to update a record in a table with data from text boxes on a
form. As a background to this, I managed to add a new record to the
table in a similar manner with the following:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.AddNew
rs("Project_Number") = Me![Project_Number]
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close
This works really well, but now I want to be able to change the data in
an existing record. Firstly I put the three bits of info onto the form
and then the user can change either the project manager or project
name. The project number is protected as this is a unique id for the
record, (although this is not the primary key in case you were
wondering!)

It seems to me that all I should do is something like this:

Set db = CurrentDb
Set rs = db.OpenRecordset("Project Table")
rs.Edit
SrchCrit = "[Project_Number] ='" & Me![Project_Number] & "'"
rs.FindFirst SrchCrit
rs("Project_Manager") = Me![Project_Manager]
rs("Project_Name") = Me![Project_Name]
rs.Update
rs.Close

However this just bombs out on the FindFirst method saying it's not
supported for this type of object. I know I have to do some sort of
search on the records because I need to ensure I'm changing the correct
one.

So someone please tell me what stupid amateur mistake I'm making here.

Cheers

Dave.


While looking at your code, select Tools>References look for something like:
Microsoft ActiveX Data Objects 2.1 Library
I expect you will be able to uncheck it with no problems, but after you do
make sure your code compiles.

What is happening is that there are two competing object libraries, both of
which have the recordset object. However, they are not identical and
although the DAO.Recordset object has a FindFirst method, the
ADODB.Recordset object does not. Since you have not specified which type of
recordset you want, Access assumes you are talking abot an ADO one and can't
find this method.

The morals of this episode are:
1. Remove any un-used references from your application
2. Always dimension your objects to show which library, e.g Dim dbs As
DAO.Database, Dim rst As DAO.Recordset

Now the next bit. If you did use the FindFirst method then you should check
to see whether you have found anything before you proceed. Check the
..NoMatch condition. However, in any case you should never load the whole
table into a recordset if you only intend to edit one record. All you need
is:

SELECT Project_Manager, Project_Name
FROM [Project Table]
WHERE Project_Number="ABC123"

So if this is a valid project number, your recordset has a single record in
it which you can edit.

As a final point, you can also avoid recordsets altogether by using
dbs.Execute strSQL where strSQL is an update query.
Feb 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Pastrovick | last post by:
I use a function, myrandomPIN (), to generate random PIN numbers. The following sql query updates records with the SAME PIN number but. I want to generate DIFFERENT pin numbers for every record....
2
by: Sporke13 | last post by:
I have used stored procedures to insert and select but for some reason I can not get this code to update records. Please help I must have made a dumb mistake STORED PROCEDURE CREATE Procedure...
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...
8
by: mantrid | last post by:
Hello Im having problems working out why the following code does not work. I dont think its the sql as the error occurs on the first update which ever one is put there ($q1 or $q2). Ive swapped...
0
by: sukaina | last post by:
hello i want to update some records in the database by using checkboxes. I wrote a code but it didn't work proparly. It update whole records not only the selected one by the checkbox. how can i...
3
by: ebru | last post by:
about insert/delete/update records in mysql server with asp code how can I insert record in mysql database.. I insert into database but if I leave one text box empty, its give me error how can...
3
by: Toby Gallier | last post by:
I am trying to figure out a way to update multiple records when a report is exported. Each record has a "Status" field. I have a query that selects all records with a status of "To Send" and that...
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...
4
colinod
by: colinod | last post by:
I am trying to update multiple records in 1 hit, i have a page that sends the info to an update page with ids for the records generated with a count, these are mp31, mp32 etc and also an id for the...
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.