Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox - need to remember Cursor Position on return

Status
Not open for further replies.

ShaneBrennan

Programmer
May 19, 1999
198
GB
Evening Everyone.

I've written an email screen (in Access XP), the user can write any text in the body of the email and can use special "Merge Codes" that will be swapped with a record details before sending. E.g. <ClientName>, <ProjectNumber>, etc.

All this works fine, but the user has to type in the codes which can be a pain. What I'm looking for is a way to speed up this entry of codes. I have 2 ideas but haven't got a clue where to start.

Idea 1 (probably the easiest)

Have a list box next to the text are. And get the user to double on the code they want to use, the return the cursor back to the text box after the code.

To do this I assume I will need to know the Current cursor position within the text box when I leave it, the length of the code and where the new cursor position needs to go and the commands used to put it there.

Idea 2 Ideal Solution

Use a shortcut menu to insert the codes at the current cursor position.

Has anyone got an example of one of the above or an guidance to point me in the right direction.

Thanks in advance for any help given

Shane Brennan


 
Can you do an onexit of the textbox and grab the len for the cursor position? Just grabbing at random thoughts here. Not really sure I have ever seen a request like this one!

Is the place where the code needs to be insert always the same place of the email content?

my thought is you could split that text box. Have a part1 text box. Then a combo to choose the code, then a part2 textbox for the rest of the text. You could make it visually look like the parts of the user's stuff to fill out so it flows.



misscrf

It is never too late to become what you could have been ~ George Eliot
 
here is a demo of your preferred solution. Showing how to use a shortcutbar to allow you to insert codes into a text string at the current cursor location.



Here is the code you need
It shows how to read a table of codes and create a short cut bar of values. Then shows how to run a procedure based on selection from the short cut bar.

mdlCodeBar
Code:
Public Sub createCodeCommandBar()
  Const conBarName = "cbCodes"
  
  Dim rsCod As DAO.Recordset
  Dim strSql As String
  Dim CodCaption As String
  Dim CodValue As Long
  Dim cbCod As Office.CommandBar
  Dim cbCodCtrl As Office.CommandBarControl
  Dim ctl As CommandBarControl
  Dim cb As CommandBar
  
  
  Set rsCod = CurrentDb.OpenRecordset("tblCodes", dbReadOnly)
  If isCommandBar(conBarName) Then
    Application.CommandBars(conBarName).Delete
  End If
  Set cbCod = CommandBars.Add(conBarName, msoBarPopup, False, True)
  
  Do While Not rsCod.EOF
    CodCaption = rsCod!CodeText
    CodValue = rsCod!CodeID
    Set cbCodCtrl = cbCod.Controls.Add(msoControlButton, , CodValue)
    cbCodCtrl.Tag = CodValue
    cbCodCtrl.caption = CodCaption
    cbCodCtrl.OnAction = "SelectCode"
    rsCod.MoveNext
  Loop
End Sub
Public Sub selectCode()
  Dim strCode As String
  strCode = CommandBars.ActionControl.caption
  Call insertCode(strCode)
End Sub

Public Function isCommandBar(strBarName As String) As Boolean
  Dim cb As CommandBar
  For Each cb In Application.CommandBars
    If cb.Name = strBarName Then
      isCommandBar = True
    End If
  Next cb
End Function

Public Sub insertCode(strCode As String)
  Dim currentText As String
  Dim leftText As String
  Dim rightText As String
  Dim cursorLocation As Integer
  Dim txtBx As Access.TextBox
  Set txtBx = Forms("frmCodes").memoCodeFld
  currentText = Nz(txtBx.Text, "")
  cursorLocation = txtBx.SelStart
  leftText = Trim(Mid(currentText, 1, cursorLocation))
  rightText = Trim(Mid(currentText, cursorLocation + 1))
  txtBx.Value = leftText & " " & strCode & " " & rightText
End Sub

Here is the code on the form

Code:
Private Sub Form_Load()
  Call createCodeCommandBar
  If isCommandBar("cbCodes") Then
    Me.memoCodeFld.ShortcutMenuBar = "cbCodes"
  Else
    MsgBox "Command Bar 'cbCodes' does not exist.  See administrator"
  End If
End Sub


The other forms are for Demo purposes to show that you can also make the Command bar multilevel like a cascading list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top