Access VBA - Changing TextBox value from within a function (ByRef) - function

I'm trying to write a sub that will get two parameters - a textbox in a form and a text.
My intention is that the function will append the text into any textbox.
Sub AppendTextBox([any textbox in my form], text As String)
[code that appends the text parameter to the textbox]
End Sub
Please note that I'm not trying to append text to a specific textbox, but to create a function that can receive any textbox in the form and append it with any text.
Thanks for your help.

I've found the answer and it's much simpler than I though it is:
Private Sub AAA(A)
A.Value = "Desired text"
End Sub
Or if you want to append:
Private Sub AAA(A)
A.Value = A.Value & vbnewline & "Desired text"
End Sub

Hi Zephram have you managed to find the solution for this?
I have one but is a bit heavy because it uses loops. If you have one better let me know.
Private Function change_TextBox2(altera As String, textbox As ctl, valor As Variant)
Dim ctl As Control
If altera = "Popula" Then
For Each ctl In Me.Controls
With ctl
If (InStr(.Name, textbox)) > 0 Then
.Value = valor
End If
End With
Next ctl
ElseIf altera = "hide" Then
For Each ctl In Me.Controls
With ctl
If (InStr(.Name, textbox)) > 0 Then
.Visible = False
End If
End With
Next ctl
End If
End Function

can be accomplished by:
dim appendTxt as String: appendTxt = "appended text"
dim ws as Worksheet: for each ws in ActiveWorkbook.Worksheets
dim shape as Shape: for each shape in ws.Shapes
if shape.Type = msoTextBox then
'you can move this code parameterized to a separate function then as req by OP:
with shape.TextEffect: .Text = .Text & appendTxt
end if
next shape
next ws

Related

Controls not changing color in the first page of a report

I have a sub that opens a report "eBouchTest". I loop through controls and change the controls' backcolors. I'm simplifying my question, eventually this will only happen based on a condition but for now I want every combobox, listbox or textbox colored grey.
When running the following piece of code, page 1 of my report does not have it's control's colors changed, but all the other pages do. Why is that so and how can I fix it? Clicking a button launches the following sub with no errors:
Sub ViewReport()
Dim formname As String
Dim ctl As Control
Dim frm As Report
formname = "eBouchTest"
Set frm = Application.Reports(formname)
DoCmd.openreport formname, acViewPreview
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
ctl.BackColor = RGB(200, 200, 200)
Debug.Print ctl.Name & " " & ctl.BackColor
End If
Next ctl
Set ctl = Nothing
End Sub
Page 1 of the report (partial picture only for size purpose):
following pages:
How can I make page 1 change colors as well?
Use the code of formatting control in the detail_format event of report.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim formname As String
Dim ctl As Control
Dim frm As Report
formname = "eBouchTest"
Set frm = Application.Reports(formname)
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
ctl.BackColor = RGB(200, 200, 200)
Debug.Print ctl.Name & " " & ctl.BackColor
End If
Next ctl
Set ctl = Nothing
End Sub
Also use a global variable to set the formatting of control dynamically.
AFIK, only way to effectively control dynamic appearance of controls on report with VBA is code in Format event of section the controls are in. Format events run only for Normal and Preview not Report view.
Can use custom Global variable or intrinsic OpenArgs to pass conditional value to the report. DoCmd.openreport formname, acViewPreview, , , , "some value"
Maybe Conditional Formatting would work for you. This will allow display in Report view. A CF rule can reference the OpenArgs parameter passed to report. Ex:
Can simultaneously select all controls desire to have this rule and set the condition for all in one action.
CF is really the only way to change control appearance between records because VBA code sets property for ALL instances of the control. So if the condition depends on data in each record, use CF.
The first page will also be coloured if I refresh it in one way or another. In my case, the reports are displayed in Landscape mode in print preview, so at the end of the code, I just add
frm.Printer.Orientation = acPRORLandscape
which really doesn't change anything, but for some reason it colors the fields in without adding or changing anything else to the code.

Access 2013 refer to a combobox's own tag in its rowsource

I have some comboboxes on a form and I'd like each box's RowSource to refer to the tag I set. The tag will filter the appropriate dropdown options from a table.
SELECT DropdownNames
FROM Table
WHERE DropdownCategory=[Screen].[ActiveForm]![Me].[Tag]
What is the correct syntax? Is using the tag the best way to do this or is there a better property for this purpose?
Here's example of what I suggested.
Option Explicit
Private Sub Form_Load()
Const DROPDOWN_SQL As String = "SELECT DropdownNames FROM Table WHERE DropdownCategory = "
Const DOUBLE_QUOTES As String = """"
Dim ctl As Control
Dim strRowsource As String
For Each ctl In Me.Controls
With ctl
' Only look at Tagged Combo Boxes
If (.ControlType = acComboBox) And (.Tag <> "") Then
' Set Dropdown Row Source
strRowsource = DROPDOWN_SQL & DOUBLE_QUOTES & .Tag & DOUBLE_QUOTES
.RowSource = strRowsource
End If
End With
Next
Set ctl = Nothing
End Sub

Color field in access 2003

On MS-Access 2003 i've a mask that shows results of a query. For example result of query is:
Column1Column2
1 Y
2 N
3 N
4 Y
It shows in the mask ad a table.
I need to color background field of column2 if the value is Y. To do that i've use the code:
Private Sub Form_Current()
if (Column2) = "Y" Then
Stato.BackColor = vbGreen
End If
End Sub
But it colored all background. So i've tried a workaround:
For Each ctl In Me.Section(acDetail).Controls
If (ctl) = Column2 Then
If (Me.Column2) = "Y" Then
ctl.BackColor = QBColor(2)
End If
End If
But this also colored all bg. Some suggestion?
You can add conditional formatting in code using something like this. This function is based on some code I've used and you may need to tweek it to fit your specific requirements.
Dim fcd As FormatCondition
Dim ctl As control
Dim frm As Form
Dim txt As TextBox
Dim strCond As String
For Each ctl In frm.Controls
If TypeOf ctl Is Access.TextBox Then
If ctl.Visible = True Then
Set txt = ctl
If txt.Name = "Column2" Then
strCond = "=Y"
Set fcd = txt.FormatConditions.Add(acExpression, acEqual, strCond)
fcd.BackColor = QBColor(2)
End If
End If
End If
Next

How to cycle through Textbox Controls Access VBA

I wanted a code that would cycle thru the names of text box controls on a form whose Visible = true. I know this code is constructed incorrectly, but I need to ask if anyone can point me in the right direction.
Public Sub TxtBoxNamevisible(ByRef pfrm As Form)
Dim ctl As Control
Dim txtbx As TextBox
For Each txtbx In pfrm.Controls.visible = true
MsgBox txtbx.Name
Next txtbx
End Sub
pfrm.Controls.visible does not compile because Controls is a collection. Visible is a supported property for members of that collection, but not supported on the collection itself.
Loop through pfrm.Controls, check whether each of them is a Visible text box, and MsgBox the names of those which are ...
Dim ctl As Control
For Each ctl In pfrm.Controls
If ctl.ControlType = acTextBox And ctl.Visible = True Then
MsgBox ctl.Name
End If
Next ctl

MultiSelect Listbox issues

I am having an issue making a listbox into a multiselect listbox. I understand that the code to make a listbox multiselect is:
[forms]![formname]![listboxname].multiselect=2
However when I run this in Private Sub Form_Load() I get run-time error '2448' You can't assign a value to this object.
I guess I don't understand how to make a listbox multiselect, but I am pretty sure I understand how to use the multiselect listbox in VBA.
Any help on how to use the above code to actually change the listbox to multiselect would be appreciated.
From Microsoft Office Help on property MultiSelect:
This property can be set only in form Design view.
Set this property in Form Design, and don't try to change it in code.
Some examples of working with MultiSelect:
' Retrieve all selected values
Public Function ListBoxGetMultiSelect(ByVal rListBox As Access.ListBox) As String
Dim v As Variant
Dim vList As Variant
vList = ""
With rListBox
For Each v In .ItemsSelected
vList = vList & .Column(0, v) & vbCrLf
Next
End With
ListBoxGetMultiSelect = vList
End Function
' clear all selected values
Public Sub ListBoxClearSelection(ByVal rListBox As Access.ListBox)
Dim v As Variant
With rListBox
For Each v In .ItemsSelected
.Selected(v) = False
Next
.Value = Null
End With
End Sub
I've found a way to set Multiselect Property via Vba Code:
DoCmd.SetWarnings = False
DoCmd.OpenForm myformname, acDesign
Forms(myformname).SetFocus
Forms(myformname).Controls(List3).MultiSelect = 0
DoCmd.Close acForm, myformname
DoCmd.SetWarnings = True