Problem With Code In Multiselect Listbox
Jan 27, 2005
Hi,
I have adapted code from ghudson's example on
'http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=52736
I have a subform called frmsJobPartsUsed, which contains a multiselect list box where the user can select multiply parts used for one job and click a save button, which saves the parts to rows on the same forms (see picture). The user then enters the number used and that number is taken away from the UnitsInStock.
This form is made up of the following two tables;
TblStore
PartNo
PartName
UnitsInStock
ReOrderLevel
Discountinued
Remark
tblPartsUsed
PartUsedID
JobDetailsID
PartNo
PartUsedNum
NumberUsed
The multiselect listbox is made form tblStore, PartNo, PartName and Discontinued = 0
This all works fine so far.
What is need to do is before the parts selected are saved to the table I want to run some code to check
If a part’s UnitsInStock is equal to 0 then Message box saying no stock left need to reorder. It won’t save it to the table.
Or else
If UnitsInStock is greater than 0 but less than or equal to ReOrderLevel
Message box saying Stock running low need to reorder asap.
I have this kind of working but it doesn’t seem to be finding the correct UnitsInStock for the part selected.
Here is the code;
Private Sub cmdAnswer_Click() 'SAVE BUTTON
On Error GoTo ErrMsg:
'Code adapted from ghudson's example on
'http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=52736
Dim myFrm As Form, myCtl As Control
Dim mySelection As Variant
Dim iSelected, iCount As Long
Dim myDB As DAO.Database
Dim myRst As DAO.Recordset
Dim myRstCount As DAO.Recordset
Set myDB = CurrentDb()
Set myRst = myDB.OpenRecordset("tblPartsUsed")
Set myFrm = Me
Set myCtl = Me.lstAnswers
iCount = 0
'Count number of selected records/items
For Each mySelection In myCtl.ItemsSelected
iCount = iCount + 1
Next mySelection
'Check if anything is slected
If iCount = 0 Then
MsgBox "There are no Parts selected..", _
vbInformation, "Nothing selected!"
Exit Sub
End If
StrSQLCount = "SELECT tblPartsUsed.JobDetailsID, Count(tblPartsUsed.PartNo) AS CountOfPartNo " & _
"FROM tblPartsUsed " & _
"GROUP BY tblPartsUsed.JobDetailsID " & _
"HAVING (((tblPartsUsed.JobDetailsID)=" & [Forms]![frmJobs]![JobDetailsID] & "));"
Set myRstCount = myDB.OpenRecordset(StrSQLCount, dbOpenSnapshot)
'SART OF MY CODE TO CHECK FOR UNITSINSTOCK
For Each mySelection In myCtl.ItemsSelected
If Me.UnitsInStock.Value = 0 Then
MsgBox "Out of Stock!" & Chr(13) & "Please returen to Orders or Store to Re-Order Stock. " & Chr(13) & " ", vbOKOnly + vbCritical, "Re-Order Stock"
myCtl.Selected(mySelection) = False
Else
If Me.UnitsInStock.Value > 0 And Me.UnitsInStock <= Me.ReOrderLevel.Value Then
MsgBox "The Store is running low on stock!!" & Chr(13) & " Please return to Orders or Store to re-order as soon as possible.", vbInformation, "Need to Re-Order Stock"
End If
End If
Next mySelection
'END
iCount = 0
'Go throught each selected 'record' (ItemsSelected) in listbox
For Each mySelection In myCtl.ItemsSelected
'Current count of selected items
iCount = iCount + 1
'Print value to Immediate Window
Debug.Print myCtl.ItemData(mySelection)
'Add answers
With myRst
.AddNew
.Fields("JobDetailsID") = Forms![frmJobs]![JobDetailsID]
.Fields("PartUsedNum") = iCount
.Fields("PartNo") = myCtl.ItemData(mySelection)
.Update
End With
Next mySelection
'Requery form
Me.Requery
ResumeHere:
Exit Sub
ErrMsg:
MsgBox "Error Number: " & Err.Number & _
"Error Description: " & Err.Description & _
"Error Source: " & Err.Source, vbCritical, "Error!"
Resume ResumeHere:
End Sub
Private Sub cmdUnselect_Click() 'UNSELECT BUTTON
On Error GoTo ErrMsg:
'Code adapted from ghudson's example on
'http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=52736
Dim myFrm As Form, myCtl As Control
Dim mySelection As Variant
Dim iSelected, iCount As Long
Set myFrm = Me
Set myCtl = Me.lstAnswers
'Count number of selected records/items
For Each mySelection In myCtl.ItemsSelected
iCount = iCount + 1
Next mySelection
If iCount = 0 Then
MsgBox "There are no selections to Un-Select..", _
vbInformation, "Nothing selected!"
End If
'Go throught each selected 'record' (ItemsSelected) in listbox
For Each mySelection In myCtl.ItemsSelected
Debug.Print myCtl.ItemData(mySelection)
myCtl.Selected(mySelection) = False
Next mySelection
ResumeHere:
Exit Sub
ErrMsg:
MsgBox "Error Number: " & Err.Number & _
"Error Description: " & Err.Description & _
"Error Source: " & Err.Source, vbCritical, "Error!"
Resume ResumeHere:
End Sub
Any help would be greatly appreciated.
Thanks in advance
Rita
View Replies
ADVERTISEMENT
Apr 19, 2005
I have a table, "people" which has all the main data stored. I have another table, "organizations" which I'd like to be able to use on a form to select which organizations people belong to. I created a button on my main form which opens another form on which a listbox with all the organizations shows. What I'd like to do is from the main form, click a button that brings up a new form. On the new form, I can select the affiliations. Then, when I close that form, a box on the main form shows what i've just selected. I got this to work with the listbox, but when I changed it to multi-select it stopped working. Help!! Thanks.
View 5 Replies
View Related
Jun 25, 2005
I want to be able to select multiple items from a listbox, click a button, and then print the data related to that item selected using a report.
In the report to print the data, I have added the criteria
[Forms]![Print Labels]![List2]
This is the name of the listbox. However, this doesn't print the items selected in the list box.
Do I have to do this through VBA, or can I accomplish this through a query in the report?
Thanks!
View 1 Replies
View Related
Jan 28, 2005
I was wondering whether it is possible to select multiple values in a list box on a form that can be stored in a table.
View 4 Replies
View Related
May 21, 2007
I have a multiSelect Listbox that has email recipients. I have a command button with Me.emaillist = Null, and after I click the command button the Listbox is still highlighted in black. What VBA command do I create to completely clear the multiSelect Listbox, including the highlighted selection?
losstww
View 1 Replies
View Related
Jan 26, 2005
I'm trying to create a database where a single "Classification" field is populated by selections made in a multiselect listbox and I can't figure out how to do this. Any help people can provide?
Table 1:
Name_ID <pk>
Name
Classification
Table 2:
Classification_ID <pk>
Classification
What I want to happen is click on a button next to the Classification field (text) and a popup form with a multi-select list loads (this part is easy, of course). The user can select as many classifications as they want, click the ok button and each item selected then goes back to the first form and populate in the Classification field (seperated by commas or semi-colons).
This possible?
View 3 Replies
View Related
Apr 19, 2005
OK - I have seen the other posts where individuals are trying to select multiple items from a list box and have a field populate with the selections. I have not seen a clear explanation defining if this is possible.
Essentially, I want to be able to query on the field and search for multiple selections within that field. Any recommendations as to how this can be achieved?
My next question, is if the selections in a multiple instance field are separated by a comma or some other character what is the best method to query for multile responses. For example, if the following data is in the field 1,2,3 and I want to query on 1 or 2?
Regards,
PolarBear
View 3 Replies
View Related
Dec 12, 2006
Hi, i was wondering if it's possible in a listbox with the multiselect option set to extended simply click on a field and open a form with the data of this field..
example:
i've a listbox of my customers, (only first and last name)..i click on one of this and a new form with all the details'll open, so i can see and modify stuff...
with the multiselect set to none this works..but with the extended??
(btw..i wanna just click on one filed, but i need the extended multiselection on for other options..)
thx in advance
View 14 Replies
View Related
May 22, 2014
Is it possible to have a multiselect listbox on a subform with a datasheet view or do I need a combobox in this situation?
View 4 Replies
View Related
Jul 31, 2013
I created a form with a ListBox and a Command Button. The users selects the values in the listbox and then click the button to create word documents. I've written VBA code to accomplish this. But it's not working properly. It opens multiple word documents but all for the same one.
Private Sub Command6_Click()
Dim appWord As Object
Dim varItem As Variant
Dim strPathToTemplateFile As String
Dim strPathToProspectiveFile As String
Dim strPreferredFileName As String
For Each varItem In Me.List0.ItemsSelected
[code].....
View 13 Replies
View Related
Apr 21, 2014
I would like to use a listbox set to multiselect to add records to a junction table. I've been using code to accomplish this with checkboxes (love how it looks and works) but after moving my tables to Office 365 as the backend, linkedto a local frontend, sql does not like this particular set up, and I do not have the time or knowledge to sort out why. So what I need is a step by step to look at the many, in this case possible roles a contact can have, and choose one or more, which then creates a record in the junction table with the contact id and role id.
I would prefer to not use a combobox on a continuous form because every time a user goes to select roles he would have to scroll through all the choices for each separate role.
View 5 Replies
View Related
Sep 2, 2013
I've got a form with a multiselect listbox (extended) that holds a very long list of items (~90,000):
1 | Apple
2 | Orange
3 | Banana
...............
35313| Corn cob
...............
The user can select multiple items (non-sequential) on the listbox, say items 1 and 35313. I'd like the listbox to scroll (meaning display) to the currrently selected item, so that the user can see it while being processed.
I've seen stuff about property ListIndex, which does exactly that, but it's only useful when you have just ONE item selected (in a multiselect listbox apparently it deselects the rest of the items, so no point in using it, I guess, unless there's more to it!!).
So the key here is how to do the scrolling/display.
The code to loop through the listbox and process each selected item is attached:
Code:
Dim vItem As Variant
Dim Content as String
For Each vItem In Me.lst.ItemsSelected
'scroll to selected item ???
'processing of the selected item
Content = Me.lst.Column(1, vItem) 'copy the content to do sth with it
Me.lst.Selected(vItem) = False 'unselect the current item
Next vItem 'go to next selected item
how to scroll to the currently selected item? Mind that I need to go through the list of selected items to process them for further use (i.e, I need them selected).
View 4 Replies
View Related
Jan 24, 2005
I have a form "frmMAINEntry" with a tabcontrol with several pages. The form's Record Source is a query "qryMainData"; however, on two of the pages there's a difference. On the page "Program List" I have a listbox that has a Row Source to table "tsubProgramList" and on the page "Program Details" I have a subform that's Record Source is also "tsubProgramList". The two pages of the tabcontrol are: "ctlProgramDetails" and "ctlProgramList".
The table tsubProgramList contains these fields:
ProgramID..............AutoNumber
Program Description...Text
Facility...................Text
ProgramCoordinator...Text
I'm trying to develop code that will allow me to doubleclick on an item in the listbox which will change tab pages and display the selected record in the subform that's on the other tab page. See code below:
Code:
=================================================
Private Sub lstPrograms_DblClick(Cancel As Integer)
Dim rs As Object
Dim db As Database
Set db = CurrentDb()
Set rs = Forms![frmMainEntry]![fctlProgramList].Form.Recordset.Clone
rs.FindFirst "[ProgramID]" = " & Str(Nz(Me![lstPrograms], 0))"
If Not rs.EOF Then Forms![frmMainEntry]![fctlProgramList].Form.Bookmark = rs.Bookmark
Me![ctlProgramDetails].SetFocus
Call HideNavButtons
End Sub
=================================================
Someone's help would be greatly appreciated.
PC
View 3 Replies
View Related
Jul 19, 2005
I used one of the listbox search functions from the forums and changed it a bit to suit my needs.
I'm not having any errors in the code but I'm not getting any results in my listbox after I search. Also, when I type in what I'm searching for the letters always start in the left so i ended up spelling my words backwards.
This is what I have:
Private blnSpaceBar As Boolean
Option Compare Database
Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 32 Then blnSpaceBar = True
End Sub
Private Sub Form_Load()
On Error Resume Next
SetListDefaultRowSource
Me.txtFilter.Enabled = True
Me.txtFilter = ""
DoCmd.GoToControl "txtFilter"
End Sub
Private Sub ComboParent_AfterUpdate()
Me.ComboChild.Requery
End Sub
Private Sub ComboChild_AfterUpdate()
Me.LstFindings.Requery
End Sub
Private Sub txtFilter_Change()
If IsNull(Me.txtFilter) Then
SetListDefaultRowSource
Exit Sub
Else
If blnSpaceBar = True Then
Me.txtFilter = Me.txtFilter & " "
blnSpaceBar = False
End If
Me.txtFilter.SelStart = Len(Me.txtFilter)
End If
End Sub
Sub SetListDefaultRowSource()
LstFindings.RowSource = _
"SELECT DescriptionID, ElevationID, TradeID, RoomID, Description, Cost, Retail, Markup" & _
"FROM NEWDESCRIPTIONS" & _
"WHERE NEWDESCRIPTIONS.ModelID = [Forms]![frmExample]![ComboChild]" & _
"ORDER BY NEWDESCRIPTIONS.Description"
End Sub
Does anyone have an idea of what I need to do?
thanks very much,
joëlle
View 14 Replies
View Related
Dec 14, 2005
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Logins"
stLinkCriteria = "[ID]=" & "'" & Me![lstLogins] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
This code is on the doubleclick event.
lstLogins is sourced from this: SELECT Logins.[ID], Logins.[Login for] FROM Logins WHERE (((Logins.Customer)=[forms]![customers]![customer]));
When i run it I get a VB error "The OpenForm action was cancelled", and the debugger stops on the last line above (Do.Cmd...)
Any ideas why?
I know [ID] is a long integer, but I tried formatting it with Str just in case and it made no difference :(
View 2 Replies
View Related
Jul 17, 2014
I have a Access table that stores BatchNo,Scandate,NewBatchNo . As I can't attach Access database here so I have exported data from Access to Excel in Sheet1 to show how the data is stored in Access table.
Now I have to write code in VBA that will check the last NewBatchNo in the table . In the attached workbook its 194389. Now vba code should check the NewBatchNo which are blank before 194389 . Now in the records where NewBatchNo is blank , it should add the corresponding BatchNo,ScanDate in the listbox1 in form1.
e.g Sheet2 in the attached workbook stores Bathcno and Scandate of those records where NewBatchNo is null and before the Last used NewBatchNo which is 194389 in our example.
View 4 Replies
View Related
Aug 9, 2013
Need importing just 1 column from excel file into vba !
View 1 Replies
View Related
Jan 14, 2014
Access 2007-10
Listbox created: List62 (I know I need to rename it, but for now)
Multi-select: Extended
Row Source Type: Field List
Row Source: qryFieldList
Open Query: qrySelectedFields (I added primary key to first column, just to have at least one destination field).
Goal: to select multiple fields within listbox, click on command button to open query "qrySelectedFields" with selected fields from list box.This is the code I have on the command_click:
Dim varItem As Variant
Dim strSQL As String
If Me.List62.ItemsSelected.Count = 0 Then
MsgBox "Please select one or more fields.", vbExclamation, "Healthcare REIT"
Me.List62.SetFocus
End If
[code]....
It does absolutely nothing - doesn't add the fields to "qrySelectedFields", doesn't open the query, notta.
View 8 Replies
View Related
Oct 24, 2013
I've been using the following code successfully in Access 2003 & now I need to migrate to Access 2010. The purpose of the code is to use the items that the user selects in the list box to build the criteria of a query. Access 2010 keeps giving me a syntax error when I try to run the query & I don't know why:
My code is:
On Error GoTo Err_Command151_Click
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
[Code] .....
The syntax error I get in Access 2010 is:
Syntax Error in query expression 'SELECT * FROM
qryContractListSummarybyDateContract3TYPEBREAK WHERE
qryContractListSummarybyDateContract3TYPEBREAK.Rep ortableName IN('Adbri
Masonry NSW');'
View 12 Replies
View Related
Mar 31, 2005
Hi all
I have an unbound form. To the left I have a list box with a list of employee names. To the right I have a bunch of text fields. Once I've entered the data, I click on a button and it adds a new record. It works perfectly provided I add one employee at a time. It gets painful when I have multiple employees and I'm entering the same data.
Is there a way I can multiselect employees from the listbox, fill in the data fields and add multiple records which just one click??
I'd appreciate any advice.
View 4 Replies
View Related
Aug 27, 2005
Now i need some help here.
I adapted this code from somewhere on this forum. But iam having a problem with using the 'ALL' criteria.
It Opens a Report Dynamically from multiple selections in the two multiselect ListBoxes, but when i select 'All' and select any other item in another listbox
For Example(All in Names and Home1 in Homes) it gives me all the Results.
I have tried several criterias to solve it out but iam still failing.
Hope there will be help. Attached is a sample db in A2K and A97.
Thanks in Advance.
View 3 Replies
View Related
Dec 25, 2004
Merry X-mas everyone!!!
I'm new to this message board and I'm glad i came across it. I've been looking for a way to multiselct search using Combo boxes.Reading through the threads here i found one (Posted by SBaxter). I've use the SQL Statements and VB code for my search. It works but not like his for some reason. The problem i'm having is when you open the form it asks for criteria like a parameter Query does. You have to type it in before it loads, unlike his where you click what you want out of combo boxes. At first i thought it was something i did wrong with the query but i've noticed that in his DB if you have the form open and then open the query it doesn't act like parameter. It does if the form is close. With mine that doesn't matter i still have to type it in! So i'm guessing it's with how i set up the list box in my form. I've attached a copy for someone to look at.
Thank.
RichM
View 5 Replies
View Related
Dec 19, 2004
Hi all
I have a list box with a bunch of names in it that links to a subform on a master form. At the moment I click on a name and update details in the subform. It would be nice if I could multiselect names then update details all at once without having to individually change each one. I tried the multiselect property but I get index or relationship errors. I don't have any relationships (because I'm not adding new records... just updating) and I changed the indexes to NO. Didn't work. I tried!! Any idea how I might get this working??
I appreciate any advice.
Damon
View 13 Replies
View Related
Mar 10, 2006
Does any one know of a way to toggle a multiselect listbox's property programmatically?
The One In The VBS Help doesn't work!
View 5 Replies
View Related
Feb 18, 2006
Hi, my problems are numerous!
I have a form with a number of multiselect listboxes. Their sources are individual tables such as YEARS, PARISHES, CLIENTS. The underlying data is stored in a separate table. I want users to be able to select none, one or some values from each listbox and run a report.
As simple listboxes with one selection permitted this worked fine. I know that my problem is in concantenating the values.
I have tried attaching some code to the OnClick function of a cmd button on the form. I found this code referred to a number of times here and have tried to adapt it but no luck. I have now reduced my form to one listbox to test stuff out- this is the code-
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Set frm = Form!testform
Set ctl = frm!lboparish
strSQL = "Select * from StJamesAgric where [Parish]="
For Each varItem In ctl.ItemsSelected
strSQL = strSQL & ctl.ItemData(varItem) & " OR [Parish]="
Next varItem
'Trim the end of strSQL
strSQL=left$(strSQL,len(strSQL)-300)
The first error I get is "can't find field 'testform'"
My form is called testform
The listbox is called lboparish
The table with all the data is called StJamesAgric
What am I doing wrong???? And also, if this code EVER works, do I need to repeat it for each listbox or do I insert refernces to each list box within just one subcommand?
Any help greatly appreciated.
Stella
View 5 Replies
View Related
Nov 8, 2013
I have a single-user application about to become multi-user.
There is a table, displayed in a form as datasheet, where we do lots of things with the selected items.
Code:
MyID MyItem IsSelected ....
1______ItemA__ Yes
2______ItemB__ Yes
3______ItemC__ No
....
The user checks the IsSelected (bound to a checkbox) for the items required for further processing, does the processing and starts over. The function of the column IsSelected is to hold the boolean signifying whether or not to process the record, and to bind to the checkbox in the form.
How to retain the user interface (i.,e. selecting using one or more checkboxes) if the database is to be used by more than one user?
View 4 Replies
View Related