473,588 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create new row in table

675 Contributor
I want to have an "Edit" form=fEditLoc for rows in a subform=subfSho wLoc. Once the form opens, I want to be able to create a new record in the table=tLoc which I will then edit.

Or to put it another way, I want to edit a record, and then do a "Save As" to a new record. Seems simple, but I can't keep the data in all the fields when I move the form to the NewRecord position. So how do I do this?
Apr 8 '09 #1
16 3178
ADezii
8,834 Recognized Expert Expert
@OldBirdman
Hello OldBirdman, try this out and see what happens:
Expand|Select|Wrap|Line Numbers
  1. 'Select Current Record
  2. DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  3.  
  4. 'Copy Current Record
  5. DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
  6.  
  7. 'Paste Append Record
  8. DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70
Apr 8 '09 #2
OldBirdman
675 Contributor
Thank you. These tools will work for this.

Just curious, but I think somewhere I read that DoMenuItem was to be avoided if possible. The relative position of the items may change, or something. Is this something I should be concerned about?
Apr 8 '09 #3
OldBirdman
675 Contributor
I think I replied too quickly. I would like my "Save As" to leave the original record unchanged, as happens if we edit a file in say NotePad, make some changes, and decide to create a new file. We SaveAs and continure editing. At the end, the original file is unchanged, and the new file has the changes from before the SaveAs and after it (presuming a final Save before closing).

Line #2 of the code suggested in Post #2 saves the record. If I select before any changes are made, it is un-selected when form becomes dirty.

I could save all the fields using each control's OldValue property. Then I select the record, allowing Access to save the changed record. On FormClose, I would have to move back to the original record, and restore the original values and save it. Procedures would have to be recursive, because I might want to create a series of records, each a slight change from the previous one.

1. Call form
2. Make changes, say txtField = Test 1
3. "Save As" to create a new record
4 Change the 1 to a 2, avoiding typing the word "Test" and any other common data fields
5. "Save As" to create a new record
6. ... Repeat as necessary

This seems clumsy, crude, and amaturish. Like most of my code, I'm afraid.
Apr 8 '09 #4
ADezii
8,834 Recognized Expert Expert
@OldBirdman
I do have a solution that I based on the [FirstName] and [LastName] Fields of the Employees Form in the Northwind Sample Database. Make changes to the First and Last Name Fields, then execute the following code within the context of the Form, making sure to previously Declare the Globals. An exact Duplicate of the Record will be created then Appended. The Original Record will revert back to its Original State prior to the changes. I know it is a little convoluted, but for now, see if it will do until a better solution comes along. I tested it and it actually does work! (LOL)!
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. '*******************************************************************************************
  4. 'Store Original Values in Public Variables  Declared in a Standard Code Module
  5. glngEmployeeID = Me![EmployeeID]
  6. gstrFirstName = Me![FirstName].OldValue
  7. gstrLastName = Me![LastName].OldValue
  8. '*******************************************************************************************
  9.  
  10. '*******************************************************************************************
  11. 'Define the SQL Statement that will restore the Original Values to the very Record from which
  12. 'changes were made, and a Duplicate created
  13. strSQL = "UPDATE Employees SET Employees.FirstName = '" & gstrFirstName & "', " & _
  14.          "Employees.LastName = '" & gstrLastName & "' Where Employees.[EmployeeID] = " & _
  15.           glngEmployeeID
  16. '*******************************************************************************************
  17.  
  18. '*******************************************************************************************
  19. 'Duplicate the Record with modifications
  20. 'Select Current Record
  21. DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  22.  
  23. 'Copy Current Record
  24. DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
  25.  
  26. 'Paste Append Record
  27. DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70
  28. '*******************************************************************************************
  29.  
  30. '*******************************************************************************************
  31. 'Restore Original Values to the Original Record
  32. CurrentDb.Execute strSQL, dbFailOnError
  33. '*******************************************************************************************
  34.  
  35. 'P.S. - At some point you 'MUST' Reset the Public Variables before duplicating this Procedure
Apr 8 '09 #5
OldBirdman
675 Contributor
OK, Good. I never used an UPDATE query pefore, so that was my missing piece. I don't get the Global Variables. Why? strSQL = "UPDATE ..." & Me![EmployeeID].OldValue & "..." & Me![FirstName].OldValue & ..... saves the data locally. It only has to last from line 13 to line 32.

Thank you for your time & effort.
Apr 8 '09 #6
ADezii
8,834 Recognized Expert Expert
@OldBirdman
Anytime, sorry for the confusion, strSQL can be Declared Locally as is.
Apr 8 '09 #7
OldBirdman
675 Contributor
This doesn't work, and I'm going nuts trying to figure why.

I have 2 tables, linked in Relationships. I'm trying to create a simple form to edit 2 of the fields in the child table, and be able to start an edit and then do a "Save As".

Expand|Select|Wrap|Line Numbers
  1. Tables
  2. Table 1 Name = tPhotographers
  3. Key as Autonum (Random) = PK
  4. ...
  5.  
  6. Table 2 Name = tPhotographerLocations
  7. Key as Autonum (Random) = PK
  8. Photographers  as Long 'Points to Key in table = tPhotographers
  9. LastDate       as Date
  10. Type           as String
  11. Location       as String
The form has 5 textboxes for the 5 fields in the table, named txtXXX where XXX is the name in the table. For testing, this form is being opened from the DataBase (Forms) window, and no other forms are open.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. 'Photographers key = 782320442
  3.     Me.RecordSource = "SELECT * FROM tPhotographerLocation WHERE Key=1251495069"
  4. End Sub 'Form_Load
  5.  
I have 1 record (Key=1251495069 ) a test record with Photographers pointing to a test record. Everything displays correctly at this point.

In txtLocation, I change the current value "Test 1" to my new value "Test 2". I press button = cmdSaveAs to create a new record, and to restore the initial record to its original state ("Test 1").
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSaveAs_Click()
  2. Dim strSQL As String
  3. Dim iiHoldPtr As Long
  4.  
  5.     iiHoldPtr = txtPhotographer
  6.     If Me.Dirty Then
  7.         'Build SQL to restore original record
  8.         strSQL = "UPDATE tPhotographerLocation " & _
  9.                 "SET Type='" & Nz(txtType.OldValue, txtType) & _
  10.                 "', Location='" & Nz(txtLocation.OldValue, txtLocation) & _
  11.                 "' WHERE Key=" & txtKey
  12.     Else
  13.         strSQL = ""
  14.     End If
  15.  
  16.     'Select the record now being edited
  17.     DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  18.     'Copy the Record
  19.     DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
  20.     'Paste append record to create new record
  21.     DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append
  22.     Me.Dirty = False 'Force Save for test purposes only
  23.     txtPhotographer = iiHoldPtr
  24.     Me.Dirty = False 'Force Save for test purposes only
  25.     'Restore original record (if necessary)
  26.     If strSQL <> "" Then CurrentDb.Execute strSQL, dbFailOnError
  27. End Sub
With a breakpoint at Line 22, I have the following in Immediate (window). First, I did a cntl+V to paste the clipboard into Immediate.
Expand|Select|Wrap|Line Numbers
  1. txtKey  txtPhotographer txtLastDate txtType txtLocation
  2. 1251495069  782320442   4/8/2009    X   Test 2
I then printed some controls and variables:
Expand|Select|Wrap|Line Numbers
  1. ?txtKey, txtPhotographer, txtLastDate, txtType, txtLocation
  2.  1976622305    0            Null          X             Test 2
  3.  
  4. ?me.Dirty 
  5. True
Note that the foreign key txtPhotographer is 0 and invalid as there is no key=0 in table=tPhotogra phers. The date field is Null, also invalid, but the other fields are correct.
Attempting to run Line 22 gets me
Expand|Select|Wrap|Line Numbers
  1. Run-time error '3201':  You cannot add or change a record because a related record is required in table 'tPhotographers'. 
which is because txtPhotographer is now 0 and invalid. If I skip Line 22, and run Line 23, Line 24 also works. I really don't want to do this, because now UnDo in not possible, but for now, OK.
Line 26 restores the original record, and closing the form leaves me with 2 records, but the date is null in the 2nd record.

If I now change txtLocation to "Test 3" and continue, more problems occur. So I need to understand why this part doesn't work, rather than just force it with more code like Line 23.
A real-life example would be if a photographer visits a city briefly, the location might be entered as txtLocation =
Expand|Select|Wrap|Line Numbers
  1. AnyCity, SomeState, Country
If he returns for a more extensive visit, he might want to edit this, adding local locations such as
Expand|Select|Wrap|Line Numbers
  1. Central Park, AnyCity, SomeState, Country
  2. Waterfront Park, AnyCity, SomeState, Country
  3. Courthouse Park, AnyCity, SomeState, Country
  4. Civic Center, AnyCity, SomeState, Country
  5. ...
  6.  
Line 21 is the code that is producing incorrect results. Something is very wrong with this approach. Giving up the office clipboard is an undesired side-effect of this method, but I could live with it if it is the only way. Can it be fixed?

It seems like the code might be:
Expand|Select|Wrap|Line Numbers
  1. rst = tPhotographerLocations
  2. rstclone = tPhotographerLocations.clone
  3. rstclone = set to current record
  4. rst = set to new record
  5. assign rst from rstclone
  6. rstclone.undo
Any ideas here?
Apr 9 '09 #8
ADezii
8,834 Recognized Expert Expert
@OldBirdman
Can you send me a Copy of the Database if I give you my E-Mail Address in a Private Message?
Apr 9 '09 #9
OldBirdman
675 Contributor
That presented was a tiny piece of a larger system, including linked tables. I'll see if I can isolate this into a test database, as the problem is fairly simple.

When I've sent my problems to others, they don't recreate on their machines. That implies that mine are corrupt, although I run two different versions of Office (2000 and 2002). Both machines, 2 versions of a program produce the same symptoms.

I am going to try to figure how to do this with direct manipulation of the tables, as I hinted at the end of my previous post.

Thank you for your effort and concern.
Apr 9 '09 #10

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

Similar topics

4
4050
by: Phil Powell | last post by:
create table if not exists nnet_produkt_varegruppe ( nnet_produkt_varegruppe_id int not null auto_increment, primary key(nnet_produkt_varegruppe_id), nnet_produkt_varegruppe_navn varchar(255) not null ); create table if not exists nnet_produkt_farge ( nnet_produkt_farge_id int not null auto_increment, primary key(nnet_produkt_farge_id),
6
6566
by: dev | last post by:
how create a temp table as a copy of a existing table and then update1 field and insert the hole temp table back in the existing table? please any help? if i have 10 fields in 1 record and about 100 records and a field.status=1 in a existing_table and i want to create a temp_table with all the recordse and values of the existing_table and then update the field.status to 2 and insert in 1 query the temp_table in the existing_table
4
4432
by: Michael Jackson | last post by:
I have a stored procedure to create a table. In my program I want the user to name the table to be created, therefore I pass a parameter to the SP for the table name. I cannot get it to work. It creates a table called "@NewTableName". Any ideas? CREATE PROCEDURE dbo.sp_FFProduction_CreateTable (
7
9670
by: Wolfgang Kreuzer | last post by:
Hello all, I have two tables - Projects and ProjectStruct Table Projects contains master records of the projects, ProjectStruct allows to define a project herarchie and contains the fields PrjStructId, ProjectId, PrjStructName, ..., ParentId PrjStructParent contains a reference to the parent or to itselves if record is top-level-record for a project.
1
3345
by: poohnie08 | last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot hour, slot1, slot2, slot3, slot4 and others). The "()" will keep repeating from day 1 until end of month. eg in excel spreadsheet, ============================================================================== A1 |A2 A3 A4 A5 A6 A7 A8 |A9 A10 A11 | 01/02/04 |02/02/04 StaffName |Work Hr OT Hr Slot1...
24
3089
by: flkeyman | last post by:
Work in legal office. Trying to create solid designed database structure. This is list of tables w/fields and primary keys. Any comments/advice greatly appreciated. tbl-Defendants CaseNumber (primary key) FirstName MiddleName LastName
6
7700
by: Peter Nurse | last post by:
For reasons that are not relevant (though I explain them below *), I want, for all my users whatever privelige level, an SP which creates and inserts into a temporary table and then another SP which reads and drops the same temporary table. My users are not able to create dbo tables (eg dbo.tblTest), but are permitted to create tables under their own user (eg MyUser.tblTest). I have found that I can achieve my aim by using code like...
27
3771
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
4
12427
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
2
2425
by: lakuma | last post by:
Hi, I have a table called A (say) with columns called name, place, animal and thing. I would want to write an on insert trigger on this table, which would create a table with the name of the value entered in the name column. Let's say a new column is entered with the value of name column as mickey. I want to create a table called mickey, with the help of triggers. I'm really new to this and i would like some help. Thanks in advance for...
0
7860
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5726
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.