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

Access 2003: Insert/Update records by form with subform using VBA

5
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 number), buy_id, item_name, price, quantity,...)

Target:
- Make a main-form of table [Buy_Header] with sub-form of table [Buy_Detail] to insert/update records for both tables. Each form of each [Buy_Header] and the sub-form can have many records of [Buy_Detail].
- When update, you can add more records to [Buy_Detail] through sub-form

Problem:
- I can make a main-form that link directly to table/query [Buy_Header] with sub-form link to table/query [Buy_Detail] to insert/update. However, although it is easy, the drawback is that I cannot cancel the process. Right after I input value to the main-form fields and click on any field of sub-form, Access will automatically insert/update data of the main-form to DB, even if I later clear all the field (Undo method is kind of useless here). Therefore, I intent to use VBA for insert/update instead.

- However, even though I can insert/update the info. of the form, I have a problem with the sub-form data.
1. I don't know how to get the [buy_id] field from the main-form to put into the data of sub-form.
2. I cannot distinguish each row of the sub-form. I use [datasheet] type for sub-form and naming each field of it. However, when I want to get the value from any field, with VBA, I can only get the value of the field from top row.

I am a totally Access/VBA newbie (but have a good knowledge of coding). Any idea will be appreciated. Thank you.
Sep 6 '10 #1

✓ answered by ndhvu

After search, I intent to use these code for Insert task:

Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database
  2.     Dim rst1 As Recordset
  3.     Dim rst2 As Recordset
  4.     Dim rst3 As Recordset
  5.     Dim rs As Recordset
  6.  
  7.     Dim buyHeaderId As Long
  8.     Dim wrk As Workspace
  9.  
  10.     Set wrk = DBEngine.Workspaces(0)
  11.     Set db = DBEngine.Workspaces(0).Databases(0)
  12.  
  13.     wrk.BeginTrans
  14.  
  15.     Set rst1 = db.OpenRecordset("BUYHEADER", DB_OPEN_DYNASET, DB_DENYREAD)
  16.     rst1.AddNew
  17.     rst1![INVOICE_ID] = Me.txtInvoiceNo
  18.     rst1![BUY_PERSON] = Me.txtUser
  19.     rst1![BUY_DATE] = Me.txtBuyDate
  20.  
  21.     stockInHeaderId = rst1![BUY_HEADER_ID]
  22.     rst1.Update
  23.     rst1.Close
  24.  
  25.     Set rst2 = db.OpenRecordset("BUYDETAIL", DB_OPEN_DYNASET, DB_DENYREAD)
  26.     Set rs = Me.subfrmStockOutDetailInsert.Form.RecordsetClone
  27.     rs.MoveFirst
  28.     While Not (rs.BOF Or rs.EOF)
  29.             rst2.AddNew
  30.             rst2![BUY_HEADER_ID] = buyHeaderId
  31.             rst2![ITEM] = rs.Fields.Item(0).Value
  32.             rst2![QUANTITY] = rs.Fields.Item(1).Value
  33.             rst2![PRICE] = rs.Fields.Item(2).Value            
  34.             rst2.Update
  35.             rs.MoveNext
  36.     Wend
  37.     rst2.Close
  38.  
  39.     Set rst3 = db.OpenRecordset("BUYTEMP", DB_OPEN_DYNASET, DB_DENYREAD)
  40.     rst3.MoveFirst
  41.     While Not (rst3.BOF Or rst3.EOF)
  42.         rst3.Delete
  43.         rst3.MoveNext
  44.     Wend
  45.     rst3.Close
  46.  
  47.     wrk.CommitTrans
  48.  
  49. Insert_Exit:
  50.     Exit Sub
  51.  
  52. Insert_ErrHandler:
  53.     wrk.Rollback
  54.  
However, I not sure if update task should use the same approach.

7 7587
TheSmileyCoder
2,322 Expert Mod 2GB
I think in a case like this, I would create a temporary table to hold my Buy_Header and a temp table to hold my Buy_Detail, then when your order is done, click a button (or cancel) and transfer the record to the main tables and clear the temporary tables. That will allow you to use the built in subform functionality, and not having to re-invent it all from scratch.
Sep 6 '10 #2
ndhvu
5
Thanks for your reply :).
I just got the same idea about making tables to hold data temporary, transfer and then delete all record after finish. The problem are:
1. Work well with insert but not with update. using this way for update data meaning i have to delete the old data and insert a new one. It ill disrupt the order of records (or at least that is what I think) and making the 2nd problem happen faster.
2. Use up a lot of number for the [auto-number PK]. Since each buy can include a lot of items, the [auto-number PK] of [Buy_Detail_Temp] can reach the limit very fast. Unless I can some way to TRUNCATE the table like on other DB.

But maybe I will stick to this way for now before I can figure out a better solution.

Thank you.
Sep 6 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
1) When is it you require the ability to Update?

2) The limit is on autonumber field is 2.147.000.000 (roughly) as far as I know. If your approaching that amount of sales, I think you can afford to buy a system instead of developing it yourself!
Sep 6 '10 #4
ndhvu
5
1) The insert form and update form have same structure and I want to find an approach that can work well with both of them in order to reduce the writing and maintenance time (or so I hope), since I also have some other forms with the same relationship.

2) I have no idea I can be that much :), but it still be better if there is a way to TRUNCATE the table. But well, my boss doesn't even care about how the project run anyway. Just make sure the job done is good enough :).
Sep 6 '10 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Problem:
- I can make a main-form that link directly to table/query [Buy_Header] with sub-form link to table/query [Buy_Detail] to insert/update. However, although it is easy, the drawback is that I cannot cancel the process. Right after I input value to the main-form fields and click on any field of sub-form, Access will automatically insert/update data of the main-form to DB, even if I later clear all the field (Undo method is kind of useless here). Therefore, I intent to use VBA for insert/update instead.
Lets return to the core of the problem. Maybe all you need is a delete button (we can call it undo, if you like).
I don't know enough about how you intend to use your forms and for what purpose, to give more specific suggestions.

If the problem is that you MUST update both tables at the same time, please specify so. (This might also be directly achievable from the subform)
Sep 6 '10 #6
ndhvu
5
If you ask if after i click [Save] button, the data on the main-form and sub-form will be insert/update to both table, then the answer is Yes.

If users close the form, then data simple won't be insert/update into DB

This is but a simple project for daily tasks for a small office, so a simple solution will be preferred.
Sep 6 '10 #7
ndhvu
5
After search, I intent to use these code for Insert task:

Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database
  2.     Dim rst1 As Recordset
  3.     Dim rst2 As Recordset
  4.     Dim rst3 As Recordset
  5.     Dim rs As Recordset
  6.  
  7.     Dim buyHeaderId As Long
  8.     Dim wrk As Workspace
  9.  
  10.     Set wrk = DBEngine.Workspaces(0)
  11.     Set db = DBEngine.Workspaces(0).Databases(0)
  12.  
  13.     wrk.BeginTrans
  14.  
  15.     Set rst1 = db.OpenRecordset("BUYHEADER", DB_OPEN_DYNASET, DB_DENYREAD)
  16.     rst1.AddNew
  17.     rst1![INVOICE_ID] = Me.txtInvoiceNo
  18.     rst1![BUY_PERSON] = Me.txtUser
  19.     rst1![BUY_DATE] = Me.txtBuyDate
  20.  
  21.     stockInHeaderId = rst1![BUY_HEADER_ID]
  22.     rst1.Update
  23.     rst1.Close
  24.  
  25.     Set rst2 = db.OpenRecordset("BUYDETAIL", DB_OPEN_DYNASET, DB_DENYREAD)
  26.     Set rs = Me.subfrmStockOutDetailInsert.Form.RecordsetClone
  27.     rs.MoveFirst
  28.     While Not (rs.BOF Or rs.EOF)
  29.             rst2.AddNew
  30.             rst2![BUY_HEADER_ID] = buyHeaderId
  31.             rst2![ITEM] = rs.Fields.Item(0).Value
  32.             rst2![QUANTITY] = rs.Fields.Item(1).Value
  33.             rst2![PRICE] = rs.Fields.Item(2).Value            
  34.             rst2.Update
  35.             rs.MoveNext
  36.     Wend
  37.     rst2.Close
  38.  
  39.     Set rst3 = db.OpenRecordset("BUYTEMP", DB_OPEN_DYNASET, DB_DENYREAD)
  40.     rst3.MoveFirst
  41.     While Not (rst3.BOF Or rst3.EOF)
  42.         rst3.Delete
  43.         rst3.MoveNext
  44.     Wend
  45.     rst3.Close
  46.  
  47.     wrk.CommitTrans
  48.  
  49. Insert_Exit:
  50.     Exit Sub
  51.  
  52. Insert_ErrHandler:
  53.     wrk.Rollback
  54.  
However, I not sure if update task should use the same approach.
Sep 6 '10 #8

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

Similar topics

0
by: Josh C. | last post by:
Hello everyone. I'm a bit of an Access newbie, so please bear with me. Please go to http://www.dumoti.com/access/ to view the database - 536kb. I'll go straight into examples: In the form...
2
by: hawg1 | last post by:
I'm fairly new to Access and need help with form/subform. I've seen another access database in which a main form allowed the user to enter school class specific information shuch as start date,...
1
by: tbo | last post by:
The code below works fine to update my database by adding rows of data to my dataTable. However when I use the same commands (changing the names of database, datatable and rows) with another...
5
by: bubbles | last post by:
I am developing an Access 2003 frontend for SQL Server 2005. Problem: Access 2003 cannot update tables in SQL Server 2005 (but can update SQL Server 2000 tables) Is there a patch for Access...
5
Scott Price
by: Scott Price | last post by:
Hello, I'm running Access 2003 trying to filter a subform with approximately 15 records per year per the main record contained on the parent form. The filter works fine if the subform is opened...
6
by: mahowe | last post by:
Hi, I have had this problem for a while and have not been able solve it. What im looking at doing is looping thru my patient table and trying to organise the patients in to there admission...
1
by: Bobby | last post by:
Hi I am using Access 2003 mdb as a front end to an application which uses SQL Server 2000 as the backend. The two are connected using ODBC. On one particular table (the Stock table), I have a...
2
by: venkatanarasimhaa | last post by:
Hi, I am very basic SQL user. I have a table in SQL server called "Employee" Now I would like to insert a record into the same using VB 6.0 coding. 1) I need to enter : Employee_Name =...
1
by: sop23456 | last post by:
Microsoft Access 2003 I have hyperlink the image/picture to another file. When I click on the hyperlink, the image appears using MS Paint. What do I do to view Image/picture in Windows Picture...
3
by: grinder332518 | last post by:
My Form accesses a Query as follows : SELECT tblA.A, tblB.B, tblB.C FROM tblA LEFT JOIN tblB ON tblA.A = tblB.A UNION SELECT tblB.A, tblB.B, tblB.C FROM tblB LEFT JOIN TblA ON ...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.