ASCII Key press problem

Posted on 2003-03-25
Medium Priority
Last Modified: 2008-02-01
I am having a problem with some code I have.  On my main search screen I have setup code to copy specific data to the clip board on certain ASCII key presses.  This works fine if the listview item has focus (ie. Blue) but if I open a record and come back to it the listview item is ghosted.  If the user does not click on the item and presses one of my defined buttons I get a runtime 91 error as it is not the "ActiveControl".

If I put a "On Error Resume Next" under the Clipboard.Clear then it stops the error but doesn't cycle throught the code correctly. eg If you press the A key it cycles throught the U code then exits.  

How can I remedy this without simply telling the user to highlight the record before using these keys.

Original code below:

Private Sub Form_KeyPress(KeyAscii As Integer)
    If Screen.ActiveControl.Name = "lvwSearch" And (KeyAscii = 117 Or KeyAscii = 85) Then
        Clipboard.SetText (Me.lvwSearch.SelectedItem.SubItems(7)) 'UMRN (U, u)
    ElseIf Screen.ActiveControl.Name = "lvwSearch" And (KeyAscii = 97 Or KeyAscii = 65) Then
        Clipboard.SetText (Me.lvwSearch.SelectedItem.SubItems(3)) 'ACC NBR (A, a)
    ElseIf Screen.ActiveControl.Name = "lvwSearch" And (KeyAscii = 80 Or KeyAscii = 112) Then
        Clipboard.SetText (Me.lvwSearch.SelectedItem.SubItems(13)) 'Patient ID (P,p)
    ElseIf Screen.ActiveControl.Name = "lvwSearch" And (KeyAscii = 88 Or KeyAscii = 120) Then

        If Me.lvwSearch.SelectedItem.SubItems(14) <> "" Then
            Clipboard.SetText "intpatdetid = " & (Me.lvwSearch.SelectedItem.SubItems(14)) 'PatDetID (X,x)
        ElseIf Me.lvwSearch.SelectedItem.SubItems(15) <> "" Then
            Clipboard.SetText "intreferralid = " & (Me.lvwSearch.SelectedItem.SubItems(15)) 'ReferralID (X,x)
            Clipboard.SetText "INTEMERGID = " & (Me.lvwSearch.SelectedItem.SubItems(16)) 'EmergID (X,x)
        End If

    End If

    If KeyAscii = vbKeyReturn Then
        SendKeys "{Tab}"
        KeyAscii = 0
    End If
End Sub

Question by:thurst
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

robertlees earned 80 total points
ID: 8207757
The listview item is ghosted because the control doesn't have the focus, but you have set the HideSelection to True.

By testing Screen.ActiveControl.Name you are assuming it will have the focus.

Firstly why not try lvwSearch.SetFocus during Form_Activate - ie, when the form gets activated again after returning from another form, you give focus back to lvwSearch.

Alternatively, can you just test that a selection has been made, without requiring that it have the focus. That is, replace all your If Screen.ActiveControl.Name = "lvwSearch" tests with If Not (lvwSearch.SelectedItem Is Nothing)


Expert Comment

ID: 8531595
Hi thurst,
This old question (QID 20563203) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Expert Comment

ID: 9441032
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Accept robertlees's comment as Answer

Please leave any comments here within the next seven days.


Cleanup Volunteer

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

765 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