copying data from one table to another via command button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm building a database for my office to track certain tasks.

In tblMaster there are fields for task name, date, total time, present
personnel, sites involved and notes

In tblPersonnel there is a field with all available office personnel

In tblSites there are two fields, one which has the sites name, and one
which has a single character site identifier.

I've built a frmMaster which has input fields for task name, date & total
time and notes. I've created a list box on frmMaster which pulls the names
from tblPersonnel.

Basically what I want to do is to be able to CTRL+Click all the personnel
from the list box, and click a command button which will copy those names
(perhaps seperated by a comma) into the present personnel field on tblMaster.
HOWEVER, I am not savvy enough to know how to properly code the command
button to do the desired function...ANY help will be GREATLY appreciated.

Thanks,
Chris
 
Basically what I want to do is to be able to CTRL+Click all the
personnel from the list box, and click a command button which will
copy those names

Okay: getting a list of the selected items out of a list box is easy.
Look up help on the Selected() property of the list box.

(perhaps seperated by a comma) into the present
personnel field on tblMaster.


This, however, is a Big Problem. You really should not be trying to store
lists in single fields: you need a new table to manage this relationship
properly. Try something like (sorry about renaming, I don't do Hungarian
when it comes to user objects):

Tasks(*TaskNum, TaskName, TotalTime, etc etc)

Personnels(*PersonID, FName, LName, etc etc)

Sites(*SiteCode, Address, PhoneNumber etc)

and then for the relationships:

IsPresentAt(*PersonID, *TaskNum, HowLongPresentFor, etc)
Primary Key(PersonID, TaskNum)
FK PersonID references Personnels
FK TaskNum references Tasks

and I guess you also want

IsSitedAt(*SiteCode, *TaskNum, AreaInvolved, etc)
Primary Key(SiteCode, TaskNum)
FK SiteCode references Sites
FK TaskNum references Tasks

HOWEVER, I am not savvy enough to know
how to properly code the command button to do the desired
function...ANY help will be GREATLY appreciated.

With a proper design, the command button becomes something like

' remove any existing present people from this task
' shouldn't be any errors, but trap them anyway
jetSQL = "DELETE FROM IsPresentAt WHERE TaskNum = " & me!tasknum
db.Execute jetSQL, dbFailOnError

' look through the list box
for i - 0 to lis.ListCount -1

' if the user wants this one, then do it
if lis.Selected(i) then

' create the command
jetSQL = "INSERT INTO IsPresentAt(PersonID, TaskNum) " & _
"VALUES ( " & lis.ListItem(i) & ", " & Me!TaskNum & ")"

' remove this once you know it's working okay
debug.assert vbyes=msgbox(jetSQL, vbYesNo, "Is this okay?")

' and run the command. Trap any errors although there should not
' be any
db.Execute jetSQL, dbFailOnError

' all done
end if
' get next row in the list
Next i




As usual: this is air code and not tested, but it should show you roughly
how to go.

B Wishes


Tim F
 
Hi Chris,

Welcome to the wonderful world of Access.Your questions/expressed
desires indicate that you are not yet familiar with Access under the
hood. Be warned, there is a longer and steeper learning curve in
Access than in the other MS Office applications.

There are several Access newsgroups and the most useful ones for
people learning the basics are: microsoft.public.access.gettingstarted
and microsoft.public.access.tablesdesign People who have mastered
Excel or who may have Access Step by Step may feel that those
newsgroups are beneath them. They aren't.

Tablesdesign is all about the organization of your data. That seems
so boring and uninteresting to people starting to learn Access.
However, if you stay with it you'll come to understand that the
characterization of the entities in play in your applaction (entities
are captured in TABLES) is crucial to creating a useful application.
If you already understand terms like "normalization" and know what
"third normal form" means without having to think about it then you
may not need to lurk tablesdesign. A couple of surprising facts you
should learn while lurking tablesdesign: Tables are cheap. You need
one table for each entity type. Don't try to cram several entities
together. Fields are expensive. Use as many fields as you need but
no more than that. When someone posts that they have a table of 60 or
100 fields it's wise to be suspicious of their data design. Get the
tables right and you can extend and enhance your application forever.
Get the tables wrong and your application soon becomes difficult or
impossible to enhance. Another thing is that data should never be
entered or massaged in tables directly by the user.

Gettingstarted expects that people posting questions there are humble
novices who are just venturing into the Access world. The people
answering posts tend to be appropriately considerate. That's actually
true of all of the Access newsgroups. Lurk the newsgroups to see what
others are posting and see if you can follow the initial problem and
what the responder(s) suggested to resolve the issue.

The first response to many posts will be something like: "You can't
do exactly what you asked, however, ...."

Limit yourself to one issue per thread. Once your initial issue is
resolved, don't piggyback a new issue onto the old one: :Oh, while
I've got you ..." Alsol, don't hijack someone else's thread. Start a
new thread for your new issues. Don't address your issue to a single
responder. some of us go for weeks at a time without checking in.

If you hope to make good progress in Access you'll be well advised to
visit your local bookstore. Browse several Access books and pick one
that seems to communicate to you. You'll be back again later for more
advanced stuff. :-) An incredibly valuable resource is
www.mvps.org/access It is filled with Access lore and continually
grows.

To your issues:

Name each table for the entity it represents. I doubt that "Main" or
"Master" is a good table name. Some candidate entity names: Person,
Site, Room, Job, Task, Order, Item, etc. Entities like Site or Color
are placed in regular tables but are treated specially as Lookup
Tables. You'll typically use a combobox to display all the values and
pick the one you want.

It seems that Task might be the thing your application is all about.

Your user interface (even if you're the only current user) should be
entirely via forms and reports. The forms and reports will be based
on queries. In time you may want to turn over the use of your
application to someone else. A good user interface will facilitate
that hand-off.

You won't need to CTRL-Click. The control you'll need in order to
associate just selected personnel with a given task is a Multi-Select
List Box that will list every person in tblPerson. Each person will
be individually selectable. In the OnClick event of the command
button you'll need to step through the index of the listbox and
concatenate the name of each selected person to the list in the textbo
x on your form...When you save your form, the field in the table will
be updated with the contents of the textbox.

HTH
 
Larry - Can I copy & save this? I don't think I've ever read as eloquent
of a response to newbies.
 
Hi David,

Thank you. Feel free to use, quote and copy :-)

I hope to encourage newbies to stay the course. They have to know
that they've started on something difficult and time consuming -
they're not stupid! There have been a few times when I've followed
someone else's slam with a letter of enlightenment and encouragement
and received thanks and a declaration of intent to stay the course. I
like the idea that the community of Access developers continues to
grow.

I've noticed your posts and am glad to see you chiming in.
 
Hard to believe that I created my first .mdb file nearly 10 years ago -
OUCH! using all macros. Now never touch the things.
 
THANKS a ton for the help Tim!!

I've re-designed the database, using the format you described, and its
working exactly the way I had envisioned it.

I still, however, am still having trouble getting the command button to
work. I can enter data exactly into the tables and have them portray the
correct data...to an extent. I can't get the PersonnelID from tblPersonnel to
reflect the FirstNames and LastName from tblPersonnel in the PresentPersonnel
field on tblTest.

Anyways, I've made a form (frmPrimaryTestData) which pulls TestID, TestName,
Date, TotalTime and notes from tblTest, it has a list box which pulls Rank,
FirstName, MiddleInitial and LastName from tblPersonnel and I have a text
field locked, because I only want it to portray information not be available
for input, but this is where I want the command button to come in. I want to
be able to select a person from the list box of available personnel, and
click the button and have it enter the persons PersonnelID (an autonumber
used to easier manage the personnel rather than by names and such) into the
PresentPersonnel field of tblTest, and I need to be able to enter multiple
persons into this field as most of our tests require at least 3 personnel to
be present.

The assistance I receive on this topic will be mirrored to resolve an
identical issue on the same database doing the same operation only it's
involved with the test sites.

THANKS in advance,
Chris
 
THANKS a ton for the help Tim!!

Glad to help
I can't get the PersonnelID
from tblPersonnel to reflect the FirstNames and LastName from
tblPersonnel in the PresentPersonnel field on tblTest.

I don't understand quite what you want here. I assume that tblTest is the
same as what I described as IsPresentAt (i.e. the list of all the people
who are present at the Tasks). You really don't want any people's names
in this table: when you want a list of people's names against their
tasks, you make a query that joins the (two or ) three tables together.

I want to be able to select a
person from the list box of available personnel, and click the button
and have it enter the persons PersonnelID (an autonumber used to
easier manage the personnel rather than by names and such) into the
PresentPersonnel field of tblTest,

Nonononono : see above. There is no harm in doing a DLookup, for example,
to display the name and stuff to the user, but you really DO NOT want to
start duplicating fields all over the place.

Try this:

strWhat = "Rank & "" "" & FirstName & "" "" & _
"(MiddleInitial + "" "") & LastName"
strTable = "tblPersonnel"
strWhere = "PersonnelID = " & Format(me!cboPerson.Value, "0000")

' forget about nulls, .Value is safe with them
txtPersonDetails.Value = DLookup(strWhat, strTable, strWhere)


and I need to be able to enter
multiple persons into this field as most of our tests require at least
3 personnel to be present.

Use a list box: that's what they are for. You can write the names into a
label or a text box with commas if you really want, but it's code you'll
have to write.
The assistance I receive on this topic will be mirrored to resolve an
identical issue on the same database doing the same operation only
it's involved with the test sites.

Yes -- we like reusable solutions!

Best wishes


Tim F
 
Back
Top