Databound CheckBoxList for Microsoft Access - ms-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.

Related

Editing listbox item

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.

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.

Microsoft Access How to retrieve current form ID to print

I am trying to print a report called Carton Labels, and I want to print a specific record that the user just created. Every time I try using Me.cartonNo which is a actual field on the current form the user is on. He gets prompted for input. What am I doing wrong?
EDIT:
Dim strDocName As String
Dim strFilter As String
DoCmd.RunCommand acCmdSaveRecord
strDocName = "Carton Labels"
strFilter = "[cartonNo] = Forms![frm_addFinishedGoodsInventory]![cartonNo]"
DoCmd.OpenReport strDocName, acViewPreview, strFilter
my print button doesn't work, it acts as if nothing was stored in the strFilter, but if I create Dim intFilter As Integer and store it into a integer, I can clearly see the cartonNo if I set a break point, yet I dont in the strFilter.
You need to resolve the carton number before you pass it to OpenReport. By placing the Forms! reference outside the quotation marks like this:
strFilter = "[cartonNo] = " & Forms![frm_addFinishedGoodsInventory]![cartonNo]
you are creating the string [cartonNo] = 6 and then passing that to the report. If the field were a text field (or date), you'd need to include quotes (or #s) around the value.

filter continuous form using textbox

I need to let users filter a continuous form using values the user enters into a textbox. And the continuous form is also nested within a couple levels of navigation subforms. This sounds easy enough, but all the examples I find on the web use macros instead of vba.
I set up the structure and wrote an AfterUpdate procedure for a textbox txtFilter as follows:
Private Sub txtFilter_AfterUpdate()
Dim filterval As String
filterval = txtFilter.Value
With Forms!Main!NavigationSubform.Form!NavigationSubform.Form
.Filter = "LastName Like " & filterval
.FilterOn = True
End With
End Sub
I have played with different syntax, but none of it seems to work properly. Here is a link to download the relevant parts of the database from a file sharing site: http://jmp.sh/v/HGctZ4Ru74vDAjzN43Wq
Can anyone show me how to alter this so that users can use the textbox to filter the continuous form?
I got it to work using this: .Filter = "LastName Like """ & filterval & """"
Need those annoying String Identifiers even for strings sometimes.
Okay, To get the form to open with no records and then pull up just the records you (or the user) specifies is easiest with a bit of re-work.
(I'd recommend you working with a copy and not your original)
1:On your Continuous Form, remove the Recordsource; we're going to use Late Binding (Kinda)
2:Then delete the code under the txtFilter box, then delete the box itself.
3:Add a comboBox with something like this as the recordsource:
SELECT DISTINCT myTable.LastName FROM myTable ORDER BY myTable.LastName; (This will get you a unique list of last names so knowing how to spell the name will not be necessary, plus it assures at least one match)
4:In the After Update event of that combobox, add code like this:
Dim strSource As String
strSource = "SELECT mt.IntakeNumber, mt.ClientNumber, " & _
"mt.LastName, mt.FirstName, mt.ConsultationDate " & _
" FROM myTable mt " & _
"WHERE (mt.LastName)= '" & Me.cboFilter.Value & "'"
Me.RecordSource = strSource
Me.Requery
Obviously you'll need to change the table and field names as necessary, but hopefully you get the idea.
Option Compare Database
Option Explicit '<- always include this!!!!!
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
' only set Filter when text box contains something
' to search for ==> don't filter Null, empty string,
' or spaces
If Len(Trim(Me.txtFilter.Value & vbNullString)) > 0 Then
strFilter = "LastName Like '*" & _
Replace(Me.txtFilter.Value, "'", "''") & _
"*'"
' that Replace() prevents the procedure from breaking
' when the user enters a name with an apostrophe
' into the text box (O'Malley)
Debug.Print strFilter ' see what we built, Ctrl+g
Me.Filter = strFilter
Me.FilterOn = True
Else
' what should happen here?
' maybe just switch off the filter ...
Me.FilterOn = False
End If
End Sub