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

Access 2007 Subform acting as clipboard, not adding new row to subform table

I am using Access 2007. I have a form with fields that I want to copy to a subform. I nave an ADD button that can sopy the data to the subform, so the subform is acting as a clipboard. I can get it to add a row, but not add a new row to subform table after doing one entry into the subform.

Here is some code I have tried:

Expand|Select|Wrap|Line Numbers
  1.     With Me.GiftListSubform
  2.         .Form.SetFocus
  3.         .Form![GiftQuantity].SetFocus
  4.         RunCommand acCmdRecordsGoToNew
  5.         .Form!GiftQuantity = Me.GiftQuantity
  6.         .Form!GiftItemReceived = Me.GiftItemReceived
  7.         .Form!TitleRank = Me.TitleRank
  8.         .Form!FirstName = Me.FirstName
  9.         .Form!LastName = Me.LastName
  10.         .Form!GiftDate = Me.GiftDate
  11.         .Update
  12.     End With
  13.  
It doesn't like the .Update with this one.

Expand|Select|Wrap|Line Numbers
  1.     Forms![GiftRecipientForm]![GiftListSubform]!GiftQuantity = Me.GiftQuantity
  2.     Forms![GiftRecipientForm]![GiftListSubform]!GiftItemReceived = Me.GiftItemReceived
  3.     Forms![GiftRecipientForm]![GiftListSubform]!TitleRank = Me.TitleRank
  4.     Forms![GiftRecipientForm]![GiftListSubform]!FirstName = Me.FirstName
  5.     Forms![GiftRecipientForm]![GiftListSubform]!LastName = Me.LastName
  6.     Forms![GiftRecipientForm]![GiftListSubform]!GiftDate = Me.GiftDate
  7.  
It copies one record here but doesn't let you add more than one record into the subform at a time.

Can there be a way to have the Add button create rows each time it is clicked with updates of textbox entries data?
Aug 4 '15 #1
10 2292
jforbes
1,107 Expert 1GB
Using the UI to do something like this can be like a house of cards. Tricky to build and even trickery to maintain.

I would recommend creating a Method to insert records into your Sub Form/Table (GiftList), then call the Method from the Main Form/Table (GiftRecipient). You could create a Function similar to this:
Expand|Select|Wrap|Line Numbers
  1. Public Function addGift(ByRef lGiftQuantity As Long, ByRef iGiftItemReceived As Integer, Byref sTitleRank As String, Byref sFirstName As String, Byref sLastName As String, Byref dGiftDate As Date) As Boolean
  2.  
  3.     Dim sSQL As String
  4.  
  5.     addGift = False
  6.  
  7.     sSQL = sSQL & "INSERT INTO GiftList ("
  8.     sSQL = sSQL & "  GiftQuantity "
  9.     sSQL = sSQL & ", GiftItemReceived "
  10.     sSQL = sSQL & ", TitleRank "
  11.     sSQL = sSQL & ", FirstName "
  12.     sSQL = sSQL & ", LastName "
  13.     sSQL = sSQL & ", GiftDate "
  14.     sSQL = sSQL & ") VALUES ("
  15.     sSQL = sSQL & "  " & lGiftQuantity & "'"
  16.     sSQL = sSQL & ", '" & iGiftItemReceived & "'"
  17.     sSQL = sSQL & ", '" & sTitleRank & "'"
  18.     sSQL = sSQL & ", '" & sFirstName & "'"
  19.     sSQL = sSQL & ", '" & sLastName & "'"
  20.     sSQL = sSQL & ", #" & dGiftDate & "#"
  21.     sSQL = sSQL & ")"
  22.  
  23.     CurrentDB.Execute sSQL, dbFailOnError + dbSeeChanges
  24.  
  25.     addGift = True
  26.  
  27. End Sub
Then on your Main Form, you can have your button that when clicked would call the Method kinda like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAdd_Click()
  2.  
  3.     Dim lGiftQuantity As Long
  4.     Dim iGiftItemReceived As Integer
  5.     Dim sTitleRank As String
  6.     Dim sFirstName As String
  7.     Dim sLastName As String
  8.     Dim dGiftDate As Date    
  9.  
  10.     lGiftQuantity = Nz(Me!GiftQuantity .Value, 0)
  11.     iGiftItemReceived = Nz(Me!GiftItemReceived.Value , 0)
  12.     sTitleRank = Nz(Me!TitleRank.Value , "")
  13.     sFirstName = Nz(Me!FirstName.Value, "")
  14.     sLastName = Nz(Me!LastName.Value, ""0)
  15.     dGiftDate = Nz(Me!GiftDate.Value, Now())
  16.  
  17.     Call addGift(lGiftQuantity, iGiftItemReceived, sTitleRank, sFirstName, sLastName, dGiftDate)
  18.  
  19.     Me.SubForm.Requery
  20.  
  21. End Sub
The nice thing about this is that if the addGift Function is placed into a Module and made Public it can be called from any Form or Method in the Database. You could then take it further and add some business logic/validation to the Function to make sure you aren't adding duplicates or that all the necessary information is provided and jives.
Aug 5 '15 #2
Thank you jforbes,

This is some awesome looking code. I'm a little more an engineer than a coder, but maybe I can learn. There seem to be some typos in what you had, but that's easy to understand. I'm guessing you weren't copying and pasting from your own test code. It all seems to work OK, except for an error when trying to run the SQL statement. Here is an example of what the sSQL variable looks like when trying to run the SQL:

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO GiftList (  GiftQuantity , GiftItemReceived , TitleRank , FirstName , LastName , GiftDate ) VALUES (  '1', '2', 'CTR', 'Matt', 'Dewell', #8/6/2015#)"
Can you tell what might be going wrong here?

Thanks a lot.
Aug 5 '15 #3
Hello jforbes,

I think I found what the error was. A variable was defined as an integer, when the field was a string in the DB table. I got that fixed. Plus a control name was not matching, but that's not a problem now.

Here's where things get a little funny. It added it to the table fine. Yet it doesn't seem to show it in the subform. It clears out the entry from displaying in the subform as soon as the query is run. At least that's what it looks like. It at least shows up in the Table when I go to just open the table. What can I do to get the subform to display the table entries that have been added? Do I have something set wrong with my subform?

Thank You
Aug 5 '15 #4
jforbes
1,107 Expert 1GB
Yeah, I see some of the errors you are talking about. I just copy and pasted into the Reply Box something similar I had and then copy and pasted from you example. I'm fighting the urge to correct the typos, but I think I can resist. =)

In my experience it's lining up the datatypes seems to take up the most time and is the most error prone part of a SQL insert. Strings need to be Single Quoted, Numbers must not, and Dates need to be boxed in with # (only in Access, in SQL Server they can be Single Quoted). So my guess it needs to be more like this:
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO GiftList (  GiftQuantity , GiftItemReceived , TitleRank , FirstName , LastName , GiftDate ) VALUES (  1, 2, 'CTR', 'Matt', 'Dewell', #8/6/2015#)"
I realize the orginal code wasn't right, but again, it was some copy and paste magic. Additionally, I typically use a SQL Server Backend, so I took my best shot at the Date syntax.
Aug 5 '15 #5
jforbes
1,107 Expert 1GB
You'll probably need to include the Primary Key of the Main Table (GiftRecipient) as part of the Insert into the Sub Table. It would be the same column that you used to link your Sub Form to your Main Form. I just guessed as to the columns you would need.
Aug 5 '15 #6
OK, now this is where it gets really weird, at least for my knowledge of Access, the client wants this subform for entry of multiple lines into the main table. So the process I was thinking was to have this temporary subform/table, and then copy the lines into the main table. I guess, perhaps my better option would be to somehow create a subform that only shows the matching ID entries for the primary keys. I believe I have the primary key in a hidden field on the parent form of the subform. So, should I even use a temporary table, that I think is probably a bad idea, or somehow crate a subform that shows the entries that match those clicked by the add button? Please ask if this sounds too confusing.

Thank You
Aug 5 '15 #7
To clarify a little better, each item added by a click would create a new entry in the main table, but the client wants all the same info to remain there in the parent form, as well as the list of items to add, before a final save of the record(s). Any idea of a way to do this? The big item is to have a subform to show the items being added, and then creating a new record in the main table after the save button is clicked.

Thanks.
Aug 5 '15 #8
I'm trying to explain to the client about linked tables, one to many relationships. Cross your fingers. I won't hear back for a while though. Sorry if I was driving anyone to say, "What's the point in using Access, when you can't use in a relational manner?" I just think the customer isn't that familiar, but has done a little work in Access, yet is using me for the final product work.
Aug 5 '15 #9
A problem I would have is converting the existing single entries table into the new table, with a relationsal field to the full gift table. Anywhere I could find out about writing a routine to do the conversion? I'm guessing it's a routine I need to write and do once.

Thank You
Aug 5 '15 #10
jforbes
1,107 Expert 1GB
If you are wanting a Subform (Continuous Form/Grid) to enter information, here are some things to consider:

Types of Controls

Trying to use Unbound Controls in a Datasheet (Grid) wont work because access will consider each control in a column the same control. It's odd, but it's the way it works. With unbound controls, anytime the value in a column is changed, it changes for every row in the column (only in a Datasheet)... so you'll need to use Bound Controls.

When using Bound Controls, Access will save a record when an edit is performed and then the Current Record is Changed, so clicking around in a Datasheet (Grid) and modifying data will cause the records to be saved.

Since what your Customer wants is basically a Posting process, you have two options. You could create the records for them to edit in a Temp Table and then when they Post the Changes, copy the records over to the Live Table. Or, create the new records in the Live Table with a flag to differentiate Temp records from Live records (Unposted vs Posted records), then when the user chooses to accept the changes, set the flag to Posted.

Personally, I'm a fan of the second option, a flag to Post records. The database has less of a chance of growing unnecessarily as temp records aren't created (as much). But it will add a little bit more complexity to your Forms, Queries and Report as you will have to take this flag into account. On the flip side, sometimes you just wanna keep it simple and using Temp Tables does this.

Once you decide which way you want to go, the code will sorta write itself. If using Temp Tables, you'll need a SQL INSERT into your Live Tables from the Temp Tables of all your recently added records, then a SQL DELETE to clean up the Temp Tables (which can be put into the Form Open). If you go with Posting, then you'll need a SQL UPDATE query to post the Temp Rows.
Aug 5 '15 #11

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

Similar topics

7
by: wwwords | last post by:
I have a form in Access 2007 containing a subform. The control source of the latter is SELECT DISTINCTROW ., ., ., ., . FROM ; which is undoubtedly correct. However, if I click on the down...
1
by: MLH | last post by:
If the RecordSource for subform SF on main form MF is a query and I change one of the field values in the subform control from 75 to say - 13 and click on another record, the value in the...
3
by: IntelliOfficer | last post by:
The data I am using was imported from Excel. The tables were then merged into one large table (3 million + records) and so cannot be re-exported into excel for modification. I am trying to map the...
0
by: WPW07 | last post by:
Hello Everyone, I stumbled onto something interesting and was wondering if anyone else has experienced it and how they fixed it. I've posted to several forums to no avail. I've got a...
3
by: ramab | last post by:
Hi, im trying to merge 2 databases having the same structure. How do i merge the two databases which have unique identifiers and auto numbers. e.g the first 10 records in table A , it will have auto...
4
by: hausj0dw | last post by:
How can I rermove the sort option on columns displayed on a form. This database is access 2003, some users have access 2007. The 2007 users want the sort option removed.
4
by: netnewbie78 | last post by:
Hello All, I don't have a problem (but maybe I will after I explain). I have a question with regards to something I saw in Access 2007. But first, a little backstory: I'm writing a very small...
1
by: Cathy Hosek | last post by:
Hi all, I have an ODBC linked table with a 3-field combo key. I am trying to link to a local table using the relationships window. I can drag the first line fine, but when I try to drag the...
2
by: sebair | last post by:
My main form contains as tab control. The first tab contains nothing but a suform whose ControlSource is a select query. When the form is opened the results of the query are displayed in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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...
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.