Link to home
Start Free TrialLog in
Avatar of DarrinE
DarrinE

asked on

Variables in text

Hi All

My question is probably simple to one of you - in many accounting programs they enable a "shortcut" to insert text in a field

For instance if you are typing a sentence and place  \a in the text it will complete the word as you go - the shortcuts are normal stored in a table (database)

if \a is equal to a shortcut to the text "today"

then

"I have type 85 words \a"

would be the equivalent of

"I have typed 85 words today"

How is this done ??

Code examples appreciated (and required for the points)

MTIA

DarrinE
Avatar of mcbeth
mcbeth

mystring = Replace mystring, "/a", "today", 1, , vbTextCompare
The following code provides a simple example:

Private Sub Text1_KeyPress(KeyAscii As Integer)

    Dim intLoc As Integer

    intLoc = InStr(Text1.Text, "\a")
   
    If intLoc <> 0 Then
        Text1.Text = Mid(Text1.Text, 1, (intLoc - 1)) & "today" & (Mid(Text1.Text, (intLoc + 2)))
        Text1.SelStart = Len(Text1.Text)
    End If

End Sub
mcbeth:

Cool - didn't know you could do that...
Here is an example of this, it doesn't show the / character as you type, simply inserts the corresponding text when you press the next character. To display a / character simply type it twice as it is in the collection.

Private colShortcuts As Collection

Private Sub Form_Load()
    Set colShortcuts = New Collection
    colShortcuts.Add "Today", "/a"
    colShortcuts.Add "Tomorrow", "/t"
    colShortcuts.Add "Yesterday", "/y"
    colShortcuts.Add "/","//"
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Static blnShortcut As Boolean
    If blnShortcut Then 'Determine which key is pressed
        blnShortcut = False
        Text1.SelText = colShortcuts("/" & Chr(KeyAscii))
        KeyAscii = 0
    End If
    If Chr(KeyAscii) = "/" Then
        blnShortcut = True
        KeyAscii = 0
    End If
End Sub
Avatar of DarrinE

ASKER

: TimCottee

Your code seems the best at this stage - I have increased the points to 100 - now can you show me ... using an existing recordset with two fields the first field is a  integer (shct) - ie "1" to "999" (limited to a number upto 999) - the second is the description (desc) - ie \1 converts to the description "today"

Sorry for the confusion but you have done it in a manner which I nevr really thought of - ingenious actually <s>

MTIA

DarrinE
Try this:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim lStr As String
Dim lCurPos As Integer
Dim lReplStr As String

lCurPos = Text1.SelStart
If lCurPos <> 0 Then
If InStr(Mid(Text1.Text, lCurPos, 1) & Chr(KeyAscii) & Mid(Text1.Text, lCurPos + 1), "\a") > 0 Then
    lReplStr = "Today"
   
    lStr = Left(Text1.Text, lCurPos - 1) & Mid(Text1.Text, lCurPos, 1) & Chr(KeyAscii) & Mid(Text1.Text, lCurPos + 1)
    lCurPos = InStr(lStr, "\a") + 1
    lStr = Replace(lStr, "\a", lReplStr, 1)
    Text1.Text = lStr
    Text1.SelStart = lCurPos + Len(lReplStr) - 2    'move after Repl. Str. - may need some change.
    KeyAscii = 0
End If
End If
End Sub

DarrinE:

You will of course need to expand Tim's code to handle the '/' character when it isn't being used as a shortcut - i.e. 1/2.
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rd707, it already does that, you just press the / key twice and it is displayed, the original version did it because // was an item in the collection. The modified version does it slightly differently but it still works.
Not in the original code - crashes if you enter say "1/2".
Avatar of DarrinE

ASKER

its also crashes when you press say "/z" - not in the collection -

TimCottee : How do you handle an error of that nature ? "On Error Resume Next" or ...
try this

Option Explicit
Dim isShortKey As Boolean
Dim xDic As New Dictionary

Private Sub Form_Load()

xDic.Add "a", "today "

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Static pzTest As String
If isShortKey Then
   pzTest = Chr(KeyAscii)
   If xDic.Exists(pzTest) Then
       Text1 = Replace(Text1, "\", xDic.Item(pzTest), 1, , vbTextCompare)
       KeyAscii = 0
       Text1.SelStart = Len(Text1)
       Text1.SelLength = 0
   End If
   isShortKey = False
End If
If KeyAscii = 92 Then
   isShortKey = True
   pzTest = "\"
End If
End Sub

you have to reference the MS scripting runtime to get the
dictionary object. then place your table data in the dictornary object only character after \ will be
used
if item is not found nothing happens (e.g. 1\2)
On error resume next would be fine, unfortunately the collection object unlike the dictionary object has no method for determining whether an element exists. However you do it you have to test for the element and handle the error. In this situation, an invalid shortcut should probably be ignored so on error resume next just before the "text1.seltext =" line would be effective and as good a solution as you will get. You could perhaps consider using the dictionary object instead and its .Exists property but if you are using a recordset then the second method should be better for you anyway.
Avatar of DarrinE

ASKER

Now this was fantastic <!> - Many thanks guys for the input - anyone searching on the title will get some decent answers out of this - I know I did <!>

TimeCottee: Many thanks to you ... you've been a great help

Now one final thing ... do you use ADO all the time instead of DAO ? I have a new program ahead of me and I usually write in C or C++ (no or very little MFC) - I see you do a lot in VB and maybe can point out whats a better choice for the time being ...?
DAO is easier of you're going to be using Access.

I use ADO for stuff like Oracle etc.
Avatar of DarrinE

ASKER

ADO does not work with MDB files over a network ?

DarrinE
For the last year or so I have been using ADO exclusively. Whilst rd707 is correct in saying that DAO works better with access in some respects, the flexibility of ADO for me makes it a better choice. Generally speaking I only use Access for prototyping, I then move onto SQL server for which ADO is a much better solution. If I use DAO for one and ADO for another it is two lots of technology I have to remember and also have to recode when migrating so I find it much better to put up with the slightly slower response of ADO against access and only use the one methodology. There is in fact only one thing that cannot be achieved using ADO and JRO against an access database and that is to set/change the database password in code.
Avatar of DarrinE

ASKER

YOu mension there is a speed problem - is that a *real* problem because this database will hold a lot of records (in excess of 1 million new entries per year) - its for a law office that is looking for a program to manage its documents ... and matters (no financial transactions)

I looked at ADO and RDO because perhaps in the future a webb application could deal with the satellite offices (they have one main offce and 7 smaller ones outside of a 100klm radius)

DAO does not readily seem to be adaptable to the webb stuff wereas the others seem to do ok (in MSDN anyway)

??

DarrinE