Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to get and set the position of the cursor in a textbox.

Posted on 2011-03-25
18
Medium Priority
?
260 Views
Last Modified: 2013-11-28
I have a textbox control that is 40 characters wide and 50 lines high.

How can I use code to tell which line the cursor is currently on?
How can I find the number of the next line that contains a given string?
How can I use code to move the cursor to the start of another line?
0
Comment
Question by:Milewskp
  • 9
  • 4
  • 2
  • +3
18 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35219134
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35219216
LineOn = Int((Forms!Form1.Text1.Properties("SelStart").Value)+1)/40

Remember,  SelStart is zero based.  There are three properties of interest, SelStart, SelText, and SelLength.

Instr(Forms!Form1!Text1.Value, "Search for me") will give you the start of where "Search for me" is located in the string.  As that start is zero based, add one and divide by 40 and take the integer value:
Int((Instr(Forms!Form1!Text1.Value, "Search for me") + 1)/40)

With this start, using SelStart you should be able t answer Q3.
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35220991
Unless your text box is using a non-proportionally spaced font (such as Courier), I suspect that the number of characters per line is variable.

If this is the case then you'll have to parse the text box's .Text or .Value property, counting new line characters.
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 17

Accepted Solution

by:
JezWalters earned 2000 total points
ID: 35221511
The attached sample database should do everything you're after:
Q26912674.mdb
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221533
If you type some multi-line text into the Text box and then click anywhere within the Text (or use the cursor keys, or just type), you'll see the Current Line value gets updated accordingly.

The Next line containing 'Hello' value is also set - but bear in mind that I've currently made this case-sensitive, though it's very easy to change this.

Finally, if you change the Current line value and then click the Goto Current Line button, the cursor will be positioned at the beginning of the specified line within the Text box.

Any questions, let me know!  :-)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221537
It might be worth setting the Access "Behaviour entering field" Option to "Go to start of field" too.  You can do this in code - let me know if you'd like to know how!  :-)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221714
Oh yeah, and I forgot to mention that (unless I'm missing something) GRayL's idea will only work if every line has exactly 40 characters.  No offense GRayL!  :-)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35222763
JezWalters:  None taken.  Having stated the width was 40 characters, I assumed the asker was using a fixed width font.  
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35224392
Just for the benefit of Milewskp, whether you're using a non-proportionally spaced font or not, you'll still have some lines that are less than 40 character - which means you have to parse the text to work out line numbers.

Hope that makes sense!  :-)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35224494
my benefit too, thanks jez.
0
 
LVL 75
ID: 35224512
Guys ... may I offer a suggestion here.  Wait for Milewskp to respond before making any further posts :-)

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35228856
;-)
0
 
LVL 1

Author Comment

by:Milewskp
ID: 35376306
Hi Jez,
Excellent work, Thanks!

<It might be worth setting the Access "Behaviour entering field" Option to "Go to start of field" too.  You can do this in code - let me know if you'd like to know how!  >
Yes, I'd like to know.

(Sorry I didn't response earlier.)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35376764
Is it time to accept an answer/close the question then - or is there something else I can help with?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35380099
I->I/We?
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35382066
The "Behaviour entering field" options are the second set of Advanced Access Options:

1. Click the Office Button (top left)
2. Click the "Access Options" button (bottom right)
3. Click "Advanced" in the category list on the left hand side
4. Find "Behaviour entering field" in the "Editing" section and click "Go to start of field"
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35382906
You can set the option in code like this:
SetOption "Behavior Entering Field", 1

Open in new window

0
 
LVL 1

Author Comment

by:Milewskp
ID: 35487555
Hi Jez,
Thanks again for your help. Full points (sorry for the delay)!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question