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

Multiselect Listbox

I have a table named
tbl_Questions
with a column called Region
I have a total of 8 regions

I am attempting to set an Unbound multiselect listbox on a form called frm_DH

I need the user to be able to select and deselect any combination of 8 regions and the record to show in the splitform datasheet and update the questions table.

So far I can select the data but it does not populate the qry_DH nor the tbl_Questions and fails to save once the form is closed.

here is my code:

[Code/]
Private Sub cmd_lst_Region_Click()
On Error GoTo PROC_ERR

'Declare items
Dim iItem As Integer
Dim Region As String
Dim db As DAO.Database
Dim rs As DAO.Recordset

' Save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If

' Open a Recordset based on the table
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_Questions", dbOpenDynaset)
With Me!lst_Region


' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
Region = .Column(0, iItem)

' Determine whether this combination is currently
' in the table
rs.FindFirst "[ID] = " & Forms!frm_DH!lst_Region & " ; " _
& "[Region] = " & lst_Region.AddItem

If rs.NoMatch Then

' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
rs!ID = Me.txt_ID
rs!lst_Region = lst_Region
rs.Update
End If

' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then

' delete this record if it's been deselected
rs.Delete
End If

' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing

PROC_EXIT:

Exit Sub

PROC_ERR:
MsgBox "Error " & Err.Number & " in cmdProcess_Click:" _
& vbCrLf & Err.Description
Resume PROC_EXIT

End Sub
[End_Code/]

I am sure I have messed this up royally in addition I am sure I am attempting to do the impossible.

thank you for any assistance and I hope I have offered enough information. I am using access 2007 at this time.
Dec 30 '10 #1
13 1821
ADezii
8,834 Expert 8TB
Unless I am misreading this Thread, you basic Logic for creating a String of Semi-Colon Delimited, Selected, Region(s) is way off. The following Code will generate the desired String from the Region(s) chosen:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As ListBox
  2. Dim varItm As Variant
  3. Dim strBuild As String      'To build Region(s) Selected List
  4.  
  5. Set ctl = Me![lst_Regions]
  6.  
  7. If ctl.ItemsSelected.Count < 1 Then Exit Sub
  8.  
  9. For Each varItm In ctl.ItemsSelected
  10.   strBuild = strBuild & ctl.ItemData(varItm) & ";"
  11. Next varItm
  12.  
  13. strBuild = Left$(strBuild, Len(strBuild) - 1)       'Strip Trailing ';'
  14.  
  15. Debug.Print strBuild
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Region1;Region5;Region8
  2. Region4
  3. Region2;Region4;Region6;Region7
  4. Region7;Region8
  5. etc...
Dec 30 '10 #2
ADezii

I am sure you are reading it correctly. I appreciate your assistance and will get back to it in the AM. I will post the outcome.

Thank you,
Dec 30 '10 #3
ADezii,

I appreciate your response and code snip, but this did not work for me.

I need the form to update the table and the information is not being updated or saved once I leave the form. As a novice to this I am unsure how else to explain it.

I will try again.

Table Name: tbl_questions
Query Name: qry_DH (Select Query)
Split-Form Name: frm_DH being fed by qry_DH
MultiSelect Listbox Name: lst_Regions (there are 8 Regions)

I need the listbox to populate the column region on the tbl_Questions using the frm_DH. If it could do it dynamically that would be a plus, but I would settle for it updating after I leave the form or after I press the save button or something. Right now my code nor the above snip is working. I can choose the items they just do not save or populate the table.

Any ideas?
Jan 3 '11 #4
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As ListBox
  2. Dim varItm As Variant
  3. Dim strBuild As String      'To build Region(s) Selected List
  4.  
  5. Set ctl = Me![lst_Regions]
  6.  
  7. If ctl.ItemsSelected.Count < 1 Then Exit Sub
  8.  
  9. For Each varItm In ctl.ItemsSelected
  10.   strBuild = strBuild & ctl.ItemData(varItm) & ";"
  11. Next varItm
  12.  
  13. strBuild = Left$(strBuild, Len(strBuild) - 1)       'Strip Trailing ';'
  14.  
  15. 'Does the String exist in tbl_Questions
  16. If DCount("*", "tbl_Questions", "[Region] = '" & strBuild & "'") > 0 Then       'Yep, exists
  17.   MsgBox strBuild & " alread exists in the [Region] Field of tbl_Questions", vbExclamation, "Duplicate Entry"
  18. Else
  19.   If IsNull(Me![txtID]) Then        'Must have an ID
  20.     MsgBox "You must enter an ID before the Record can be saved", vbExclamation, "Missing ID"
  21.       Me![txtID].SetFocus
  22.   Else      'Good to go!
  23.     CurrentDb.Execute "INSERT INTO tbl_Questions ([ID], [Region]) VALUES (" & Me![txtID] & _
  24.                       ", '" & strBuild & "')", dbFailOnError
  25.       MsgBox "Record Saved", vbInformation, "Save Record"
  26.   End If
  27. End If
P.S. - If this does not adequately explain things, I'll send you an Attachment to illustrate the concepts more clearly.
Jan 3 '11 #5
oh boy, okay - I am getting an error on the SQL INSERT INTO statement. I guess because the [ID] on tbl_questions is the primary key on that table. It is calling duplicate record and stating unable to update the table based on no duplicates allowed. Which makes perfect sense except now I royally confused on how to get the information into the table.

Maybe I am going about this all wrong. I was Attempting to keep only one table. Maybe a second table making the database one-to-many or something would be more appropriate?
Jan 3 '11 #6
ADezii
8,834 Expert 8TB
Brandi, why don't you Upload your Database, so that we can get a picture of exactly what is going on? If you are able to do this, just provide the bare essentials, and strip it of any confidential information.
Jan 3 '11 #7
ADezii,

I was thinking about that, but was unsure how involved you wanted to get. I am being blocked by my companies security rules. I have sent home the file and will upload it this evening for review. I have stripped down to example data and removed all company logos.

thank you and will upload this evening.
Jan 4 '11 #8
ADezii
8,834 Expert 8TB
Kindly provide detailed info on anything that is relevant.
Jan 4 '11 #9
okay,

I need the frm_DH and multiselect lst_region to populate the qry_DH and subsequently the tbl_Questions column regions table.

The user may select to remove a region from the record or add the region to the record at any time. They should have to have at minimum one region and they could have a possible choice of "ALL". If "ALL" is selected then they should not be able to select additional items from the list.

Anything you can do to assist would be great - thank you
Attached Files
File Type: zip Copy.zip (274.9 KB, 53 views)
Jan 5 '11 #10
ADezii
8,834 Expert 8TB
Cannot Open the File, Brandi - Unrecognized database format
Jan 5 '11 #11
:(

Of course! I think I saved it in 2010 instead of older version. I am ridiculous. I will reload it again this evening. thank you
Jan 5 '11 #12
I am so sorry for the lateness of this reply. I just started back on the project and would be very greateful if you could look at this for me again.

I hope you can open this one, Let me know.

thank you,
Attached Files
File Type: zip Copy (2).zip (273.6 KB, 59 views)
Jan 12 '11 #13
ADezii
8,834 Expert 8TB
Unrecognized Database Format again, need the DB in Access 2003.
Jan 12 '11 #14

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

Similar topics

3
by: arthur-e | last post by:
Hi I'm trying to use a multiselect listbox to limit records in a report. My version at work is 97 but now at home I'm using Access2002- I know I can't go backwards ( to use this or similar code at...
2
by: Sally | last post by:
I have a simple multiselect listbox with a rowsorce of MemberID, MemberName, SendLetter. SendLetter is a Yes/No field. What is the code to set SendLetter to Yes when the user selects MemberName? I...
2
by: Sally | last post by:
In a simple multiselect listbox, what is the code to return an item's index when it is selected? Thanks! Sally
2
by: Cassie Pennington | last post by:
I am trying to write various items from a multiselect list box to an SQL statement to update a report, without success. SQL only appears to accept hard-coded data or control values from a form, not...
1
by: | last post by:
I am having trouble getting the procedure to use a Multiselect listbox in a report to work. I am using Access 2000. Here's the code. The error I get is QueryDef "User-defined type not...
2
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
1
by: Mike P | last post by:
How do I get the values selected from a multiselect listbox? Cheers, Mike *** Sent via Developersdex http://www.developersdex.com ***
2
by: ttime | last post by:
I've got a form that uses a multiselect listbox. When a user is selected from a combo box, values are populated into this listbox associated with that user. The problem is, if one person has say...
3
by: kaosyeti via AccessMonster.com | last post by:
hey... i have an unbound multiselect listbox on a form that i want to use to populate text boxes on that form. so if a user selects the 3rd item in a list of 20, how can i have that item show up...
5
by: martin DH | last post by:
Hello, The details are below, but I have a simple form (Form1) with two objects and a "search" command button. When the two objects are cascading combo boxes (the form creates the parameters for a...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.