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
LVL 2
DarrinEAsked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Darrin, here is an example using a recordset, in this case I am only using a disconnected ado recordset but you could use anything. Again the / character is not displayed unless pressed twice, pressing any non-numeric key is treated as the end of the shortcut sequence and the shortcut is inserted followed by the non-numeric keypress.

Private rstShortcuts As ADODB.Recordset

Private Sub Form_Load()
    Set rstShortcuts = New ADODB.Recordset
    With rstShortcuts.Fields
        .Append "ShortcutID", adInteger
        .Append "Description", adBSTR
    End With
    With rstShortcuts
        .Open
        For intItem = 1 To 999
            .AddNew
            .Fields("ShortcutID").Value = intItem
            .Fields("Description").Value = "Shortcut Item " & CStr(intItem)
            .Update
        Next
    End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Static blnShortcut As Boolean
    Static strShortcut As String
    If blnShortcut Then 'Determine which key is pressed
        If Chr(KeyAscii) Like "[0-9]" Then
            strShortcut = strShortcut & Chr(KeyAscii)
            KeyAscii = 0
        ElseIf Chr(KeyAscii) = "/" Then
            blnShortcut = False
        ElseIf Len(strShortcut) > 0 Then
            blnShortcut = False
            With rstShortcuts
                .MoveFirst
                .Find "ShortcutID=" & strShortcut
                If Not .EOF Then
                    Text1.SelText = .Fields("Description").Value
                Else
                    Text1.SelText = "Invalid Shortcut ID"
                End If
            End With
            strShortcut = ""
        End If
    ElseIf Chr(KeyAscii) = "/" Then
        blnShortcut = True
        KeyAscii = 0
    End If
End Sub
0
 
mcbethCommented:
mystring = Replace mystring, "/a", "today", 1, , vbTextCompare
0
 
rd707Commented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rd707Commented:
mcbeth:

Cool - didn't know you could do that...
0
 
TimCotteeHead of Software ServicesCommented:
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
0
 
DarrinEAuthor Commented:
: 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
0
 
Valliappan ANSenior Tech ConsultantCommented:
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

0
 
rd707Commented:
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.
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
 
rd707Commented:
Not in the original code - crashes if you enter say "1/2".
0
 
DarrinEAuthor Commented:
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 ...
0
 
mcbethCommented:
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)
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
 
DarrinEAuthor Commented:
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 ...?
0
 
rd707Commented:
DAO is easier of you're going to be using Access.

I use ADO for stuff like Oracle etc.
0
 
DarrinEAuthor Commented:
ADO does not work with MDB files over a network ?

DarrinE
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
 
DarrinEAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.