Access vba filter button - vba

I am trying to make a form with two filter buttons.
First button will + 1 to the filter and
second button will -1 to the filter.
so far I have.
Dim ADD As String
If Me.TypeID Is Empty Then
'empty TypeID, do nothing
Else
ADD = Me.TypeID + 1
DoCmd.ApplyFilter "", "[TypeID] = " & ADD
End If
I am getting
error 2427
because I can't trap if the next TypeID doesn't exist.
For example, I have now 4 records as TypeId. If I add 1, I will have 5 which I don't have.
Thank you.

Find it.
Dim Add As String
Dim LastID As Integer
LastID = DMax("TypeID", "tblType")
If Me.TypeID = LastID Then
'Empty TypeID, Do Nothing...
Else
Add = Me.TypeID + 1
DoCmd.ApplyFilter "", "[TypeID] = " & Add
End If

Do not forget to set:
Me.filter on = true

Related

How do I assign an already known integer to a field after the NotInList event is called?

I have this complicated VBA function on a MSAccess form frm_DataEntry. It searches for values which are not in a list. The function is called on de NotInList event of the comboboxes.
When the typed string in combobox cbo_CustomerLocations is not in the list, it will ask if I want to add it to the table tbl_CustomerLocations by Yes/No question. After that it goes from 1st column to the last column and asks if I want to add some new data. The code below shows how to add a CustomerLocation.
The last field CustomerID of my table tbl_CustomerLocations is linked to the CustomerID field of table tbl_Customers
Now my question:
How do I alter my VBA code when the NotInList event is called, and when it reaches the CustomerID column (the last column), It must not ask 'What do you want for CustomerID', but rather automatically selects the CustomerID I previously selected on the same form frm_DataEntry on combobox cbo_Customers?
Private Sub cbo_CustomerLocationID_NotInList(NewData As String, Response As Integer)
Dim oRS As DAO.Recordset, i As Integer, sMsg As String
Dim oRSClone As DAO.Recordset
Response = acDataErrContinue
String_Value = Me.cbo_CustomerLocationID.Text
MsgBold = String_Value
MsgNormal = "Add to list with locations?"
Debug.Print
If Eval("MsgBox ('" & MsgBold & vbNewLine _
& "#" & MsgNormal & "##', " & vbYesNo & ", 'New Location')") = vbYes Then
Set oRS = CurrentDb.OpenRecordset("tbl_CustomerLocations", dbOpenDynaset)
oRS.AddNew
oRS.Fields(1) = NewData
For i = 2 To oRS.Fields.Count - 1
sMsg = "What do you want for " & oRS(i).Name
oRS(i).Value = InputBox(sMsg, , oRS(i).DefaultValue)
Next i
oRS.Update
cbo_CustomerLocationID = Null
cbo_CustomerLocationID.Requery
DoCmd.OpenTable "tbl_CustomerLocations", acViewNormal, acReadOnly
Me.cbo_CustomerLocationID.Text = String_Value
End If
End Sub
Use an If Then block within the loop to check for name of field.
If oRS(i).Name = "CustomerID" Then
oRS(i) = Me.cbo_Customers
Else
sMsg = "What do you want for " & oRS(i).Name
oRS(i).Value = InputBox(sMsg, , oRS(i).DefaultValue)
End If

select multiple rows from datagridview and show the IDs

I'm doing a simple quiz program.
Here's how my program works: User can choose from two options;
either choose only one category(DropDownList)
choosing multiple category(where multiple rows are selected from the DataGridView)
For now I just want that if user choose multiple rows, all the IDs of the rows selected must be shown in MsgBox(). Is there anyway that I can do it? Thank you in advance.
Edited
Okay, so far I have this:
Dim id, i, j As Integer
Dim idList(1)
For Each selectedItem As DataGridViewRow In qstSets.SelectedRows
'show ids of multiple selected rows
id = qstSets.SelectedRows(0).Cells("ID").Value
idList(i) = id
i += 1
Next selectedItem
For j = 0 To 1
MsgBox("Element " & j & " = " & idList(j))
Next j
I have planned to safe the id's of selected row in array and then display it. But the problem here is that, I keep getting only one id in the Element when I have selected 2
(Posted on behalf of the OP):
Thank you for your help #cyril
Dim id, i As Integer
Dim idList(5)
For Each selectedItem As DataGridViewRow In qstSets.SelectedRows
'show ids of multiple selected rows
id = selectedItem.Cells("ID").Value
idList(i) = id
i += 1
Next selectedItem
Dim sResult As String = ""
For Each elem As String In idList
sResult &= elem & ", "
Next
MsgBox(sResult)
You can iterate over all selected datagridview rows:
For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows
For getting the Id or any value you like, please have a look to:
DataGridView get column values
To show ID's and select Multiple rows in datagridview I think it would be helpful to You.
Dim SelectedRow as datagridview.selectedrow(0)
Dim selectedID as selectedRow.cells("ID").value
Dim Row as Datarow
Dim IDLists as List(of integer)
For i = 1 To datagridview.SelectedRows.Count()
selectedRow = datagridview.SelectedRows(i - 1)
selectedID = selectedRow.Cells(0).Value
row = _table.Select("ID =" & selectedID).FirstOrDefault()
IDlists.add(selectedID)
Next
for j = 0 to IDLists.count
messagebox.show(IDlists(j))
Next
I think This will be helpful If I am wrong correct me

Get text of listBox.selectedItems?

I have a listBox that displays members of a distribution group. I have the listbox setup for multisimple selection to select more than one member. I'm trying to do a loop to get the text of the selected items(index's). Here is my code which works fine for the first item. On the second item it crashes and says "Index was outside the bounds of the array." I will be taking these members and placing them in the To: field of a meeting request. So I need to to be formatted like this: member1;member2. I'm just using the MsgBox to test what I'm getting back. But I'm guessing I would need to add to an array. Please help!
For i = 0 To (myListBox.Items.Count - 1)
If myListBox.GetSelected(i) Then
MsgBox(myListBox.SelectedItems(i))
End If
Next
Re-read the question and I think you want to store this as a string. This should do what you are looking for:
Documentation on ListBox.Selected()
Dim MailStr as String
MailStr = ""
If myListBox.SelectedItems.Count = 0 Then
MsgBox "No User Selected"
Exit Sub
End If
For i = 0 to (myListBox.Items.Count - 1)
If myListBox.Selected(i) Then
MailStr = MailStr & myListBox.Items.Item(i) & "; "
End If
Next i
You can also try:
Dim assets As String
assets = ""
For Each item In assetListBox.SelectedItems
If assets = Nothing Then
assets = item
'Outputs 1 item "Member1"
MsgBox(assets)
Else
assets = assets & "; " & item
'Outputs 2 items in format "member1; member2"
MsgBox(assets)
End If
Next
This is what worked the way I want it.
Dim assets As String
assets = ""
For Each item In assetListBox.SelectedItems
If assets = Nothing Then
assets = item
'Outputs 1 item "Member1"
MsgBox(assets)
Else
assets = assets & "; " & item
'Outputs 2 items in format "member1; member2"
MsgBox(assets)
End If
Next

If a listbox contains an item LIKE

Is there anywhere to check whether a listbox contains an item which is similar to a string?
I have tried to use a Like statement but this doesn't work.
I have tried:
For i As Integer = 0 To TopicListBox.Items.Count - 1
If (TopicListBox.Items(i).ToString.Contains(Record.Item("Keyphrase2"))) Then
Dim Item As String = TopicListBox.Items(i).ToString
If Item Like "Questions related to:" & Record.Item("Keyphrase2") & ": |*| Qs" Then
Dim ItemSplit() As String = Item.Split(New Char() {"|"c})
Dim AmountOfQuestionsAfter As Integer = AmountOfQuestions + CInt(ItemSplit(1))
Item = ItemSplit(0) & AmountOfQuestionsAfter & ItemSplit(1)
Else
TopicListBox.Items.Add("Questions related to:" & Record.Item("Keyphrase2") & ": |" & AmountOfQuestions & "| Qs")
Exit For
End If
End If
Next
I don't really understand what you are trying to accomplish, but here is a LINQ function to return all the items that contain a certain string.
Dim lb As New ListBox
lb.Items.Add("asdf")
lb.Items.Add("asdfasdf")
lb.Items.Add("aifisasdf")
lb.Items.Add("adf")
'' find all items in the ListBox that contain the string "asdf"
Dim found = lb.Items.Cast(Of String).Where(Function(f) f.Contains("asdf"))
For Each s In found
'' do something with those items
Next
Can't you just do
If Item.Contains("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
or
If Item.StartsWith("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
?
Dim Found = (From Result In lb.Items Where Result.ToString = "Value" Select Result)
If (Found.Count > 0) Then
' your code
End If

Append text to existing row in datatable

I'm trying to make a calendar in vb.net and I have come across this problem. I want to append some text into an existing datatable row. When I watch my debugger it says:"In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.".
Dim aantalRijen As Integer = 1
For x = 0 To 6
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(x))
If dttopdrachten.Rows.Count > aantalRijen Then
aantalRijen = dttopdrachten.Rows.Count
End If
Next
For z = 0 To aantalRijen - 1
Dim r As DataRow
r = dttAgenda.NewRow()
dttAgenda.Rows.InsertAt(r, z)
Next
For i = 0 To 6
Dim aantalItems As Integer = 0
Dim dttopdrachten As New DataTable
dttopdrachten = opdrachtendao.getOpdrachtenByDate(Today.AddDays(i))
aantalItems = dttopdrachten.Rows.Count
For j = 0 To aantalItems - 1
Dim info As String = dttopdrachten.Rows(j).Item(0).ToString & vbCrLf & dttopdrachten.Rows(j).Item(2).ToString & vbCrLf & dttopdrachten.Rows(j).Item(3).ToString & vbCrLf & dttopdrachten.Rows(j).Item(4).ToString & vbCrLf & dttopdrachten.Rows(j).Item(5).ToString & vbCrLf & dttopdrachten.Rows(j).Item(6).ToString
dttAgenda.Rows(j).Item(i) = info
Next
Next
dgvAgenda.DataSource = dttAgenda
In the code above, I first count how many rows I have to make. Afterwards I add the amount of rows to the datatable (columns are added before). Until here it works, but then when I keep debugging I get the error. I tried googling but nothing could help me so far.
Seem problem has been solved without changing anything. So if someone want to make a calendar. Here's the solution ;)