Solved

Variables in text

Posted on 2001-08-03
18
192 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:DarrinE
  • 5
  • 5
  • 5
  • +2
18 Comments
 
LVL 1

Expert Comment

by:mcbeth
ID: 6348259
mystring = Replace mystring, "/a", "today", 1, , vbTextCompare
0
 
LVL 4

Expert Comment

by:rd707
ID: 6348273
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
 
LVL 4

Expert Comment

by:rd707
ID: 6348284
mcbeth:

Cool - didn't know you could do that...
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6348297
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
 
LVL 2

Author Comment

by:DarrinE
ID: 6348341
: 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
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6348371
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
 
LVL 4

Expert Comment

by:rd707
ID: 6348374
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6348403
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 6348407
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:rd707
ID: 6348415
Not in the original code - crashes if you enter say "1/2".
0
 
LVL 2

Author Comment

by:DarrinE
ID: 6348451
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
 
LVL 1

Expert Comment

by:mcbeth
ID: 6348453
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 6348506
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
 
LVL 2

Author Comment

by:DarrinE
ID: 6348518
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
 
LVL 4

Expert Comment

by:rd707
ID: 6348524
DAO is easier of you're going to be using Access.

I use ADO for stuff like Oracle etc.
0
 
LVL 2

Author Comment

by:DarrinE
ID: 6348537
ADO does not work with MDB files over a network ?

DarrinE
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6348608
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
 
LVL 2

Author Comment

by:DarrinE
ID: 6348635
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now