MS Access VBA: get selected portion of text from text box? - ms-access

I'm having trouble find a way to achieve this. Bascially, I just need a way to get the selected portion of text within a text box. The idea is that a user can double click a word within a larger string to automatically search against another set of data.
If can get the below to work, the selected text will simply call a function that runs my search process. The below does not work and many of my online finds only capture the whole text box. Any ideas?
Private Sub txtproductName_DblClick(Cancel As Integer)
Debug.Print txtproductName.SelText
End Sub

I've used MouseUp instead
Private Sub txtproductName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Len(txtproductName.SelText) > 0 Then
'do my thing
Debug.Print txtproductName.SelText
Else
'do nothing
End If
End Sub

You can use the LostFocus event.
Private Sub txtproductName_LostFocus()
MsgBox Me.txtproductName.SelText
End Sub

Related

How to refer to a checkbox in access navigation form through vba (Detail_Format) in report

I am trying to hide details section in a report if checkbox in navigation form is not checked.
It's not working somehow, please tell me what am I doing wrong.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Forms!DASHBOARD!NavigationSubform.Form!rptMain.chkWaBox.Value = True Then
Detail.Visible = True
End If
End Sub
Thanks for the help in advance.
Is your details section set to visible in design view? If it is, your code will not hide it. Your code only makes it visible if it's hidden.
When using boolean values like this, I like to make the assignment directly, so it doesn't matter what the state of the object is in design, it always gets handled correctly at runtime:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Detail.Visible = Forms!DASHBOARD!NavigationSubform.Form!rptMain.chkWaBox.Value
End Sub

Access-VBA Changing Font and Size of text in form

I am trying to change the font and font size of the text in one box of my form
With Me.[Notes:]
.SetFocus
.FontName = "Verdana"
.FontSize = 8
.ForeColor = vbBlack
End With
I have already looked here
Access VBA programmatically setting font/size not working
And seem to have that working, though I still have a couple of issues.
It changes the text in the right box but on EVERY one of my records, rather than just the one I'm editing.
It only works on text which has been types into the text box directly. Not on text that has been pasted into the form. (This is the reason for the button in the first place)
In case it matters, I'm using MS Access 2016
Thanks in advance,
Daniel
If it's not a continuous form, sounds like you need to add an IF statement to specify under which conditions this should take place. Then place it on the current event of the form.
Private Sub Form_Current()
if condition met then
With Me.[Notes:]
.SetFocus
.FontName = "Verdana"
.FontSize = 8
.ForeColor = vbBlack
End With
end if
end sub
For the second issue you can set the code (or create a procedure and call it) on the afterupdate event of the field
Private Sub notes_AfterUpdate()
if condition met then
With Me.[Notes:]
.SetFocus
.FontName = "Verdana"
.FontSize = 8
.ForeColor = vbBlack
End With
end if
End Sub

Change caption of button to field in a table using Access

I am trying to make the caption of a button the content of a field from a table.
Everything I am finding online in forums and MS websites only shows using a macro with the OnClick. I need it to display without every clicking. It also needs to dynamically change every time the field from the table changes.
How can I achieve this? Again, I don't want an event such as OnClick, I want it to simply read as the field from the table reads.
You could simply use the Form's OnCurrent event to set the buttons caption.
On Current:
Private Sub Form_Current()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub
This will make it so ever time the selected record changes the button will update. Also just add the code into the specific field's AfterUpdate event so it changes then as well.
After Update:
Private Sub Textbox1_AfterUpdate()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub

How to make Access select the entire text in a box instead of all records when using ctrl+a?

In Access, is there a way to set what is selected when a user hits ctrl+a? I have a text box for users to input data, but if they hit ctrl+a, it selects all of the records instead of the entire text of a field. Is there a way to get around this? I am worried about someone accidentally deleting a bunch of records instead of the text of a field.
I'm a beginner user, so please forgive me if I am asking a silly question. I tried to search the forum, but haven't found a good answer.
Working with a textbox and the Keydown event, you can say:
Private Sub Content_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print KeyCode, Shift
If KeyCode = vbKeyA And Shift = acCtrlMask Then
MsgBox "Please use F2"
KeyCode = 0
Shift = 0
End If
End Sub
The textbox is called Content in this case.
You may like to get more complicated and look into KeyPreview.

Custom Record Navigation

Hi I have an App with nested sub-forms, the record navigation is confusing and I've started moving the Record Navigation controls to the top of the forms to improve that. I'm stuck on the 'n of n' field, is there a generic property(s)/query that will give me this?
For the first n, use the AbsolutePosition property of the form's RecordsetClone. see MSDN
For the second n, use the RecordCount property.
Update: I prefer Mike's approach using CurrentRecord. I created a form with 2 text boxes: txtCurrentRecord and txtRecordCount. Then this procedure for the form's On Current event causes the text boxes to display the same values as the built-in navigation bar.
Private Sub Form_Current()
Me.txtCurrentRecord = Me.CurrentRecord
If Not Me.NewRecord Then
Me.txtRecordCount = Me.RecordsetClone.RecordCount
Else
Me.txtRecordCount = Me.CurrentRecord
End If
End Sub
Update2: I added a procedure to Form Load to make sure RecordCount is accurate.
Private Sub Form_Load()
Me.RecordsetClone.MoveLast
Me.RecordsetClone.MoveFirst
End Sub