Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
KeyCode arrays
Lars Schindler 
     
2 years ago
Hello everyone,

I am currently trying to rule out KeyCodes.
Expressions like 'If Not (KeyCode >= 48 And KeyCode <= 57)' work quite well.
But instead of 'If Not KeyCode = 8 And Not KeyCode = 27 And Not Keycode = 46' I wanted to try something like 'If Not KeyCode In (8, 27, 46)', which unfortunately doesn't seem to work.
The next idea was something with arrays, which looked like this in ChatGPT:

DetailsFunction IsInArray(valToBeFound As Integer, arr As Variant) As Boolean
    Dim i As Integer
    IsInArray = False
    For i = LBound(arr) To UBound(arr)
        If arr(i) = valToBeFound Then
            IsInArray = True
            Exit Function
        End If
    Next i
End Function

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    ' List of KeyCodes that are to be ignored
    Dim ignoreKeys As Variant
    ignoreKeys = Array(8, 27, 46) ' Insert the desired keycodes here

    If Not IsInArray(KeyCode, ignoreKeys) Then
        ' Your action if the KeyCode is not in the list
        MsgBox 'KeyCode: ' & KeyCode & ' was pressed.'
    End If
End Sub


And that in turn brings me to my question:
Can't this be done a bit shorter and more succinctly, perhaps without an additional function?
Kevin Yip  @Reply  
     
2 years ago
An array could be useful if you have lots of values to be checked.  If you have only a few values, you can use Select Case to check them.
Lars Schindler OP  @Reply  
     
2 years ago
All the time I was searching for a suitable operator, I had the feeling that I must have overlooked something that didn't require an array.
'Case 8, 27, 46' actually seems more intuitive to me.
ChatGPT has also recognized it. ;-)
Thomas Gonder  @Reply  
      
2 years ago
In this example a simple Select case will work fine. Good job pointing that out Kevin Y.
There are cases where variable string arrays come in VERY handy. They work similar to the Split()().
I wrote these in assembly many years ago for another database system. It took me a couple of weeks to write the routines in VBA, and it was quite painful to do it again. They are part of the ADS template that I give an evaluation of.
This is how it might work in more elaborate situations:
MyString = "a]b]c"
saPos = fSaLocate(MyString, "B") ' saPos = 2
saFind = fSaExtract("x]y]z", saPos) ' saFind = "y"
MyNewString = fSaReplace(MyString, saFind, saPos) ' MyNewString = "a]y]c" (MyString remains "a]b]c")
MyNewString = fSaDelete(MyNewString, 1) ' MyNewString = "y]c"
HowMany = fSaCount(MyString) ' HowMany = 3
MyString = fSaInsert(MyString, "wow", HowMany, "]") ' MyString = "a]b]wow]c"

The optional argument for the delimiter is "]", but it can be changed to any character. (I reserve "]  /  \" for my delimiters)
These functions aren't usually used for user data, I use it mostly inside coding for control.
The interesting thing is, Access is a variable length string model in its "object oriented" db approach (as compared to fixed length field dbs). So, something similar to these routines must exist under the hood in Access. It's too bad they didn't make them available as VBA functions. So Lars, you didn't overlook anything, Microsoft did (IMHO).

Thomas Gonder  @Reply  
      
2 years ago
To wrap it up, For Lars example, in the ADS, I would write: If fSaLocate("8]27]46", Keycode) = 0 then....do good stuff.
Lars Schindler OP  @Reply  
     
2 years ago
Hello Thomas,

thanks for your input and please excuse the late response.
It will probably take me a while to understand the code in all its depth, but I think I at least have an initial idea.
At the same time, I'm already busy with other codes - as it happens. :-)

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 9:23:34 PM. PLT: 1s