Access VBA programmatically setting font/size not working - ms-access

I have a button that is supposed to change the font face and size of a textbox to Tahoma 8pt. The button event is:
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.FontSize = 8
Me.InventoryDescription.FontName = "Tahoma"
End Sub
Unfortunately, the text does not change. I'm testing it by first editing the font and size by hand, and then pressing my button.
However, if I do the following,
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.Value = "hello"
Me.InventoryDescription.FontSize = 24
Me.InventoryDescription.FontName = "Times"
End Sub
The text changes to "hello" of course, but the font and size do indeed change. (I used Times 24pt because the default for the textbox is Tahoma 8pt and I wanted to make sure it wasn't just reverting to the default) This made me think that the textbox needs to have the focus to make the changes. So, I tried:
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.SetFocus
Me.InventoryDescription.FontSize = 24
Me.InventoryDescription.FontName = "Times"
End Sub
But, no go.
Soooo, what am I doing wrong?
I found one aspect to the problem. The text box .TextFormat is set to Rich Text. If I change it to Plain Text, then the button effect works. However, the reason it is set to Rich Text is to allow italics. So, I tried first setting it to plain text, and then changing the font/size, but that didn't work either.

I have the same need: I want the users to be able to format the text with boldface, italics and underlined characters, but I don't want to allow font name changes, or font size changes. And copy/paste actions often import formatted text in my textBox, which needs to be "cleaned".
The solution I found is in the Function below.
This function should be called by an Event Procedure (i.e. After Update, or On Click).
Public Function CleanRichText(strTEXT, strFont, nSize)
'*****************************************************
'
For i = 1 To 9
strTEXT = Replace(strTEXT, "size=" & i, "size=" & nSize)
Next i
strTEXT = Replace(strTEXT, "font face", "font_face")
strTEXT = Replace(strTEXT, "font" & Chr(13) & Chr(10) & "face", "font_face")
Do While InStr(1, strTEXT, "font_face=" & Chr(34)) > 0
iCut1 = InStr(1, strTEXT, "font_face=" & Chr(34))
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(34))
strLeft = Left(strTEXT, iCut1 - 1) & "font_face=Face"
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
Do While InStr(1, strTEXT, "font_face=") > 0
iCut1 = InStr(1, strTEXT, "font_face=")
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(32))
strLeft = Left(strTEXT, iCut1 - 1) & "font face=" & strFont & Chr(32)
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
CleanRichText = strTEXT
End Function
Private Sub Cause_AfterUpdate()
Me.Cause = CleanRichText(Me.Cause, Me.Cause.FontName, 2)
End Sub

Your code works for me. Try adding Me.Repaint after changing the font.

Your code worked for me using Access 2010.
I added this code to the click event for a couple command buttons that allow the user to dynamically control the font size in a List box called teachersList.
Private Sub cmdDecreaseFont_Click()
Me.teachersList.FontSize = Me.teachersList.FontSize - 1
End Sub
Private Sub cmdIncreaseFont_Click()
Me.teachersList.FontSize = Me.teachersList.FontSize + 1
End Sub
Then added a couple simple command buttons to the form

I started using the #yves solution but it fails when a "font" tag includes a "size" property, for example:
<font face="Arial" size="5">a colour in it will break?</font>
So I found a better way using #RegExp, you can follow the thread in this forum: accessforums.net
Public Function CleanRichTextRegEx(ByVal strText As String, _
ByVal strFont As String, _
ByVal nSize As Integer) As String
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
On Error Resume Next
With objRegEx
.Global = True
'Replace font size
.Pattern = "size=[0-9]"
strText = .Replace(strText, " size=" & nSize)
'Replace font face
.Pattern = "face=([""'])(?:[\r\n]*(?=(\\?))\2.)*?\1"
strText = .Replace(strText, "face=" & strFont)
End With
Set objRegEx = Nothing
CleanRichTextRegEx = strText
End Function
And you can use it in this way:
richText = "<font face='Arial' size='5'>a colour in it will break?</font>"
richTextResult = CleanRichTextRegEx(richText, "Arial", 2)

Related

Export formatted bullets to an email from Excel TextBox

I've been working on a spread sheet to allow my team to manage our workload more effectively, whilst the business is developing a new tool. Anyway, what the sheet does is inject information, then at the click of a button, it populates an OFT email template so that the info can be sent out.
Problem is, we rely heavily on bullet lists for our emails, and I'm really struggling to find a way of adding bullets effectively from an ActiveX Textbox.
At the moment, I have a button which adds the follow to a text box:
[bullets]
* Bullet 1
* Bullet 2
* Bullet 3
[/bullets]
I then have Replace statements that look for strings and it replaces them with the appropriate HTML tags. Here's the code:
' Add HTML formatting to text updates so it displays correctly in the email.
LatestUpdate.Text = Replace(LatestUpdate, "[bullets]", "<ul>")
LatestUpdate.Text = Replace(LatestUpdate, "[/bullets]", "</ul>")
LatestUpdate.Text = Replace(LatestUpdate, "* ", "<li>")
LatestUpdate.Text = Replace(LatestUpdate, vbCrLf, "<br>")
The problem I'm having, is that non-technical people are using this document, so I would really like to have it in such a way were they don't have to look at the markup, but can simple add bullets straight from the textbox.
I was originally thinking about replacing "* " with "< li >" however, that doesn't add the correct < ul > tags, so it's not actually a bullet list within the email.
Can anyone help in simplifying this process for the end users please? I'm really stuck.
The holy grail would be to enable rich text formatting on the textbox, but I don't believe that's possible from all the research I've done?
TIA.
Based on your last comment, what you are looking for is not just a bullet point in your textbox but indentation as well. So here is an attempt at it:
First add the below in your <textbox>_KeyUp function:
Private Sub txtBulletPoints_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim STRING_LENGTH As Long: STRING_LENGTH = 49
Dim aLine() As String
Dim aLineSpace() As String
Dim iC As Integer
Dim sText As String
Dim bUpdate As Boolean
' Only do this if there is a string to work with
If Len(Me.txtBulletPoints.Text) > 0 Then
' Set initial values
aLine = Split(Me.txtBulletPoints.Text, vbCrLf)
bUpdate = False
' First lets indent the last line if we need to
If Left(aLine(UBound(aLine)), 2) = "- " Then
For iC = LBound(aLine) To UBound(aLine)
If iC = UBound(aLine) Then
sText = sText & vbTab & aLine(iC)
Else
sText = sText & aLine(iC) & vbCrLf
End If
Next
Me.txtBulletPoints.Text = sText
End If
' Now the tricky bit. Check if we have reached the end of the
' line so that we can indent the text into the next line
If (Len(aLine(UBound(aLine))) >= STRING_LENGTH) And (InStr(1, aLine(UBound(aLine)), vbTab) = 1) Then
For iC = LBound(aLine) To UBound(aLine)
If iC = UBound(aLine) Then
aLineSpace = Split(aLine(iC), " ")
' As we have to indent the last bullet point line, call the finction to do that
sText = sText & SetIndentsInString(aLine(iC), STRING_LENGTH)
Else
sText = sText & aLine(iC) & vbCrLf
End If
Next
Me.txtBulletPoints.Text = sText
End If
End If
End Sub
Now add the below UDF where your form code is (essentially at the same place where your <textbox>_KeyUp function is):
Function SetIndentsInString(ByVal sString As String, ByVal iIndentLen As Long) As String
Dim iC As Long
Dim iLastTab As Long: iLastTab = 0
Dim aSpace() As String
Dim aTab() As String
Dim sCurString As String
' Check if the string is the same as what it was last
' time (sLastString is a private module variable initialised
' to "" when the form is activated)
If Replace(sString, vbTab, "") = Replace(sLastString, vbTab, "") Then
' Its the same string so lets return it as is
SetIndentsInString = sString
Else
' Its not the same string so set initial values
sLastString = sString
SetIndentsInString = ""
' Loop to see how many lines we have based on number of TABs in the string
Do While InStr(iLastTab + 1, sString, vbTab) > 0
iLastTab = iLastTab + InStr(iLastTab + 1, sString, vbTab)
Loop
' If there is only 1 TAB, simply indent the line
If iLastTab = 1 Then
aSpace = Split(sString, " ")
SetIndentsInString = Mid(sString, 1, Len(sString) - Len(aSpace(UBound(aSpace)))) & vbTab & " " & aSpace(UBound(aSpace))
Else
' More then 1 TAB.. damn!. Ok well lets work it
aTab = Split(sString, vbTab)
sCurString = aTab(UBound(aTab))
' Check if the last line of our bullet point has more characters then allowed in a line
If Len(sCurString) >= iIndentLen Then
' It does. Now loop through all the lines in our bullet point and set the last character in a new line with indent
aSpace = Split(sCurString, " ")
For iC = LBound(aTab) To UBound(aTab)
If iC = UBound(aTab) Then
SetIndentsInString = SetIndentsInString & Mid(sCurString, 1, Len(sCurString) - Len(aSpace(UBound(aSpace)))) & vbTab & " " & aSpace(UBound(aSpace))
Else
SetIndentsInString = SetIndentsInString & aTab(iC) & vbTab
End If
Next
Else
' It doesnt. Loop through and send the string back
SetIndentsInString = sString
End If
End If
End If
End Function
Now in the same module, make the following declaration at the top:
Private sLastString As String
Essentially the above will act like a bullet point as it would be in a Rich Text box. Things to remember is that you will have to set STRING_LENGTH to the number of characters your textbox will take in a given bullet point line (you will have to play around with that). Below is a screen print of how it worked for me

How can I determine the difference between typing into a combo box and selecting from a drop down in Access VBA?

This question was asked in the topic with a similar name earlier, but the answer provided didn't really indicate HOW those events would help determine whether somebody was typing in the combo box or selecting an item in the list. I think that it really answered the other question about how to determine when somebody was done typing, but without seeing the event handlers, I can't be sure.
Unfortunately, I'm new here and don't have enough reputation to post a comment asking for clarification, so I have to start a new question. Here's what I'm trying to do:
I have a form with a combo box in the Header and, as I type in the combo box, I want the characters that I've typed to be used as a filter on the Details part of the form. Both the combo box control source and the form's record source use the same query string.
I've tried numerous iterations of the code below, but I can't get it to work correctly.
Private Sub cmbAppName_Change()
Dim strApp As String
Dim nSelStart As Integer
Dim nSelLen As Integer
Dim nSelected As Integer
Dim strMsg As String
On Error GoTo ERR_SUB
strMsg = ""
Me.cmbAppName.SetFocus
' Get current selection details
nSelStart = Me.cmbAppName.SelStart
nSelLen = Me.cmbAppName.SelLength
nSelected = Me.cmbAppName.ListIndex
Me.cmbAppName.SetFocus
strApp = Nz(Me.cmbAppName.Text, "")
Debug.Print "Index = " & nSelected & "; SelStart = " & nSelStart & "; SelLen = " & nSelLen
If nSelected = -1 Then
Debug.Print "Change by typing: " & strApp
Else
Debug.Print "Change by list selection: " & strApp
End If
' Get the part of the text that the user has typed
If nSelStart > 0 Then
strApp = Left(strApp, nSelStart)
Debug.Print "App piece = '" & strApp & "'"
End If
' If there is text, set a filter (MatchAppName = InStr(strApp, datbase_column_value)
If strApp <> "" Then
Me.Filter = "MatchAppName('" & strApp & "', " & DCApplications_Application_Col & ") > 0"
Me.FilterOn = True
' Me.txtApplication.SetFocus
' Call DoCmd.FindRecord(strApp, acStart, False, acSearchAll, False, acCurrent, True)
' Me.cmbAppName.SetFocus
Else
Me.Filter = ""
Me.FilterOn = False
End If
EXIT_SUB:
' Restore the selection in the combo box's text box
Me.cmbAppName.SetFocus
Me.cmbAppName.SelStart = nSelStart
Me.cmbAppName.SelLength = nSelLen
Exit Sub
ERR_SUB:
If ERR.Number = 2185 Then
strApp = Nz(Me.cmbAppName.Value, "")
Me.cmbAppName.SetFocus
Debug.Print "Using " & strApp
Resume Next
End If
Me.Filter = ""
Me.FilterOn = False
Debug.Print ErrorMessage(ERR.Description, "cmbAppName_Change", ERR.Number, "Value = '" & Me.cmbAppName.Value & "'", False)
Resume EXIT_SUB
End Sub ' cmbAppName_Change
As you can see from the error handling code, I'd often get an error 2185 telling me that my control didn't have focus when using the Text property despite having a SetFocus call right before it.
If somebody selects from the list (either by clicking or moving the selection), I'd like to go to that record, but I at least need the above piece working first.
After searching the Web, I found out that a Details section with zero records causes the 2185 error. Apparently, filtering like that causes problems when all records are filtered out.
The solutions on the Web said that you can set the Allow Additions property of the form to True, but that always displays one row in the Details section. This can be especially confusing if the rows in the Details section contain controls, which will be displayed in the "addition" row. Also, I would still get an error typing additional characters after the one that caused the Details section to have zero records.
Eventually, I replaced the combo box with a simple text control to filter the Details section. When the Details section has rows, I turn Allow Additions off and make the controls visible; when it doesn't have rows, I turn Allow Additions on and hide the controls.
Here's the code that I used:
Private Sub txtApplicationFilter_Change()
Dim strApp As String
Dim nSelStart As Integer
Dim nSelLen As Integer
Dim strFilter As String
Dim strQuery As String
Dim strWhere As String
Dim nRecs As Integer
On Error GoTo ERR_SUB
' Save text selection
nSelStart = Me.txtApplicationFilter.SelStart
nSelLen = Me.txtApplicationFilter.SelLength
' Get application name typed and selection information
strApp = Nz(Me.txtApplicationFilter.Text, "")
strFilter = "[" & DCApplications_Application_Col & "] LIKE '*" & EscapeQuotes(strApp) & "*'"
nRecs = DCount("[" & DCApplications_Application_Col & "]", LocalTableName(DCApplications_Tab), strFilter)
' Kludge code to prevent various errors (like 2185) when no records are returned in the form
Call UpdateList(nRecs)
' Update the record source to reflect the filtered list of apps
strWhere = " WHERE APPS." & strFilter
strQuery = strSelect & strFrom & strWhere & strOrderBy
Me.RecordSource = strQuery
' 20200423 SHM: Restore or update filter to avoid issues with Delete and Backspace and applications with spaces in their names
Me.txtApplicationFilter.SetFocus
Me.txtApplicationFilter = strApp
Me.txtApplicationFilter.SelStart = nSelStart
Me.txtApplicationFilter.SelLength = nSelLen
EXIT_SUB:
Me.btnAddNew.enabled = (Nz(Me.txtApplicationFilter, "") <> "")
Exit Sub
ERR_SUB:
' NOTE: ErrorMessage is a helper function that basically displays a form displaying the error
Call ErrorMessage(ERR.Description, "txtApplicationFilter_Change", ERR.Number, "Filter = " & strApp & " Records = " & nRecs)
Resume EXIT_SUB
Resume Next
End Sub ' txtApplicationFilter_Change
Private Sub UpdateList(nRecs As Integer)
Dim bShowControls As Boolean
On Error GoTo ERR_SUB
bShowControls = (nRecs > 0)
' Kludge code to turn off checkbox control source
If bShowControls Then
strSelect = strSelectStart & ", (" & strAppUser & ") AS " & strCtrlSource
Me.chkTestedByMe.ControlSource = strCtrlSource
Else
strSelect = strSelectStart
Me.chkTestedByMe.ControlSource = ""
End If
' Kludge code to prevent various errors (like 2185) when no records are returned in the form
' Turning on AllowAdditions prevents errors when no records are returned.
' However, that puts an empty row in the form, but the controls are showing, so we have to hide them to prevent confusing the user.
Me.AllowAdditions = Not bShowControls
Me.btnAddExisting.visible = bShowControls
Me.chkTestedByMe.visible = bShowControls
EXIT_SUB:
Exit Sub
ERR_SUB:
Call ErrorMessage(ERR.Description, "UpdateList", ERR.Number, " Records = " & nRecs)
Resume EXIT_SUB
Resume Next
End Sub ' UpdateList
I would use a work around to settle this issue
A simple code bellow demonstrate the work around using Tag property of Combo Box and keypress event along with change event, I hope it can be applied in your code
Private Sub Combo2_Change()
If Combo2.Tag = 1 Then
Text4 = "change - from key"
Else
Text4 = "change - from select"
End If
Combo2.Tag = 0
End Sub
Private Sub Combo2_KeyPress(KeyAscii As Integer)
Combo2.Tag = 1
End Sub
Don't forget to set Tag property of Combo Box to 0 on design view to avoid error at comparing empty Tag with number

How to create controls at run time Access VB?

How can you create controls at run time with VB code in Microsoft Access? after some digging I found that this is possible with the CreateControl function. The problem is every random forum I find online has some code similar to this:
Private Sub Button_Click()
Call AddLabel
End Sub
Function AddLabel()
Set ctl = CreateControl("MyForm", acLabel, acDetail, "", "", 0, 0, 100, 100)
With ctl
.name = "myTextBox"
.Caption = "Hello World!"
.Height = 50
.Width = 100
.FontSize = 11
.Visible = True
End With
End Function
but this code appears to not create a visible label.
In my case I'm just trying to learn how to get this to work. So I created a blank form with a button that when clicked will create a label that says "Hello world!". What I'm expecting to happen is a textbox will display in the top left of the form when the button is clicked. I'm curious if anyone could help show me a simple example of some code that actually works.
Before anyone says I can create a label and hide it then change its visibilty property, I know. But I'd like to know how to create controls dynamically and getting this simple example to actually work will greatly help my understanding.
The documentation you need is here (these are specifically for Access VBA):
Application.CreateControl Method (Office 2007)
Application.CreateControl Method (Office 2003)
According to the documentatin, there are some big limitations to this feature:
Limited to 754 controls over the lifetime of the form (this is not reset by deleting them, so you are likely to run into this limit quickly)
Must be done in Design view (so it can't be done in mde/accde)
It is questionable how it will perform in a multi-user environment.
Because of these limitations, it is inadvisable, unless you are using to design forms initially.
Duplicate Question: How do you dynamically create controls on a MS Access form?
In response to the OP's suggestion, here is my test code which was able to add 40 controls and repeat the process 50 times without exceeding the 754 limit (I reused 40 names in my test).
Caveat 1 This is inadvisable because it can only be done in design view which will not work in an mde/accde.
Caveat 2: It is questionable how it will perform in a multi-user environment.
This code is from a form with two buttons. It opens a second form named "Form2"
Option Compare Database
Option Explicit
Private Const FORM_NAME As String = "Form2"
Private m_nCounter As Long
Private Sub cmdCreate_Click()
runDynamicForm
End Sub
Private Sub cmdRepeat_Click()
Dim n As Long
m_nCounter = 0
For n = 0 To 50
runDynamicForm
DoEvents
DoCmd.Close acForm, FORM_NAME, acSaveNo
DoEvents
Next 'n
MsgBox m_nCounter
End Sub
Private Sub runDynamicForm()
Const DYNAMIC_TAG As String = "dynamic"
Dim n As Long
Dim frm As Form
Dim ctl As Access.Control
On Error GoTo EH
Application.Echo False
DoCmd.OpenForm FORM_NAME, acDesign
Set frm = Application.Forms(FORM_NAME)
For n = frm.Controls.Count - 1 To 0 Step -1
Set ctl = frm.Controls(n)
If ctl.Tag = DYNAMIC_TAG Then
Application.DeleteControl FORM_NAME, ctl.Name
End If
Next 'n
For n = 1 To 20
With Application.CreateControl(FORM_NAME, acLabel, acDetail, , , 400, n * 300, 1500, 300)
.Name = "lbl" & n
.Caption = "Question " & n
.Visible = True
.Tag = DYNAMIC_TAG
End With
With Application.CreateControl(FORM_NAME, acTextBox, acDetail, , , 2000, n * 300, 3000, 300)
.Name = "txt" & n
.Visible = True
.TabIndex = n - 1
.Tag = DYNAMIC_TAG
End With
m_nCounter = m_nCounter + 2
Next 'n
DoCmd.Close acForm, FORM_NAME, acSaveYes
DoCmd.OpenForm FORM_NAME, acNormal
GoTo FINISH
EH:
With Err
MsgBox "Error:" & vbTab & .Number & vbCrLf _
& "Source" & vbTab & .Source & vbCrLf _
& .Description
End With
FINISH:
Application.Echo True
End Sub
I took that upove code and simplified it as it was long winded, and turned it into a a sample code for my own future use. Hope it helps someone in the future.
Public Sub runDynamicCreateControls()
Dim FormName As String
Dim NumControls As Integer
Dim n As Long
Dim ctl As Access.Control
Dim ctlname As String
On Error GoTo EH
Application.Echo False
FormName = "createcontrolF" 'Input Name of Form
NumControls = 20 'input number of controls
ctlname = "txt" 'input control name
DoCmd.OpenForm FormName, acDesign
For n = 1 To NumControls
With Application.CreateControl(FormName, acTextBox, acDetail, , , 1000,1000, 1100, 600)
.Name = ctlname & "_" & n
.Visible = True
.Tag = n
End With
Next 'n
DoCmd.Close acForm, FormName, acSaveYes
DoCmd.OpenForm FormName, acNormal
GoTo FINISH
EH:
With Err
MsgBox "Error:" & vbTab & .Number & vbCrLf _
& "Source" & vbTab & .Source & vbCrLf _
& .Description
End With
FINISH:
Application.Echo True
End Sub
Whenever I attempt to run your code I get the runtime error of:
Run-time error '6062':
You must be in Design or Layout View to create or delete controls.
Based off of that information it seems like dynamically creating controls in runtime isn't going to be possible.
You might be only missing DoCmd.Restore, here is an example on how to dynamically create form, data bind it, and create controls, all in runtime.
Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub

runtime error 2455 for textbox.selStart property

I am using this simple code to filter my report. Textbox Text123 is in the header of the report.
Private Sub Text123_KeyUp(KeyCode As Integer, Shift As Integer)
Dim str As String
str = Me.Text123.Text
If Me.Text123 = "" Then
Me.Report.filter = ""
Me.Report.FilterOn = False
Else
Me.Report.filter = "[ItemNo] Like '*" & Me.Text123.Text & "*'"
Me.Report.FilterOn = True
End If
Me.Text123.SetFocus
Me.Text123.Text = str
Me.Text123.SelStart = Len(str)
End Sub
I am geting error on the line Me.Text123.SelStart = Len(str). error says "you entered an expression that has an invalid reference to the property selstart"
What is the problem?
Me.Text123.text should be Me.Text123.Value or just Me.Text123.
What's even more so, that last line of code Me.Text123.Text = str has no use whatsoever here, so preferably you should omit it.
I am unable to reproduce the problem ! However, the error seems illogical. I am a bit concerned what the ultimate motive of this code is. Try shuffling this around, see if that helps !
Private Sub Text123_KeyUp(KeyCode As Integer, Shift As Integer)
Dim str As String
str = Me.Text123
If Len(Me.Text123 & vbNullString) = 0 Then
Me.Report.Filter = ""
Me.Report.FilterOn = False
Else
Me.Report.filter = "[ItemNo] Like '*" & Me.Text123.Text & "*'"
Me.Report.FilterOn = True
End If
Me.Text123.SetFocus
Me.Text123 = str
Me.Text123.SelStart = Len(str)
End Sub
I believe the problem is because this is in a report not a form. My workaround is to make the report a subreport of a form, with the text box in the form. The following works for me:
Private Sub Text123_KeyUp(KeyCode As Integer, Shift As Integer)
If Nz(Text123.Text, vbNullString) = vbNullString Then
Me!rprtToFilter.Report.FilterOn = False
Else
Me!rprtToFilter.Report.Filter = "[ItemNo] like '*" & Text123.Text & "*'"
End If
End Sub
Text boxes seem to behave differently on reports - they don't have a change event and if you move the focus elsewhere they seem to lose their text, though I can't find documentation about this.

Access 2010 VBA Picture Object in Form

i have a form in access with one field (club_name), i have a picture object in this form. someplace in a folder i have some pictures (*.png) that each club's picture equal to records in my table.for example i have a record "FCB" and in that folder i have a picture "FCB.png". i myself code like this:
Private Sub Form_Current()
Image5.Picture = "C:\Users\Milad\Desktop\club imgs\" & Club_Name.Text & ".png"
End Sub
but it's not right.
please help?
Do not refer to the text property of controls. It is only avaiable when the control has focus. If you must use a property, use value.
Me.Image5.Picture = "C:\Users\Milad\Desktop\club imgs\" & Me.Club_Name & ".png"
You can also check that it all works by using a "real" name:
Me.Image5.Picture = "C:\Users\Milad\Desktop\club imgs\FCB.png"
Re Comment
sPath = CurrentProject.Path & "\"
sBlank = "Blank.png" ''Your own default empty picture
If IsNull(Me.Club_Name) Then
sFile = sBlank
Else
''Does the file exist? Note: Use FilesystemObject
''instead if you are working network paths.
sFile = Dir(sPath & Me.Club_Name & ".png")
''Empty string ("")
If sFile = vbNullString Then
sFile = sBlank
End If
End If
Me.Image5.Picture = sPath & sFile