Populate Data Entry Form through listbox

B

Brigitte P

Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
N

Nikos Yannacopoulos

Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos
 
B

Brigitte P

I'll try this today when I'm at work. I understand that records are in
tables and what you say makes a lot of sense. I will adapt the code to the
names in my tables, but probably need a little more help once I get started.
So please stay with me in this link. I should be back in about 4 hours or
so. Until then, thanks.
Brigitte
Nikos Yannacopoulos said:
Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte said:
Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
N

Nikos Yannacopoulos

Brigitte,

I'lll certainly watch this thread, but chances are I'll be off work for
the weekend by the time you get back, I'm 2 hrs ahead of GMT. Will check
again Monday morning.

Nikos

Brigitte said:
I'll try this today when I'm at work. I understand that records are in
tables and what you say makes a lot of sense. I will adapt the code to the
names in my tables, but probably need a little more help once I get started.
So please stay with me in this link. I should be back in about 4 hours or
so. Until then, thanks.
Brigitte
Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte said:
Can I populate a data entry form via a multiselect list box? Example,
user
selects 5 items from listbox and these values then would populate 5
records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the
unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be

completed in
Null."

something
 
B

Brigitte P

Nikos,
I tried and failed. I think what's needed is far beyond my skill level. I
worked on this problem for 3 days and some sleepless nights and tonight it
came to me. What I want to accomplish is easily done via 5 small queries
that run with macros and a little code. Just for others, here is what I did:
I'm creating an Infection Control database for our hospital. Patient has
symptoms, culture is taken, infection diagnoses, and medication is
prescribed depending on whether the offending bacteria is sensitive to it or
resistant. Of course, there are a few twists to it, but they are irrelevant
to the task on hand.
Tables Infections, Culture, Prescription and Sensitivity (Bacteria to
medication) are linked by an Infection IDCode with one to many
relationships.
The issue is that we have 30 Medications (and this list will grow) that are
resistant or sensitive to the medication. The user based on the culture has
to select from the 30 + medications and add to each the R or S or nothing,
depending. I looked at the listbox solution so the user could just select
from the 30, write the R or S in an unbound box, Infection ID and Bacteria
would be read from the Culture form, and all would have been automatically
updated via a form (so she could see it). I cannot do this.
What I have done now:
Created TempTable from the Lookup table for the 30 meds but added a field
for sensitivity level (User can add meds as needed to the Lookup table as
needed)
Append Query: will populate the TempTable with all 30 (or whatever future
number) meds (ID, Description,Level)
Select Query: Opens TempTable which now shows all 30 substances and user can
type S or R in Level field (has value list limited to values).
Append Query: Which has the values from above Select Query and fields added
for Infection ID and Bacteria read from Culture form. The receiving table
has an AutoNumber field as UniqueID which solves that problem.
Delete Query: To clear value from TempTable so it can be used (this actually
runs before the Append Query so we always start out clean).

The delete, append, select query run via a command button which runs the
macro for these 3 queries (was easier than code for me).
When User has entered data, a command button runs the second append query.
Finally, user has a command button to check the results (or results for
previous records when it is selected). Criteria in query is set to values
from current Culture form.
All these queries take only seconds because we deal with a very, very small
data set.
This is probably boring for you, but maybe it helps someone else to avoid my
struggle.
Thanks for your attempt to help, but my VBA is too elementary for the more
complex work that you experts are doing.
Brigitte P.
Nikos Yannacopoulos said:
Brigitte,

I'lll certainly watch this thread, but chances are I'll be off work for
the weekend by the time you get back, I'm 2 hrs ahead of GMT. Will check
again Monday morning.

Nikos

Brigitte said:
I'll try this today when I'm at work. I understand that records are in
tables and what you say makes a lot of sense. I will adapt the code to the
names in my tables, but probably need a little more help once I get started.
So please stay with me in this link. I should be back in about 4 hours or
so. Until then, thanks.
Brigitte
Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte P wrote:

Can I populate a data entry form via a multiselect list box? Example,
user

selects 5 items from listbox and these values then would populate 5
records

on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the
unique

index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be

completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be
Null."

Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because
something

was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
N

Nikos Yannacopoulos

Brigitte,

Glad you found a working solution, even though your original approach
was much neater. If you have the time at some point, I strongly
recommend you give it another shot (and post for help with specific
questions); I know code looks scary at first (took me years to start
playing around with it) but once you start you soon realize it opens up
a whole new world!

Nikos

Brigitte said:
Nikos,
I tried and failed. I think what's needed is far beyond my skill level. I
worked on this problem for 3 days and some sleepless nights and tonight it
came to me. What I want to accomplish is easily done via 5 small queries
that run with macros and a little code. Just for others, here is what I did:
I'm creating an Infection Control database for our hospital. Patient has
symptoms, culture is taken, infection diagnoses, and medication is
prescribed depending on whether the offending bacteria is sensitive to it or
resistant. Of course, there are a few twists to it, but they are irrelevant
to the task on hand.
Tables Infections, Culture, Prescription and Sensitivity (Bacteria to
medication) are linked by an Infection IDCode with one to many
relationships.
The issue is that we have 30 Medications (and this list will grow) that are
resistant or sensitive to the medication. The user based on the culture has
to select from the 30 + medications and add to each the R or S or nothing,
depending. I looked at the listbox solution so the user could just select
from the 30, write the R or S in an unbound box, Infection ID and Bacteria
would be read from the Culture form, and all would have been automatically
updated via a form (so she could see it). I cannot do this.
What I have done now:
Created TempTable from the Lookup table for the 30 meds but added a field
for sensitivity level (User can add meds as needed to the Lookup table as
needed)
Append Query: will populate the TempTable with all 30 (or whatever future
number) meds (ID, Description,Level)
Select Query: Opens TempTable which now shows all 30 substances and user can
type S or R in Level field (has value list limited to values).
Append Query: Which has the values from above Select Query and fields added
for Infection ID and Bacteria read from Culture form. The receiving table
has an AutoNumber field as UniqueID which solves that problem.
Delete Query: To clear value from TempTable so it can be used (this actually
runs before the Append Query so we always start out clean).

The delete, append, select query run via a command button which runs the
macro for these 3 queries (was easier than code for me).
When User has entered data, a command button runs the second append query.
Finally, user has a command button to check the results (or results for
previous records when it is selected). Criteria in query is set to values
from current Culture form.
All these queries take only seconds because we deal with a very, very small
data set.
This is probably boring for you, but maybe it helps someone else to avoid my
struggle.
Thanks for your attempt to help, but my VBA is too elementary for the more
complex work that you experts are doing.
Brigitte P.
Brigitte,

I'lll certainly watch this thread, but chances are I'll be off work for
the weekend by the time you get back, I'm 2 hrs ahead of GMT. Will check
again Monday morning.

Nikos

Brigitte said:
I'll try this today when I'm at work. I understand that records are in
tables and what you say makes a lot of sense. I will adapt the code to
the
names in my tables, but probably need a little more help once I get
started.
So please stay with me in this link. I should be back in about 4 hours
or
so. Until then, thanks.
Brigitte


Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte P wrote:


Can I populate a data entry form via a multiselect list box? Example,

user


selects 5 items from listbox and these values then would populate 5

records


on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the

unique


index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be

completed in


the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be

Null."


Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because

something


was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
G

Guest

Nikos,

I have a similar problem to this. I want to add new records to a table
based on the selection of a list box. But I want each listbox record to
include a textbox value on the form and a combobox value on the form, along
with each value of the list box. For example:

Textbox1 Value = ABC
Combobox Value = 123
Listbox Selections = X,Y,Z

So 3 new records would get entered into the table as the following:

ABC 123 X
ABC 123 Y
ABC 123 Z

Also, since I am in the Access environment I don't think I have to dim the
database, correct?

Any help on this is approciated

Nikos Yannacopoulos said:
Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte said:
Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
G

Guest

Nikos,
I have another question. I am using Access 2003 version. Your code uses
the DAO format. Access 2003 uses the ADO Format. I tried to make the
coversion of your code to ADO format but was unsuccessful. I have been
trying to get the DAO Library referenced in my Access VB Code version but I
cannot tell how to do it. I know how to do it in the strainght VB 6.0
Developer Studio. Can you either tell me how to reference it or conver your
code sample to ADO Format? Thanks...

cyberarf said:
Nikos,

I have a similar problem to this. I want to add new records to a table
based on the selection of a list box. But I want each listbox record to
include a textbox value on the form and a combobox value on the form, along
with each value of the list box. For example:

Textbox1 Value = ABC
Combobox Value = 123
Listbox Selections = X,Y,Z

So 3 new records would get entered into the table as the following:

ABC 123 X
ABC 123 Y
ABC 123 Z

Also, since I am in the Access environment I don't think I have to dim the
database, correct?

Any help on this is approciated

Nikos Yannacopoulos said:
Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte said:
Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
N

Nikos Yannacopoulos

Your code would be something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields("field1") = Me.TextBox1
rst.Fields("field2") = Me.Combobox1
rst.Fields("field3") = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The reason why I dim and set the database object is so I can then reset
it, to make sure it isn't left open.

Note: To run this code, it is required to have an appropriate DAO Object
Library reference. While in the VB editor window, go to menu item Tools
References; check if a Microsoft DAO reference is present among the
ones checked at the top of the list. If not, scroll down to find the
appropriate Microsoft DAO X.X Object Library reference and check it. The
appropriate reference is DAO 3.51 for A97, DAO 3.6 for A2K or later.

HTH,
Nikos
Nikos,

I have a similar problem to this. I want to add new records to a table
based on the selection of a list box. But I want each listbox record to
include a textbox value on the form and a combobox value on the form, along
with each value of the list box. For example:

Textbox1 Value = ABC
Combobox Value = 123
Listbox Selections = X,Y,Z

So 3 new records would get entered into the table as the following:

ABC 123 X
ABC 123 Y
ABC 123 Z

Also, since I am in the Access environment I don't think I have to dim the
database, correct?

Any help on this is approciated

:

Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte said:
Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
N

Nikos Yannacopoulos

See answer to your previous post.
Nikos,
I have another question. I am using Access 2003 version. Your code uses
the DAO format. Access 2003 uses the ADO Format. I tried to make the
coversion of your code to ADO format but was unsuccessful. I have been
trying to get the DAO Library referenced in my Access VB Code version but I
cannot tell how to do it. I know how to do it in the strainght VB 6.0
Developer Studio. Can you either tell me how to reference it or conver your
code sample to ADO Format? Thanks...

:

Nikos,

I have a similar problem to this. I want to add new records to a table
based on the selection of a list box. But I want each listbox record to
include a textbox value on the form and a combobox value on the form, along
with each value of the list box. For example:

Textbox1 Value = ABC
Combobox Value = 123
Listbox Selections = X,Y,Z

So 3 new records would get entered into the table as the following:

ABC 123 X
ABC 123 Y
ABC 123 Z

Also, since I am in the Access environment I don't think I have to dim the
database, correct?

Any help on this is approciated

:

Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte P wrote:

Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
G

Guest

Nikos,

Your code solution worked as soon as I loaded up the DAO Reference Library.
Thanks for your help, Al

Nikos Yannacopoulos said:
Your code would be something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields("field1") = Me.TextBox1
rst.Fields("field2") = Me.Combobox1
rst.Fields("field3") = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The reason why I dim and set the database object is so I can then reset
it, to make sure it isn't left open.

Note: To run this code, it is required to have an appropriate DAO Object
Library reference. While in the VB editor window, go to menu item Tools
References; check if a Microsoft DAO reference is present among the
ones checked at the top of the list. If not, scroll down to find the
appropriate Microsoft DAO X.X Object Library reference and check it. The
appropriate reference is DAO 3.51 for A97, DAO 3.6 for A2K or later.

HTH,
Nikos
Nikos,

I have a similar problem to this. I want to add new records to a table
based on the selection of a list box. But I want each listbox record to
include a textbox value on the form and a combobox value on the form, along
with each value of the list box. For example:

Textbox1 Value = ABC
Combobox Value = 123
Listbox Selections = X,Y,Z

So 3 new records would get entered into the table as the following:

ABC 123 X
ABC 123 Y
ABC 123 Z

Also, since I am in the Access environment I don't think I have to dim the
database, correct?

Any help on this is approciated

:

Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte P wrote:

Can I populate a data entry form via a multiselect list box? Example, user
selects 5 items from listbox and these values then would populate 5 records
on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the unique
index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be completed in
the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be Null."
Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because something
was strang in my email program).
As always, many thank for your help.
Brigitte P.
 
B

Brigitte P

I agree that the original solution would be neater and that code is a better
solution than multiple queries. But my coding skills are minimal; I may
return to this at another time because I seem to remember when I have done
something once. I can set up a multi-select listbox to select data for
reporting, but the code I tried (with your help) told me that it didn't
recognize the form. The table also does a UniqueID added via a Autonumber,
and I think that's where the trouble began. Anyway, at this point, thanks.
Brigitte
Nikos Yannacopoulos said:
Brigitte,

Glad you found a working solution, even though your original approach
was much neater. If you have the time at some point, I strongly
recommend you give it another shot (and post for help with specific
questions); I know code looks scary at first (took me years to start
playing around with it) but once you start you soon realize it opens up
a whole new world!

Nikos

Brigitte said:
Nikos,
I tried and failed. I think what's needed is far beyond my skill level. I
worked on this problem for 3 days and some sleepless nights and tonight it
came to me. What I want to accomplish is easily done via 5 small queries
that run with macros and a little code. Just for others, here is what I did:
I'm creating an Infection Control database for our hospital. Patient has
symptoms, culture is taken, infection diagnoses, and medication is
prescribed depending on whether the offending bacteria is sensitive to it or
resistant. Of course, there are a few twists to it, but they are irrelevant
to the task on hand.
Tables Infections, Culture, Prescription and Sensitivity (Bacteria to
medication) are linked by an Infection IDCode with one to many
relationships.
The issue is that we have 30 Medications (and this list will grow) that are
resistant or sensitive to the medication. The user based on the culture has
to select from the 30 + medications and add to each the R or S or nothing,
depending. I looked at the listbox solution so the user could just select
from the 30, write the R or S in an unbound box, Infection ID and Bacteria
would be read from the Culture form, and all would have been automatically
updated via a form (so she could see it). I cannot do this.
What I have done now:
Created TempTable from the Lookup table for the 30 meds but added a field
for sensitivity level (User can add meds as needed to the Lookup table as
needed)
Append Query: will populate the TempTable with all 30 (or whatever future
number) meds (ID, Description,Level)
Select Query: Opens TempTable which now shows all 30 substances and user can
type S or R in Level field (has value list limited to values).
Append Query: Which has the values from above Select Query and fields added
for Infection ID and Bacteria read from Culture form. The receiving table
has an AutoNumber field as UniqueID which solves that problem.
Delete Query: To clear value from TempTable so it can be used (this actually
runs before the Append Query so we always start out clean).

The delete, append, select query run via a command button which runs the
macro for these 3 queries (was easier than code for me).
When User has entered data, a command button runs the second append query.
Finally, user has a command button to check the results (or results for
previous records when it is selected). Criteria in query is set to values
from current Culture form.
All these queries take only seconds because we deal with a very, very small
data set.
This is probably boring for you, but maybe it helps someone else to avoid my
struggle.
Thanks for your attempt to help, but my VBA is too elementary for the more
complex work that you experts are doing.
Brigitte P.
Brigitte,

I'lll certainly watch this thread, but chances are I'll be off work for
the weekend by the time you get back, I'm 2 hrs ahead of GMT. Will check
again Monday morning.

Nikos

Brigitte P wrote:

I'll try this today when I'm at work. I understand that records are in
tables and what you say makes a lot of sense. I will adapt the code to
the

names in my tables, but probably need a little more help once I get
started.

So please stay with me in this link. I should be back in about 4 hours
or

so. Until then, thanks.
Brigitte


Brigitte,

To read the rows selected in a multi-select listbox, you need to resort
to VBA code; no other option. Doing that, it's easy to use a recordset
operation to create new records in the table being the form's
recordsource (records live in tables, not in forms). Your code could
look something like:

Dim db as DAO.Database
Dim rst as DAO.Recordset

set db = CurrentDb
set rst = db.OpenRecordset("TableName")
'or set rst = Forms![DataEntryForm].RecordsetClone
For Each itm in Me.ListboxName.ItemsSelected
rst.AddNew
rst.Fields(0) = Me.ListBoxName.ItemData(itm)
rst.Update
Next
rst.Close
set rst = Nothing
Set db = Nothing

The code above is purely indicative, based on assumptions... it won't
work as is, needs to be adjusted to your design. It also requires an
appropriate DAO reference.

HTH,
Nikos

Brigitte P wrote:


Can I populate a data entry form via a multiselect list box? Example,

user


selects 5 items from listbox and these values then would populate 5

records


on a data entry form with the values from column 1 in the listbox.
Listbox would look like this
1 Item A
2 Item B
3 Item C
The data entry (continous form) would be populated with whatever the

unique


index is and then
Record 1 1
Record 2 2 an so forth (first column value from the list box).
Then the user can click on any remaining fields that need to be

completed in


the continous form.
I read in help: "In addition, when the MultiSelect property is set to
Extended or Simple, the value of the list box control will always be

Null."


Will this prevent me from doing this or is there a work around?
I'm still working on the problem I previously posted, trying to use a
spreadsheet solution (and I stupidly posted several times because

something


was strang in my email program).
As always, many thank for your help.
Brigitte P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top