Detecting selected text

kirkm

Registered User.
Local time
Tomorrow, 03:23
Joined
Oct 30, 2008
Messages
1,257
If I have a Form in datasheet view, can a click in one field detect tha t part of another field that's beenhighlit ? (e.g. swiped over with the mouse to have a black background)
 
How ?
I found some examples but it seems to look for/return a known string.

I don't know in advance what has been selected, that's what Ii want to find out.
 
You can dump whatever is in the current record, (highlighted row in the sheet).
 
The current record has, say, 5 fields. Ideally you would identify which field first, but I can narrow that down to a specific field, and I can get whatever text it contains. But how do I detect (in code, I know I can 'see' it) which part of the text is selected?
 
I did a quick test in VBA immediate window. I selected some text in textbox and ran code. The value returned was the string of characters selected. The syntax is:

?Forms!formname.textboxname.SelText

However, don't really see a practical application for this. What exactly are you trying to accomplish?
 
That's good it is possible but I can't make it work here. The problem may be the Form Name. Where is that ? I have tried the name as it appears in the Database Window (under Forms) and the name from Alt-F11 Microsoft Office Access Class Objects (which starts Form_". Both give Run-time error '2450': Microsoft Office Access can't find the form....

It's use is to search for a match in another db. This starts from the d-click event in Field 7 with the criteria text in Field3.
If nothing is found, it can because of slight textual differences. This would allow me to refine the search by setting the criteria to just the selected part of the text.
By the way, I also get no drop down lost offering .Seltext. Do you ? (I'm in Access 2003)
 
You should post code for the procedure you are attempting. Is the control in a subform? Where is the code placed?

Why is user selecting text and not just typing characters into an unbound textbox?

What is 'drop down lost offering'?
 
The d-click code, do you mean? That's so different to what I'm asking and might only complicate things. It's just a Msgbox at the moment.
There's no answer to your second question, this is what it is.
Typo I meant "drop down list", intellisense, is it called? It offers just ".value".

The best I could do was "Run-time error '438':
Object doesn't support this property or method".


Is it possible this isn't supported by Access 2003 ?
 
What is 'd-click' code? And how is MsgBox involved? Code is required to execute MsgBox. If you don't provide code and/or answer questions, can't help.

Yes, it shows in intellisense dropdown. Yes, 2003 has this property.
 
All I have is this code for testing

Code:
Private Sub Command227_Click()
    MsgBox WavK.Form_frmTracks!xTitle
    MsgBox WavK.Form_frmTracks!xTitle.SelText
End Sub

The first msgbox is to confirm I have the right syntax for the control. This works but the next one brings up the error.
I don't see the dropdown, perhaps I need a special Reference or something? I'll try this is a new db.
 
Like June says, I'm not convinced by this approach either.
However, try using the mouse up event to display a message box or textbox showing highlighted text

See attached very simple example
NOTE - I can't highlight & take screenshot at the same time

attachment.php


FWIW:
1. I also got error 438 when setting another textbox control source '=Me.ControlName.SelText'
2. I can't see how it can be texted in the VBE immediate window as that will take the focus away from the control so nothing will be selected
 

Attachments

Use dot (.) instead of bang (!) to get intellisense tips in VBA.

However, the code should work regardless. Does for me.

What is WavK?

Yes Colin, I was surprised it worked in immediate window but it did. I selected a few characters in the textbox then ran code and it retrieved just the few highlighted characters. The text remains selected as long as you don't click or tab or enter to another control or record in Access window.

Oh wait, it only works with Forms. or Forms! syntax, not Form_
But I don't get error message. Forms. returns the selected text, Form_ just returns the full value.
 
Last edited:
I made a new db with one table and one Form. Added this double click event
Code:
Private Sub tField_DblClick(Cancel As Integer)
Debug.Print Forms!tblMain4.Title.SelText
End Sub
Now I get a different error
Run-time error '2185':
You can't reference a property or method for a control unless the
control has the focus.

I tried setting focus to Title, no error then but the debug.print instruction was ignored.At this stage I'd give up except you folk say it works!!
Colin. could you upload a .mdb version please?

June, I tried a dot and various combinations but no success. "WavK" is tricky, I didn't do that. It's used in addressing Forms and may be a pseudo name for the db, or part of it. I can't get it's definition either. But even avoiding that doesn't help.
 
Okay, did a test with code behind a button. This does fail. Do the immediate window code test and you will see different behavior.

Textbox MouseUp event works.
 
That was very helpful Colin! So you capture the selected text pretty much as it's selected. Not later from a different controls event, as I was attempting. I can make that work.
Thanks very much and June also.

I do see SelText in the dropdown in Colins code, but he's using "Me".

debug.print Forms!Form2.txtAmount. still shows nothing here.
 
Form_ syntax does provoke the intellisense dropdown

also

Forms("Form2").

So a variety of ways to reference form (or report) object.

Forms!

Forms.

Form_

Forms() - with dot or ! but ! won't provoke intellisense

Me.

Me!

Me and Forms() are strictly VBA, not queries nor ControlSource.

The bang (!) in queries and ControlSource will trigger tips.
 
Last edited:
I'm sure you're right but for some reason I'm not seeing it here ! In Colins code what does

debug.print Forms!Form2.txtAmount. show to you ?
 
As already noted, that syntax will not provoke intellisense in VBA.

Use Form_ or Forms(). or Me.

Forms. will present different drop list.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom