Surrounding text in text box via command button

J

John Wallace

I'm trying to make a simple newsposting form so that I can easily post
from within Access rather than code a login page just for this. I don't
need anything extravagant, just the ability to post links, email
addresses, embolden, italics, ect... Basically, I'd like to emulate a
form from an internet forum.

Here's what I've done.
- I've created a newspost userform where I have a subject text box
(text), and a news text box (memo) named news_msg.
- I've made a command button with a bold icon named cmdbtnBold.
- I've made a command button with an italic icon named cmdbtnItalic
- I've created 2 global variables named iSelStart, and iSelLen
- I've set iSelStart and iSelLen to news_msg.SelStart and
news_msg.SelLength, respectively, on the onDirty, onUpdate, onExit, and
onLostFocus events.

Well, I've got it coded to the point it does surround selected text but
it is VERY unlreliable. Every time I first hit a button with no text, it
does nothing, then after hitting it again, it works. It almost never
works the first time.

The value of news_msg.SelStart becomes reset somewhere along the way.

Here's the code:
'--------------------START-----------------------------
Option Compare Database
Option Explicit

Dim iSelStart As Integer
Dim iSelLen As Integer


Private Sub cmdbtnBold_Click()
On Error GoTo Err_cmdbtnBold_Click

Const ERR_OBJNOTEXIST = 2467
Const ERR_OBJNOTSET = 91
Const ERR_CANTMOVE = 2483

Call ReplaceText("", "")

Exit_cmdbtnBold_Click:
Exit Sub

Err_cmdbtnBold_Click:
If (Err = ERR_OBJNOTEXIST) Or (Err = ERR_OBJNOTSET) Or (Err =
ERR_CANTMOVE) Then
Resume Next
End If
MsgBox Err.Description
Resume Exit_cmdbtnBold_Click

Debug.Print "Bold: " + vbTab + "iSelStart = " + CStr(iSelStart) +
vbTab + "iSelLen = " + CStr(iSelLen)
End Sub

Private Sub cmdbtnItalic_Click()
On Error GoTo Err_cmdbtnItalic

Const ERR_OBJNOTEXIST = 2467
Const ERR_OBJNOTSET = 91
Const ERR_CANTMOVE = 2483

Call ReplaceText("", "")

Exit_cmdbtnItalic_Click:
Exit Sub

Err_cmdbtnItalic:
If (Err = ERR_OBJNOTEXIST) Or (Err = ERR_OBJNOTSET) Or (Err =
ERR_CANTMOVE) Then
Resume Next
End If
MsgBox Err.Description
Resume Exit_cmdbtnItalic_Click
Debug.Print "Italic: " + vbTab + "iSelStart = " + CStr(iSelStart) +
vbTab + "iSelLen = " + CStr(iSelLen)
End Sub

Private Sub Form_Load()
iSelStart = 0
iSelLen = 0
End Sub

Private Sub news_msg_Change()
With news_msg
iSelStart = .SelStart
iSelLen = .SelLength
End With
End Sub

Private Sub news_msg_Dirty(Cancel As Integer)
With news_msg
iSelStart = .SelStart
iSelLen = .SelLength
End With
Debug.Print "OnDirty: " + vbTab + "iSelStart = " + CStr(iSelStart)
+ vbTab + "iSelLen = " + CStr(iSelLen)
End Sub

Private Sub news_msg_Exit(Cancel As Integer)
With news_msg
iSelStart = .SelStart
iSelLen = .SelLength
End With
Debug.Print "Exit: " + vbTab + "iSelStart = " + CStr(iSelStart) +
vbTab + "iSelLen = " + CStr(iSelLen)
End Sub

Private Sub news_msg_LostFocus()
With news_msg
iSelStart = .SelStart
iSelLen = .SelLength
End With
Debug.Print "LostFocus: " + vbTab + "iSelStart = " +
CStr(iSelStart) + vbTab + "iSelLen = " + CStr(iSelLen)
End Sub

Private Sub ReplaceText(strBefore As String, strAfter As String)
With news_msg
.SetFocus
If (Len(.Value) = 0 Or (iSelLen - Len(.Value)) = 0) Then
.Value = .Value + strBefore + strAfter
.SelStart = Len(strBefore) + iSelLen
.SelLength = 0
ElseIf (iSelLen = 0) Then
.Value = Left(.Value, iSelStart) + strBefore + strAfter +
Mid(.Value, iSelStart + 1)
.SelStart = iSelStart + Len(strBefore)
.SelLength = 0
Else
.Value = Left(.Value, iSelStart) + strBefore + Mid(.Value,
iSelStart + 1, iSelLen) + _
strAfter + Mid(.Value, iSelStart + iSelLen + 1)
.SelStart = iSelStart
.SelLength = iSelLen + Len(strBefore) + Len(strAfter)
End If
' Set global vars
iSelLen = .SelLength
iSelStart = .SelStart
End With
End Sub
'---------------END----------------------------------

Any ideas?

-John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top