Adding Records for each control

pas123

Registered User.
Local time
Today, 17:20
Joined
Mar 10, 2003
Messages
40
Is this the place to ask this???
I have a form that allows the user to select several items using option buttons. When a button is clicked a sub runs to fill hidden text boxes with the data to add to the table for each control in a tab within the form.
That all works fine.
What I want is to add a record to the table with the hidden data for each control in the tab.
Here is the sub if it helps.

Private Sub Command153_Click()
If Not [Text120].Value = "" Then
Me![dlDrawing Title].ControlSource = "Drawing List.Drawing Title" 'to reset ControlSource
Me![PDrawing Title].ControlSource = "Projects.Drawing Title" 'to reset ControlSource
End If
Dim tbc As Control, pge As Page
Dim ctl As Control
Set tbc = Me!TabCtl97
Set pge = tbc.Pages(tbc.Value)
For Each ctl In pge.Controls
If Not ctl.Value = "" Then
Me![Drawing Number].Value = ctl.Value
Me!PDrawing Title].Value = Me![dlDrawing Title].Value
End If
Next ctl
End Sub

Thanks
Paul
 
Last edited:
I'm having trouble understanding what you want to do. Never mind the code. Can you revisit this in English?
 
Drawing table:
[Drawing Number]
Drawing Title]

Projects table:
[Project number]
[Project Name]
[Drawing Number] Linked to above
[Drawing Title]

I have a form that allows a user to enter a Project Number and Project Name then select (option buttons) each Drawing Number for the project entered. When either "Add New Project" button or "Close" button is clicked I need to add a record to the Projects table for each Drawing Number selected. Each recoed should have the Project Number, Project Name, Drawing Number, and Drawing Title.

Now I've set up a tab ctl page to hold the Drawing Number values when the option button is clicked.

So when the "Close" button is clicked the "For Each" statement causes a hidden field (Drawing Number) to populate with the value of the control in the tab ctl page. At the same time annother hidden field (Drawing Title) populates with the the Drawing Title assoicated with the Drawing Number.
After all this I end up with 4 fields to add to the table for each control in the tab ctl page that is not empty.

hope this helps
Paul
 
pas: It looks like you might need to back up a step and normalize your tables. Right off the bat, you're storing the same info at least twice by having duplicate [Drawing Title] fields. Once your tables are in good shape it will be easier to accomplish everything else.

--Normal Mac
 
directormac,
It all works fine.
It's just that I can't figure out how to add records with-in the "For Each" statement.
ie.
Dim tbc As Control, pge As Page
Dim ctl As Control
Dim dbs As Database, rst As Recordset
Set tbc = Me!TabCtl97
Set pge = tbc.Pages(tbc.Value)
For Each ctl In pge.Controls
If Not ctl.Value = "" Then
Me![Drawing Number].Value = ctl.Value
Forms![Supervisor Menu]![PDrawing Title].Value = Forms![Supervisor Menu]![dlDrawing Title].Value
DoCmd.GoToRecord , , acNewRec
End If
Next ctl
Thanks
Paul
 
pas123 said:
DoCmd.GoToRecord , , acNewRec
I think the "goto" methods are strictly for navigation. So, go to the new record as above, THEN populate the controls, then save the record.

Personally, I would build an SQL statement to add the record and use the DoCmd.RunSQL method instead:
Code:
dim strSQL as String

[I]... then, in the for loop ...[/I]

strSQL = "INSERT INTO tblProjects VALUES (" &_
   _ & Forms![Supervisor Menu]![txtProjectNumber].Value & ", "
   _ & Forms![Supervisor Menu]![txtProjectName].Value & ", "
   _ & ctl.Value & ", " &
   _ & Forms![Supervisor Menu]![dlDrawing Title].Value & ");"
DoCmd.RunSQL (strSQL)
... or something like that. I made up the control names. Note that if your Project Number is an autonum (as it should be), the above will have to be modified. Search the forums.

pas123 said:
It all works fine.
So does using a wire coat hanger to hold the muffler on my '81 Chevelle. That doesn't mean it's the best way... Take it from soneone who has spent way too much time trying to modify some kludge that I originally used 'cause "well, it works fine for this..."

--Sadder-but-Wiser Mac
 
Last edited:
I agree with directormac about the normalization. What comes to mind is the old Fram Oil Filter commercial where the mechanic says, "You can pay me now, or pay me later." And, remember, that if you pay later it will be at a higher cost.

If you never work on this thing again and "it works" then you can probably get away with not normalizing. But, if you are going to keep working on this, enhancing, or having new requirements thrust upon you, the only way to go is to do it right the first time around.
 
Ok I give......:)
How about a good intro to dabase design? Website or book.
I'm rethinking how I set up what I'm trying to accomplish.

Thanks
Paul
 
Here's an exerpt from a posting by Pat Hartman (one of the parts of the book she is or was writing). Since the thread has so many posts, I felt it would serve you better to just post the part that was applicable instead of making you wade through it. However, that being said, here is the link, in case you do:

http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=8864&perpage=15&pagenumber=7

FROM PAT HARTMAN:
Here's a little on Normalization. Comments welcome. You may send them to my email.

Data Normalization
Normalizing a database is considered by most people to be more art than science. Some people will need to work harder to understand the concepts and to help you, I’ll include several detailed examples of common business objects. Understanding why you need to normalize your data structures is imperative.

The actual process of normalization is iterative. There are three basic forms that are described in more detail later in this section plus two more advanced forms that are beyond the scope of this book. Once you have gathered your requirements and documented your business rules, you need to gather into logical groupings all the data items that your analysis has uncovered.

Normalization is the process of removing unwanted functional dependencies from your application’s entities (tables). A functional dependency is implied if the value of an attribute can be determined by simply knowing the value of another attribute. For example, if we know a student’s Student Id, we can determine his name and address; therefore there is a functional dependency between a Student Id and the student’s informational attributes.

First Normal Form (1NF)
Eliminate repeating groups and separate all non-atomic attributes.

Repeating groups are sets of fields that frequently include some attribute of data as part of their field names. Obvious ones are attributes such as JanPayment, FebPayment, MarPayment, … DecPayment. Notice how the word “Payment” repeats. Each time it repeats it is prefixed with a piece of data. In this example the piece of data is the month in which the payment was made. Sometimes numeric suffixes such as Contact1 and Contact2 will be used. This is also a repeating group. In this case the suffix isn’t really data related to the contact; it simply provides a way of assigning a unique name to the individual items of a repeating group.

The most difficult type of repeating group to identify is one where no part of the name repeats. An example of this would be a group of fields that are used to hold different amounts related to a rental property. The field names might be; Insurance, LawnMaintenance, Electricity, Water, Sewage, etc. If you think about the purpose of these attributes, you will see that they are all types of expenses and if you wanted, you could add a common suffix such as “Expense” and the fact that these attributes form a repeating group will become apparent.

The creation of repeating groups is one of the most common mistakes made by people designing their first database. It looks like it should be so much easier to just make a bunch of fields to hold these various attributes than to define a separate table to hold them. The problems only start to surface later on in the development phase when the user discovers additional members of the repeating groups and realizes that the tables, forms, reports, and queries all need to be changed to add new expense types or additional contacts or perhaps a second year’s worth of payments. Sometimes the problems show up when certain types of calculations or analytical reports need to be created. We’ll talk more about these problems later when we get to working with the examples.

Non-atomic fields are another common problem area. Many systems are originally designed to use a single field to hold multiple pieces of information such as one field that holds first, middle, and last names. Why create individual fields when the user says he doesn’t need them? The answer is that users do not, nor should they be expected to understand the processing that needs to go on behind the scenes. If you need to sort the non-atomic name field by surname, you’ll need to write VBA code to separate the surname from the rest of the parts of a name. Or you may need to separate the name parts because there is a new requirement to send letters and you want to address them properly - Dear Mr. Jones. The code to separate the parts can be quite complex and will probably not perform the separation accurately in all cases.

Sometimes non-atomic fields evolve over time. Your sales department now wants to track the job titles for your customers. Rather than add a new column to the table you (or they) decide to just add the job title to the name field so that now it looks like “Mary Jane Smith President”. That just complicates your parsing routine even more.

_______________________________________
So, there's a starting point for you.
 
PAT: How's that book coming, BTW? I've been looking forward to it for ever so long...

--Anxious Mac
 
Pat Hartman said:
If anyone who lives in Connecticut or near enough for face-to-face visits wants to collaborate, let me know.

I live in NYC, couldn't POSSIBLY add anything in terms of knowledge that you don't already have, but would be happy to help in any way I could. Proofreader, feedback, guinea pig, whatever you need...

In terms of an approach, I haven't read that many Access books, but then I haven't READ that many books on any piece of software because 99% seem to be rephrasings of the manuals. One genre that has broken away from that, to a large extent, is 3D graphics. They tend to focus more on "how to achieve this desired non-obvious result..." Which is also how we tend to do things around the forums (minus, all to often, the 'non-obvious part'). Could that be your way "in"?

--Suggestive Mac
 

Users who are viewing this thread

Back
Top Bottom