iterate through If statment checking checkboxs in vba word - vba

I have a code that works to check singel checkbox
Sub check_if_checked()
If CheckBox1.Value Then
MsgBox "checked"
Else
MsgBox "not checked"
End If
End Sub
What i want to do is to create a loop where in the if statment i can replace the check box number with the loop iteration variable.
Something like
If "CheckBox" & i.Value Then
Can this be done or maybe done a diffrent way?

In the below example I have 4 Check Boxes with the following names CheckBox1, CheckBox2, CheckBox3, CheckBox4.
Sub check_if_checked()
Dim i As Integer
i = 1
Do While i <= 4
If (IsNull(Me("CheckBox" & i).value)) Then _
MsgBox ("CheckBox" & i & " is Null") Else _
MsgBox (Me("CheckBox" & i).value)
i = i + 1
Loop
End Sub
Or the following may work for you:
Sub check_if_checked()
Dim TempCntrl As Control
For Each TempCntrl In UserForm1.Controls
If TempCntrl.Name Like "CheckBox*" Then
MsgBox (TempCntrl.Name & ": " & TempCntrl.value)
End If
Next
End Sub

Related

Search box not allowing me to type a space between words

I have a textbox on a form that is filtering my data by the company name. The reason for the close and open code in the error handling is because I couldn't find a way to easily fix it throwing an error when a combination of characters not present would be entered. This way it just closes and reopens it and basically resets it. I am still fairly new to this development and all I know is taught to myself through google and forums like this so forgive my lack of understanding when things should make sense to someone else able to do these types of functions.
Upon typing part of a company name and pressing space to type in a second word it essentially removes the space and puts the cursor back to the last letter typed.
This is the code for the textbox.
Reminder that I find solutions to the functions I need and adapt the code as best I can to suit my needs. I don't pretend to fully comprehend what I use yet and I'm still learning.
Private Sub txtSearch_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
'Apply or update filter based on user input.
If Len(txtSearch.Text) > 0 Then
filterText = txtSearch.Text
Me.Form.Filter = "[tblSuppliers]![SupplierName] like '*" & filterText & "*'"
Me.FilterOn = True
'Retain filter text in search box after refresh
txtSearch.Text = filterText
txtSearch.SelStart = Len(txtSearch.Text)
Else
'Remove filter
Me.Filter = ""
Me.FilterOn = False
txtSearch.SetFocus
End If
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
DoCmd.Close acForm, "frmCosteeDetails", acSaveNo
DoCmd.OpenForm "frmCosteeDetails"
End Sub
In my search to try and find a way to fix the removal of spaces I found this function that someone listed but wasn't sure how to integrate it into my code.
Public Function FindWord(varFindIn As Variant, varWord As Variant) As Boolean
Const PUNCLIST = """' .,?!:;(){}[]/"
Dim intPos As Integer
FindWord = False
If Not IsNull(varFindIn) And Not IsNull(varWord) Then
intPos = InStr(varFindIn, varWord)
' loop until no instances of sought substring found
Do While intPos > 0
' is it at start of string
If intPos = 1 Then
' is it whole string?
If Len(varFindIn) = Len(varWord) Then
FindWord = True
Exit Function
' is it followed by a space or punctuation mark?
ElseIf InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
Else
' is it precedeed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos - 1, 1)) > 0 Then
' is it at end of string or followed by a space or punctuation mark?
If InStr(PUNCLIST, Mid(varFindIn, intPos + Len(varWord), 1)) > 0 Then
FindWord = True
Exit Function
End If
End If
End If
' remove characters up to end of first instance
' of sought substring before looping
varFindIn = Mid(varFindIn, intPos + 1)
intPos = InStr(varFindIn, varWord)
Loop
End If
End Function
Edit - Code for Current Solution
Private Sub txtSearch_Change()
On Error GoTo errHandler
'clear filter
If Len(txtSearch.Text) = 0 Then
FilterOn = False
Filter = vbNullString
Exit Sub
End If
'apply filter
Filter = "[SupplierName] like '*" & txtSearch.Text & "*'"
FilterOn = True
Leave:
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
Resume Leave
'DoCmd.Close acForm, "frmCosteeDetails", acSaveNo
'DoCmd.OpenForm "frmCosteeDetails"
End Sub
A simple filter method is as shown below. You will need to handle the Change() event and use the Text property which is populated on every keystroke.
Also, filtering does not requery data, so no need to try to manually retain the search value.
The below assumes the txtSearch and the data due to be filtered are on the same form. If that's not the case, the reference to the data-form will need to be changed.
Private Sub txtSearch_Change()
On Error GoTo errHandler
'clear filter
If Len(txtSearch.Text) = 0 Then
FilterOn = False
Filter = vbNullString
Exit Sub
End If
'apply filter
Filter = "[SupplierName] like '*" & txtSearch.Text & "*'"
FilterOn = True
Leave:
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbInformation + vbOKOnly, "Information"
Resume Leave
End Sub
Rather than using a text box to constantly update as typed just change it to a search button that when clicked searches based on the value in the search box. All you have to do is then update the search criteria each time and click search. a bit slower but functions mostly the same.
Private Sub cmdSearch_Click()
Dim strWhere As String
strWhere = "[tblSuppliers]![SupplierName] Like '*" & Me.txtSearch & "*'"
'Apply Filter
Me.Filter = strWhere
Me.FilterOn = True
End Sub

VBA Search Using Text Box in Access

I'm very new to both Access and VBA. I've created a search button that looks for different items depending on what is selected on the different combo boxes. However, I would like to add another search criteria where if I type text into a texbox called "txtNotes" I can look for records that look like a match on a table field called "Notes" inside the "tbl_ContructionOrders" table. The matches have to be somewhat loose as everyone types notes differently and would like to use this box to find work orders where we had similar issues and maybe have an easier way finding a solution for said problems.
This is what I have so far and it is not working
Private Sub btnLookup_Click()
Dim strWhere As String
Me.Filter = True
strWhere = ""
If IsNull(Me.txtNotes) Then
If Not IsNull(Me.txtNotes) Then
If strWhere <> "" Then
strWhere = strWhere & " like [Notes] = """ & Me.txtNotes & """"
Else
strWhere = strWhere & "[Notes] = """ & Me.txtNotes & """"
End If
End If
If Len(strWhere) <= 0 Then
MsgBox "No Criteria", vbInformation, "No Input."
Else
Me.Filter = strWhere
Me.FilterOn = True
Me.Requery
End If
If Me.FilterOn Then
If Me.Recordset.RecordCount = 0 Then
MsgBox "Nothing Found."
End If
End If
End Sub
Try this:
Private Sub Command0_Click()
'empty notes
If IsNull(txtNotes.Value) Then
MsgBox "No Criteria", vbInformation, "No Input."
Exit Sub
End If
'search
Filter = "[Notes] Like '*" & txtNotes.Value & "*'"
FilterOn = True
'count records
If RecordsetClone.RecordCount = 0 Then
MsgBox "Nothing Found."
FilterOn = False
End If
End Sub
If you want to search (filter) as you type, use this:
Private Sub txtNotes_Change()
'search after x number of chars
If Len(txtNotes.Text) <= 3 Then
FilterOn = False
Filter = vbNullString
Else
Filter = "[Notes] Like '*" & txtNotes.Text & "*'"
FilterOn = True
End If
End Sub
It sounds like you want an auto-complete object. Jut the words that are to be auto-filled in a table. Use a combo box control on the form to select the word. Here is an instructional video that shows you how to set this up.
https://www.youtube.com/watch?v=ptRb8ffv4f0
If you need something else, post back.

Quick Filters in a Split Form Datasheet

Creating an access database for work. Users will use a split form with only the datasheet visible to review and manipulate numeric data. On the form I have built in quick filters that consist of of textboxes in which the values are either raised or lowered with arrow buttons that have on-click events. I currently have the text boxes linked to the recordsource query criteria.
With all of this stated, the problem that I am having is that I need the filter to act in the following manner:
If the value in the text box equals 0 I want to see all records. If the value is greater than 0, I want all records greater than or equal to the text box value to show. Finally, if the value in the text box is less than 0, I want to see all values less than or equal to 0.
I have considered trying to use multiple sql statements but I typically have about 3 of these quick filters on each form, and my project will eventually have about 20 forms. That is a lot of sql statements to potentially mess up.
What ideas do you guys have to solve this problem? I really need help.
If you only have 1 textbox on each form, then you may want to consider using the form's Filter property:
Private Sub txtFilter_AfterUpdate()
On Error GoTo E_Handle
If Not IsNull(Me!txtFilter) Then
If IsNumeric(Me!txtFilter) Then
Select Case Me!txtFilter
Case Is < 0
Me.Filter = "Price<=0"
Me.FilterOn = True
Case 0
Me.FilterOn = False
Case Is > 0
Me.Filter = "Price>=" & Me!txtFilter
Me.FilterOn = True
End Select
End If
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!txtFilter_AfterUpdate", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
If need to have multiple filters, then consider moving the filter creation to a procedure by itself that handles all cases, and just call this procedure from each text box. You may have to think about the logic here of what happens if one text box is 0 (No filter), but another text box is 5 (display all values >= 5) and another text box is -3 (display all values <= 0):
Private Sub txtFilter2_AfterUpdate()
On Error GoTo E_Handle
Call sFilterForm2
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!txtFilter2_AfterUpdate", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Private Sub sFilterForm2()
On Error GoTo E_Handle
Dim strSQL As String
If Not IsNull(Me!txtFilter) Then
If IsNumeric(Me!txtFilter) Then
Select Case Me!txtFilter
Case Is < 0
strSQL = " AND Price<=0 "
Case 0
Case Is > 0
strSQL = strSQL & " AND Price>=" & Me!txtFilter
End Select
End If
End If
If Not IsNull(Me!txtFilter2) Then
If IsNumeric(Me!txtFilter2) Then
Select Case Me!txtFilter2
Case Is < 0
strSQL = " AND Price<=0 "
Case 0
Case Is > 0
strSQL = strSQL & " AND Price>=" & Me!txtFilter2
End Select
End If
End If
If Len(strSQL) > 0 Then
strSQL = Mid(strSQL, 5)
Me.Filter = strSQL
Me.FilterOn = True
Else
Me.FilterOn = False
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form2!sFilterForm2", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards
Given a text box control CriteriaField1 and a corresponding field to filter Field1 in the record source I would use this:
Private Sub CriteriaField1_AfterUpdate()
Const FIELD_NAME As String = "Field1"
Dim value As Long
value = Nz(Me("Criteria" & FIELD_NAME).Value, 0)
Dim condition As String
condition = FIELD_NAME & IIf(value < 0, " <= 0", " >= " & value)
Me.FilterOn = value <> 0
End Sub
If you need to combine multiple fields to a filter condition, you would have to use and set form-global variables instead of local ones.
You could call a helper function which holds a set of arrays and builds and sets the filter dynamically:
Private Sub Filter0_AfterUpdate()
SetFilter
End Sub
Private Sub Filter1_AfterUpdate()
SetFilter
End Sub
Private Sub Filter2_AfterUpdate()
SetFilter
End Sub
Private Sub SetFilter()
Dim FieldNames() As Variant
Dim TextboxNames() As Variant
Dim Criteria() As String
Dim Index As Integer
Dim Value As Long
' Specify the field names to filter on.
FieldNames = Array("Quantity", "Stock", "Size")
' Specify the names of the textboxes to enter filter values.
TextboxNames() = Array("Filter0", "Filter1", "Filter2")
ReDim Criteria(LBound(TextboxNames) To UBound(TextboxNames))
For Index = LBound(Criteria) To UBound(Criteria)
Value = Val(Nz(Me(TextboxNames(Index)).Value))
If Value < 0 Then
Criteria(Index) = FieldNames(Index) & " <= 0"
ElseIf Value > 0 Then
Criteria(Index) = FieldNames(Index) & " >= " & CStr(Value)
Else
Criteria(Index) = "True"
End If
Next
' Assemble and apply the filter.
Me.Filter = Join(Criteria, " And ")
Me.FilterOn = True
Debug.Print Me.Filter
End Sub

Determining the Number of Lines in a Textbox in VBA

I have a textbox set up in a GUI where the user can enter information. This string is then spit out in a textbox within a PPT slide. Depending on the number of lines used in the textbox within the PPT slide, I need to enter the next set of information so many new lines below the text from the textbox. Here is what I have so far:
This is the code that takes the text the user enters in the textbox within the GUI and places it in the textbox within the PPT slide:
Private Sub Location()
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'Make sure there is text in the call to action textbox. If not, display an error message.
If C2AText = "" Then
MsgBox "Woah there! You need to enter text in the location/call to action box."
'Otherwise, if text is inserted, place that text in the WarningData box found on the PPT slide.
Else
.TextRange = C2AText
.TextRange.Paragraphs.Font.Size = 21
.TextRange.Paragraphs.Font.Name = "Calibri"
.TextRange.Paragraphs.Font.Shadow.Visible = True
.TextRange.Paragraphs.Font.Bold = msoTrue
End If
End With
End Sub
This text determines whether or not anything is selected in the HailInfo drop down. If it is, I need to place this text so many lines below the C2AText that was inserted in the previous Sub:
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
ElseIf HailDropDown <> "" And C2AText.LineCount = 2 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
ElseIf HailDropDown <> "" And C2AText.LineCount = 3 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
Using the C2AText.LineCount within the HailInfo sub does not appear to do anything. It will not insert the hail text anywhere, so I am not sure what I am doing wrong. Any help would be greatly appreciated...thanks!!
You should try the following ...
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
Else
.TextRange.Text = .TextRange.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
You were only referencing .TextRange, rather than .TextRange.Text.
Also, because you need to add the text at the end, you only need an Else condition, rather than two ElseIfs that both do the same thing! ;0)
More example code ... https://msdn.microsoft.com/en-us/library/office/ff822136.aspx

adding multiple messgeboxes values to single messagebox in vba

I have this code with me where i can display the message when every outer loop ends. I want to catch all these messages in suppose array or soome list like structure and then at the end want to display each of these messages into one msgbox. Would appreciate if someone could help me.
Thanks.
For Each objNavFolder In objNavGroup.NavigationFolders
SkippedItemCounter = 0
If oItems.Count = 0 Then
MsgBox "No Appointments items in " & objNavFolder.DisplayName & "'s folder"
Else
NextRow = NextRow + 1
For Each MyItem In oItems
If MyItem = "" Then
SkippedItemCounter = SkippedItemCounter + 1
End If
'some code here
Next
Set objExpl = _colExpl.Add(objFolder, olFolderDisplayNormal)
NextRow = NextRow - 1
End If
MsgBox "No. of items= "&SkippedItemCounter&"skipped from"&objNavFolder.DisplayName&""
Next
End If
End If
End If
instead of calling msgboxes, create a String and keep adding the messages - at the end of code msgbox(yourString)
for example
decalare a string before the main sub
Dim yourFinalMessage As String ' or Dim yourFinalMessage$
instead of
MsgBox "No Appointments items in " & objNavFolder.DisplayName & "'s folder"
say
yourFinalMessage = yourFinalMessage & vbCrLf & & _
"No Appointments items in " & objNavFolder.DisplayName & "'s folder"
keep doing this until the loop ends.
at the end of loop say
msgbox YourFinalMessage
Not sure to exactly understand what you want, but you might try to add this to a module:
Option Explicit
Dim globalMsg as String
globalMsg = ""
Function customMsg(msg as String)
MsgBox msg
globalMsg = globalMsg & VbCrLf & msg
End Function
Just call customMsg("Your Message") to display a MsgBox and at the end, call MsgBox globalMsg to display all the messages as a single message (one per line). There are a lot of other ways to do this, it depends on you. Please be more explicit if you want any further help.