Editing listbox item - ms-access

So i have a listbox that displays all the orders entered in the Order table using a select sql query. Now i also want to add the ability to edit the items from the listbox, i see the right click edit list item option but when i click it, it just opens the form but doesnt populate the fields. The form has textboxes that are unbound but i cant figure out how to get them to populate based on the right clicked entry. I have also tried to open the target form from vba and fill the fields through vba with the following code
Private Sub editrecordbttn_Click()
Dim valSelect As Variant
Dim v As Variant
Dim selector As Variant
Dim strValue As String ' just used for the demonstration
Dim splitvalue() As String
Dim selectedsampid As String
Dim selectedcusid As String
Dim Records As DAO.Recordset
Dim SQLcus As String
Dim SQLsamp As String
For Each valSelect In Me.searchlistbox.ItemsSelected
strValue = strValue & "" & Me.searchlistbox.ItemData(valSelect) & "," & "" & Me.searchlistbox.Column(1, valSelect) & ","
Next valSelect
' to remove trailing comma
strValue = Left(strValue, Len(strValue) - 1)
splitvalue() = Split(strValue, ",")
selectedsampid = splitvalue(0)
selectedcusid = splitvalue(1)
DoCmd.OpenForm ("Add Sample")
Forms![Add Sample].fnametxt.SetFocus
'query and fill cus info
SQLcus = "SELECT * FROM CustomerInfo WHERE CusID = '" & selectedcusid & "';"
Set Records = CurrentDb.OpenRecordset(SQLcus)
Me!clienttypetxt = Records![Client type].Value
End Sub

Ok, so say we have a listbox, and we do this:
The first column of the listbox is assumed to be the PK or "ID" of the rows.
so, we have this:
And thus you select a row, and then click on the button.
The button code would look like this:
Private Sub cmdEdit_Click()
Debug.Print "Hotel list id selected = " & Me.HotelList
DoCmd.OpenForm "frmEditHotels", , , "ID = " & Me.HotelList
End Sub
So, in most cases, for a better user experience, it probably better to approach things as per above.
There is of course the case in which you fill the listbox (or combo) with a "list" of values NOT from the database. In that case, you can use the "edit" list option. And this allows you to specify a form (or use the built in editor).
so, if this is NOT a list that you type in, and is from the database, then don't try to use the built in "list editing"
(add a button like above, and launch the form with the "where" clause to load the form to the ONE data record as I did above.
Since oh so very often, a listbox data will come from a table, then the edit list options are not really much particular use. And using a table (as opposed to a list) to fill + drive the combo/listbox is a much better design, and idea anyway.
This is especially the case if you ever want multiple-users, since the "list" edit feature would mean and suggest that each user editing the list would now have their own lists as opposed to using a table which everyone can edit.
Also, there is NO reason to use a loop to fill that list box. We can do this:
' setup critera for listbox.
Dim strSQL As String
' prompt user for Hotel city - we just hard code for this exmaple.
Dim strCity As String
strCity = "Banff"
strSQL = "SELECT ID,FirstName, LastName, City,HotelName FROM tblHotels " & _
"WHERE City = '" & strCity & "' " & "ORDER BY HotelName"
Me.HotelList.RowSource = strSQL
Note how we do not have some MESSAY value list, but can shove the data (sql) right to the listbox. Not only do we don't have loops, but we also don't have to worry about the size limits.
With "value list" (those messy delimited ";" list), then you have a rather small limit of 4,000 characters. Don't take many larger rows to "blow up" the listbox, since it can't handel very many rows.
In fact, I often still suggest you use the wizard to build the listbox, and you can choose a datasource (sql), or the MUCH lessor choice of "value list".
Value list is only a good choice if you have say a few choices like Mr., Mrs. or what not, and it not some large table, but only say 5-10 choices.
Anything larger? Use a data table driven listbox, and avoid use of value list.

Related

Using textboxes to search entries based on a keyword (There are multiple textboxes for separate entities) and implementing a combo box

I want to be able to enter in a keyword into a textbox that would search records for that keyword and present reports based on that word that are found.
Please Note: There are multiple textboxes for this to separate specific information so that I need this to work for each individual textbox. There is one text box that will search for entries based on the date (month/day/year) and I have been having issues with it not displaying certain date ranges.
I am also looking to add in a combo box as well.
I have tried code that another individual has given me, but the issue that I have run into with the normal textboxes is that it requires me to put in everything that is in the entry as opposed to search based on a keyword to which nothing pops up when other criteria have been put in place (I know that there is an entry that fits, but for some reason when I add in the textbox element it does not work unless I put in the exact entry that is in the textbox that I am search the specific information from).
I have been trying to find information for combo boxes, but I have not found anything that really works with what I want to do.
Function SelectedItems(objBox As ListBox) As String
Dim strRtn As String, varItm
For Each varItm In objBox.ItemsSelected
strRtn = strRtn & ",'" & objBox.ItemData(varItm) & "'"
Next varItm
If strRtn <> vbNullString Then SelectedItems = Mid(strRtn, 2)
End Function
Private Sub Command62_Click()
Dim strSQL As String
Dim strArr As String
Dim varItm
For Each varItm In Array("District", "Circumstance", "Location", "Method", "Point", "Rank", "Description", "Missing", "IDNumber", "Dateto", "Datefrom", "Address")
strArr = vbNullString
Select Case Me.Controls(varItm).ControlType
Case acListBox
strArr = SelectedItems(Me.Controls(varItm))
Case acTextBox
If Not IsNull(Me.Controls(varItm).Value) Then
strArr = "'" & Me.Controls(varItm).Value & "'"
End If
End Select
If strArr <> vbNullString Then
strSQL = strSQL & "t." & varItm & " in (" & strArr & ") and "
End If
Next varItm
If strSQL <> vbNullString Then strSQL = "where " & Left(strSQL, Len(strSQL) - 5)
With CurrentDb.QueryDefs("qryMultiselect")
.SQL = "select * from tblDataEntry t " & strSQL
End With
DoCmd.OpenQuery "qryMultiselect"
End Sub
What I want is to be able to use my multi-select listboxes in conjunction with my textboxes as well as my combo box that I have as well. Using all of that or at least a combination of them, I want to be able to sort through my data to be able to create reports based on what is being looked for. Along with making sure that the user has the freedom to not having to enter in information for all fields and only the ones that they feel they need to enter in information on.
With the textboxes: I want to be able to search based on a keywords that corresponds with a specific section of the data that I am looking for and to be able to leave others blank and be able to pull up results.
The same goes for the date/time textboxes and my combo box.

Combobox with LimitToList set to false with non-bound field

I have a products table and a tags table with a many-to-many relationship. I also have a pivot table that store the product id and the tag id.
I'd like to be able to show the tag text in the combo box but store the id. This on its own is simple as I can just adjust the column width of the id, however, I'm trying to set LimitToList to No so I can implement some VBA to filter the list when I type in the combo box, but this only appears to be possible if the first column is the bound column, which means the tag id appears in the combo box instead of the text.
How do I show the tag text in the combobox and allow 'LimitToList' to be false?
Leave LimitToList on Yes, and use the NotInList event of the combobox instead.
E.g.
Private Sub cboTag_NotInList(NewData As String, Response As Integer)
Dim RS As Recordset
If MsgBox("Do you want to add '" & NewData & "' as new Tag?", vbYesNo + vbQuestion) _
= vbYes Then
' Add the new tag
Set RS = CurrentDb.OpenRecordset("tbTags")
With RS
.AddNew
!Tag_Text = NewData
.Update
.Close
End With
Response = acDataErrAdded
Else
Response = acDataErrDisplay
' Or a user defined MsgBox and acDataErrContinue
End If
End Sub
(sigh) again I have misunderstood your question. :(
To filter the combobox as the user types a part of a Tag, you use the Change event. This works just fine with LimitToList = Yes.
Private Sub cboTag_Change()
Me.cboTag.RowSource = _
"SELECT Tag_ID, Tag_Text FROM tbTags WHERE Tag_Text LIKE '*" & Me.cboTag.Text & "*'"
' With the instant filtering, it is more convenient for the user
' to always see the filtered dropdown
Me.cboTag.Dropdown
End Sub
Of course at some point the user has to select a Tag from the list. Otherwise there would be no ID to store in the field.
Or if you want him to be able to add new tags, use the code from the other answer. Both events work together as written.
Search-as-you type technique described very good here, including automatic selection of items from opened dropdown combobox after typing few characters.

Passing query result to a textbox control on an access form

I need help with a textbox field on an Access 2007 form. I'm trying to insert the result of a query into the text box control on the form. This is used soley as information for the user. The form supplies the query with parameters to get the value. The query works fine and returns the correct result. What I can't seem to figure out is how to pass the query result to the textbox. I’ve tried several different ways but with no luck.
(PS> I know a combo box can do a lookup, however I don’t want the user to have to click the dropdown just to select the value as there can only ever be one value result from the query.) I'm open to suggestions as I'm not a programmer or DB Admin, but I've taking a few classes on Access (enough to be dangerous).
Private Sub cbo3_Change()
Me.tbx2 = ("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, tbl_Billing.Savings_b HAVING (((tbl_Billing.UBI_b)=forms!f1_UpBilled!cbo1) And ((tbl_Billing.TaxYr_b)=forms!f1_Upbilled!cbo2) And ((tbl_Billing.TaxPrg_b)=forms!f1_UpBilled!cbo3));")
End Sub
If you wish to do this in run time, you need to do the following, I take the controls you are referring to in this is on the same form.
The very simple and straight forward way to get it done is as follows,
Private Sub cbo3_Change()
Dim tmpRS As DAO.Recordset
Set tmpRS = CurrentDb.OpenRecordset("SELECT tbl_Billing.Savings_b FROM tbl_Billing GROUP BY " & _
"tbl_Billing.UBI_b, tbl_Billing.TaxYr_b, tbl_Billing.TaxPrg_b, " & _
"tbl_Billing.Savings_b HAVING ((tbl_Billing.UBI_b = '" & Me.cbo1 & "') And (tbl_Billing.TaxYr_b = '" & Me.cbo2 & "') " & _
"And (tbl_Billing.TaxPrg_b = '" & Me.cbo3 & "'))")
If tmpRS.RecordCount > 0 Then
Me.tbx2 = tmpRS.Fields(0)
Else
Me.tbx2 = 0
End If
Set tmpRS = Nothing
End Sub
Just note, I have implied all your combo boxes are returning String and the field you are comparing against are Text type. If that is not the case, you need to make changes accordingly.

Databound CheckBoxList for Microsoft Access

In ASP.NET I have the option for building a CheckBoxList control from a data source where the values of the items can be the "Id" column (for example) while the text displayed next to the checkbox is from a "Name" column.
Is it possible to do something similar to this for Access 2003 with VBA? I am trying to avoid hardcoding the list of items if at all possible, but can't figure out how to do anything similar to this.
Just use a listbox. A list box in Access is great because it allows multiple columns, but will hide the first column as you ask. And you can just set the listbox to allow multiple selections (in the other tab of the property sheet for the listbox, set Multiselect to "simple"
And even better is you don't need any code to fill out the listbox, but can type in the sql or simply base the listbox on a SQL query.
So a listbox can look like this:
The code behind the button to turn the selected items into a useful list of PK id looks like this:
Private Sub cmdFax_Click()
Dim strIDList As String
Dim vID As Variant
Dim strSQLWhere As String
For Each vID In Me.lstFaxList.ItemsSelected
If strIDList <> "" Then strIDList = strIDList & ","
strIDList = strIDList & lstFaxList.ItemData(vID)
Next
If strIDList <> "" Then
strSQLWhere = " ID in (" & strIDList & ")"
End If
DoCmd.OpenReport "rptFax", acViewPreview, , strSQLWhere
End Sub
And if you want, you can in code for supply or set the SQL of the listbox like this:
Sub mytest()
Dim strSql As String
strSql = "Select ID, firstName, LastName, City " & _
"where city = 'Edmonton'"
Me.lstFaxList.RowSource = strSql
End Sub
So you don't need to type in any kind of list here. It not clear if you "must" have a actual check box here - you can use a continues form if you wanted, but I think the above is oh so much more simple and you don't need much code to set this up at all.

Adding logic to a sub form in Access?

I have a sub form in Access:
The CopyNo is a combobox that lets me select from the MovieCopies table. When I select one, I want the Title field to show the correct movie title associated to that copy's movie ID. I also want the format to show. When I select from DaysRented combobox, if I select 1 and the movie is New, I want it to display the price, if it is regular for 3 days display the correct price etc.
I'm just not sure how to give logic to the comboboxes.
If anyone could point me in the right direction of how to do this sort of thing in Access 2007 I'd really appreciate it.
Thanks
Something like this:
Private Sub cboCopyNo_AfterUpdate()
If Nz(Me.cboCopyNo, "") <> "" Then
Me.txtTitle = DLookup("Title", "MovieMaster", "MovieID = " & Me.cboCopyNo)
End If
End Sub
Private Sub cboDaysRented_AfterUpdate()
If Nz(Me.cboDaysRented, 0) > 0 Then
Dim strType as String
strType = DLookup("[Type]", "MovieMaster", "MovieID = " & Me.cboCopyNo)
If Me.cboDaysRented = 1 Then
Me.txtPrice = DLookup("Price1Day", "Price", "[Type] = '" & strType & "'")
Else
Me.txtPrice = DLookup("Price3Day", "Price", "[Type] = '" & strType & "'")
End If
End If
End Sub
Couple notes. Some of your field names are reserved words in certain databases, such as "Type". I highly recommend you try to use field names that are not reserved words in Access or SQL server.
DLookups are not necessarily the fastest way to lookup data but will likely be fast enough for what you're trying to do here. Sometimes I create my own DAO recordset and lookup the values I want rather than using DLookup. It's basically like writing your own DLookup function.
DLookup uses SQL language so your syntax in the third argument, the WHERE clause, needs to match SQL. If the field in your WHERE clause is text/string you'll need to use a single quote on either side of the value (as shown above around the strType variable). If it is a number field you will not need the quotes. If it's a date you'll need hash signs (#).