473,418 Members | 2,340 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,418 software developers and data experts.

From ListBox to Subform -- , how to add SQL statement?

Hi All,

I have a listbox on a main form. When user double-clicks an item in
the list box, I move the selected values to a subform at the bottom of
the form. List box is not multiselect, and subform is continuous form.
This is a work-in-progress, but seems to be working ok.

On my subform I have a sequence number field. When user selects from
the listbox, and the values are moved to the subform, I need to
calculate the sequence number for this new record based on values in
its table. (The table is one of the tables in the record source query
for the subform). I have set up an SQL string to do this, but not sure
how to use it. My question is: how can I use SQL to reference a table
within my List Box double-click event logic?

My List Box double-click event is:
Dim ctl As Control
Dim itm As Variant
Set ctl = Me![lstFurnListItems]
Me.fsubFurniture.SetFocus
DoCmd.GoToRecord , , acNewRec

For Each itm In ctl.ItemsSelected
If Not IsNull(itm) Then
Me!fsubFurniture!RoomID = txtRoomID.Value
Me!fsubFurniture!ItemID = ctl.Column(0, itm)
End If
Next

I want to have SQL something like:
Dim strSql As String
strSql = "SELECT tblRoomItems.SeqNo " _
& " FROM tblRoomItems " _
& " WHERE (((tblRoomItems.RoomID) = '" & txtRoomID.Value & "')"
_
& " AND ((tblRoomItems.ItemID) = " & ctl.Column(0, itm) & "));"

I tried doing something like the following, but it didn't work:
Dim recset As Recordset
Set recset = CurrentDb.OpenRecordset(strSql)

Me!fsubFurniture!SeqNo = tblRoomItems.SeqNo + 1

Appreciate any suggestions!
Thanks,
Lori

Dec 7 '05 #1
5 2581
I'm baffled as to why anyone would want to you what you are proposing.
Why not just put a combobox in the subform? If you're going to use a
Multi-select listbox, that's one thing, because then you can select a
big chunk of records, click a button, and add them to a subform's
rowsource.

But to answer your question, you use the ItemsSelected collection of
the listbox. IF you look up "listbox" at www.mvps.org/access, there's
code that does what you want.

Dec 7 '05 #2
Thank you - you are absolutely right - I do need to have my listbox be
Multi-Select! However, I want the selected records from the list box
moved to the subform first, and for each line, the user needs to add
additional data. Is this ok?

I had a look at the mvps.org web site (thanks for that), and couldn't
find the answer to my original question. (did a search on listboxes and
looked through the results). To reiterate, for each line of data moved
from the listbox to the subform, I need to get the max sequence number
for a given field combination on the table. I was not sure how to do
the SQL, or where to put it within my form's VBA. I could also get the
max seq no from the subform, if I knew how to look through all the rows
on there...

Thank you,
Lori

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #3
I'm still somewhat baffled as to why you'd use a listbox here, but it's
your application, not mine.

Here's the code for looping through selected items in a listbox:
http://www.mvps.org/access/forms/frm0007.htm

Getting the next sequence number, use DMAX() then use DMAX()+1 to get
your next one.

If you have a listbox, it has an ItemsSelected property

Dec 8 '05 #4
Nope, you can't grab the next sequence number like you can in Oracle...
(drat!)
You can do something like this...
Say you have a table

CREATE TABLE MyTable(
MyIDKey LONG PRIMARY KEY,
MyText TEXT(50),
....)

You can increment the MyIDKey field in the form's BeforeInsert event,
and set it to something like this:

Me.MyIDKey = DMax("[MyPK]", "MyTable")+1

Hope this helps.

Dec 9 '05 #5
Hi,
The DMAX you originally suggested works fine for my sequence number.

The reason I'm using a listbox is because I am querying a table for
inventory items and may potentially have many items listed. I want the
user to be able to view them, scroll through them, and then select the
item(s) he wants to 'assign' to a room. I just think a list box is
easier viewing for this sort of thing. Does that make sense?

I liked your suggestion to have it a multi-select list box. However,
when I changed it to multi-select, only the last item selectedfrom the
listbox gets added to my subform. I am starting to think what I want
cannot be done with a multi-select: user selects chunk of records,
they all get moved to subform but are incomplete (no INSERT yet), user
enters required information for each record on subform, and THEN record
gets added to table. Can a chunk of records be added to subform
without being saved one at a time? I can't save until user enters data,
because record would be incomplete.

Thanks for your help.

Dec 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Koen | last post by:
Hi, I have three objects in my form: a listbox (lstform) a subform (fsub) a txtbox (txt) The listbox lists form names that will be displayed in the subform. To do this I use the...
3
by: DD | last post by:
I have a mainform with a subform. > The main form has a dropdown box "chooseMonth", in the afterupdate event > i requery the subform so all records with the same date are viewed. > Now i only want...
4
by: Alienz | last post by:
I have a subform where I have a subform with 20 options to select from. When I set the multiselect property to simple and select multiple options, nothing is stored. I have another table with...
2
by: Simon P | last post by:
Hello group, I'm in desperate need of help. Here goes : I have the following tables : CONTACTS (ContactID, FirstName, LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and SHOWDETAILS...
1
by: John M | last post by:
I have 1st list box from table Grain 2nd list box from related table Varieties, related by GrainID At the moment Grain listbox is on its own form and Variety listbox is on subform in Grain form....
2
by: Hey_Moe! | last post by:
I have subform which uses a SQL statement as the record source. One of the criteria (ie. Part of the WHERE condition) is a column value from a ListBox on the MainForm. Two problems exist: 1....
4
by: Corey | last post by:
I am having some real trouble getting a listbox rowsource to load properly. The listbox is actually on a subform. My main form has several command buttons that change the MASTER_SUB_FORM object...
3
by: colleen1980 | last post by:
There are two continuous subforms in main form. All are linked with ticketNum. When user comes to subform2. I need that Field3 of subform2 will automatically filled with field2 (field2 is a...
7
by: tonsam | last post by:
I am planning to create a form (Issuance) where all the records of the subform will be selected from the listbox. The listbox is not part of the main and subform but will only be visible upon...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.