UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Function Keydown And Keypress, Access 2003    
 
   
mr.siro
post May 17 2018, 01:57 AM
Post#1



Posts: 95
Joined: 27-January 18



Hello guys.
I have a form with 10 combo box. On event keydown and keypress at every combobox i write keycode = 0 and keyascii = 0
So, i want a function, when i open form, this function auto set keycode and keyacii = 0 to all combo box on form.
What should i do ?
Go to the top of the page
 
cheekybuddha
post May 17 2018, 03:57 AM
Post#2


UtterAccess VIP
Posts: 10,464
Joined: 6-December 03
From: Telegraph Hill


Do you so anything else (code-wise) in the KeyDown/KeyPress events of those comboboxes?
Go to the top of the page
 
mr.siro
post May 17 2018, 04:37 AM
Post#3



Posts: 95
Joined: 27-January 18



Private Function StripKeyCode(KeyCode As Integer) As Integer
Select Case KeyCode
Case vbKeyUp, vbKeyDown, vbKeyTab, vbKeyReturn
'Allow navigation keys to pass through
StripKeyCode = KeyCode
Case Else
StripKeyCode = 0
End Select
End Function

Private Sub combobox_KeyDown(KeyCode As Integer, Shift As Integer)
Dim Cbo As ComboBox, i As Integer, Val As Variant, CurIndex As Integer
Set Cbo = Me.ActiveControl
Select Case KeyCode
Case vbKeyBack, vbKeyDelete
KeyCode = 0
Case Else
CurIndex = Cbo.ListIndex
For i = CurIndex + 1 To Cbo.ListCount - 1
If Left(Cbo.ItemData(i), 1) = Chr(KeyCode) Then
Cbo.value = Cbo.ItemData(i)
Exit Sub
End If
Next i
For i = 0 To CurIndex
If Left(Cbo.ItemData(i), 1) = Chr(KeyCode) Then
Cbo = Cbo.ItemData(i)
Exit Sub
End If
Next i
End Select
KeyCode = StripKeyCode(KeyCode)
End Sub

Private Sub combobox_KeyPress(KeyAscii As Integer)
KeyAscii = StripKeyCode(KeyAscii)
End Sub

Private Sub combobox_KeyUp(KeyCode As Integer, Shift As Integer)
KeyCode = StripKeyCode(KeyCode)
End Sub

On event keydown and keypress and keyup of every comboxbox in form, i put that code. It's so tired and waste of time. Can you help me.
This post has been edited by mr.siro: May 17 2018, 04:39 AM
Go to the top of the page
 
cheekybuddha
post May 17 2018, 04:56 AM
Post#4


UtterAccess VIP
Posts: 10,464
Joined: 6-December 03
From: Telegraph Hill


I'm afraid I'm short of time at the moment.

Since the KeyDown, KeyPress methods take arguments for KeyCode, KeyAscii etc, you will have to to intercept these via a class.

The basic process:
1. Create a class for overriding your comboboxes.
2. In the class module setup a method to attach a combobox object, set the OnKeyDown, OnKeyPress properties to "[Event Procedure]"
3. Add your generic code for the KeyDown/KeyPress events
4. In your form's load event create a collection object
5. Loop through all the combos on the form adding to the collection an new instance of the combo class for each combo

It's not as complicated as it sounds! If others here who understand the process see this they can help you before I can.

Otherwise, I will try and give you some boilerplate code later (perhaps tonight).

Also, the code in your KeyDown example looks strange, like it's repeating itself. Is it meant to do that?

hth,

d
Go to the top of the page
 
missinglinq
post May 17 2018, 05:58 AM
Post#5



Posts: 4,553
Joined: 11-November 02



This looks like an awful lot of code for one appears to be a simple task. Am I correct is thinking that your aim, here, is to simply prevent the user from entering (typing in) anything in the Comboboxes?

Linq ;0)>
Go to the top of the page
 
mr.siro
post May 17 2018, 09:22 AM
Post#6



Posts: 95
Joined: 27-January 18



To linq, yes, do not allow user type in the comboxbox.
But with code in envent keydown. I allow to this: If combox have record: Computer,CPU, printer, fax. When user type "c" in keyboard, combox will move to "computer", if user press "c" again combox move to "CPU". User type "p", combox will move to "printer". Same to "fax".
But, to do that, i must put the code in every combox.
So, i think, have a way to create a function, when form open call this function, function will do all combox on form.

Go to the top of the page
 
missinglinq
post May 17 2018, 10:40 AM
Post#7



Posts: 4,553
Joined: 11-November 02



Are you saying that you have 10 identical Comboboxes...i.e. all with the choices of computer, CPU. printer and fax?

Linq ;0)>
Go to the top of the page
 
mr.siro
post May 17 2018, 10:57 AM
Post#8



Posts: 95
Joined: 27-January 18



No, this is a example, i want to you know what i meant.
Every combox have different record source.
Go to the top of the page
 
cheekybuddha
post May 19 2018, 04:53 AM
Post#9


UtterAccess VIP
Posts: 10,464
Joined: 6-December 03
From: Telegraph Hill


Hi,

OK, here's some code that I think should do what you need.

First, create a new Class module - name it 'clsMyCombo'. Then add the following code:
CODE
Option Compare Database
Option Explicit

Private Const EVENT_PROCEDURE As String = "[Event Procedure]"

Private WithEvents m_cbo As Access.ComboBox

Friend Property Get Cbo() As Access.ComboBox
  Set Cbo = m_cbo
End Property

Friend Property Set Cbo(Cbo As Access.ComboBox)
  Set m_cbo = Cbo
End Property

Friend Function Setup(Cbo As Access.ComboBox) As Boolean

  Set Me.Cbo = Cbo
  With Me.Cbo
    .OnKeyDown = EVENT_PROCEDURE
    .OnKeyPress = EVENT_PROCEDURE
    .OnKeyUp = EVENT_PROCEDURE
  End With
  Setup = (Err = 0)
  
End Function

Private Sub Class_Terminate()
  Set m_cbo = Nothing
End Sub

Private Sub m_cbo_KeyDown(KeyCode As Integer, Shift As Integer)

  Dim i As Integer, Val As Variant, CurIndex As Integer
  
  With Me.Cbo
    Select Case KeyCode
    Case vbKeyBack, vbKeyDelete
      KeyCode = 0
    Case Else
      CurIndex = .ListIndex
      For i = CurIndex + 1 To .ListCount - 1
        If Left(.ItemData(i), 1) = Chr(KeyCode) Then
          .Value = .ItemData(i)
          Exit Sub
        End If
      Next i
      For i = 0 To CurIndex
        If Left(.ItemData(i), 1) = Chr(KeyCode) Then
          .Value = .ItemData(i)
          Exit Sub
        End If
      Next i
    End Select
  End With
  KeyCode = StripKeyCode(KeyCode)
  
End Sub

Private Sub m_cbo_KeyPress(KeyAscii As Integer)
  KeyAscii = StripKeyCode(KeyAscii)
End Sub

Private Sub m_cbo_KeyUp(KeyCode As Integer, Shift As Integer)
  KeyCode = StripKeyCode(KeyCode)
End Sub

Private Function StripKeyCode(KeyCode As Integer) As Integer

  Select Case KeyCode
  Case vbKeyUp, vbKeyDown, vbKeyTab, vbKeyReturn
  ' Allow navigation keys to pass through
    StripKeyCode = KeyCode
  Case Else
    StripKeyCode = 0
  End Select

End Function


Second, create a new Standard module - name it something like 'basMyCombo'. Then add the following code:
CODE
Option Compare Database
Option Explicit

Function SetupCombos(frm As Form, colPass As Collection) As Boolean

  Dim ctl As Control, myCombo As clsMyCombo
  
  If colPass Is Nothing Then
    Set colPass = New Collection
  End If
  For Each ctl In frm
    If ctl.ControlType = acComboBox Then
      Set myCombo = New clsMyCombo
      Call myCombo.Setup(ctl)
      colPass.Add myCombo, ctl.Name
    End If
  Next ctl
  SetupCombos = (Err = 0)
  
End Function

Function CleanupCombos(colPass As Collection) As Boolean

  Dim i As Integer
  
  With colPass
    For i = .Count To 1 Step -1
      .Remove i
    Next i
  End With
  Set colPass = Nothing
  CleanupCombos = (colPass Is Nothing)
  
End Function


Finally, in any form where you want to apply this behaviour to the comoboxes, add the following code:
CODE
Option Compare Database
Option Explicit

Private m_colCombos As Collection    ' <-- module level collection object variable

Private Sub Form_Load()

  Call SetupCombos(Me, m_colCombos)
  
End Sub

Private Sub Form_Unload(Cancel As Integer)

  Call CleanupCombos(m_colCombos)
  
End Sub


Once you have the class module and standard module in your project you can use this in any form where you want to apply the behaviour to all its comboboxes.

Just add the module-level collection object variable in the form's declarations at the top of the module, and the SetupCombos() function call to the form's Load event, and the CleanupCombos() function call to the Unload event.

You can still add more code if necessary to the individual combobox KeyPress/KeyDown/KeyUp events in the form's module - it will run after the class module code has run.

hth,

d
Go to the top of the page
 
mr.siro
post Oct 8 2018, 04:11 AM
Post#10



Posts: 95
Joined: 27-January 18



hello cheekybuddha, your code work very great. Thanks very much.
I have question. What is CleanupCombos mean?
If not call this, what will happens.
I worry if unload_Event not call CleanupCombos. Example: Window shutdown power.
This post has been edited by mr.siro: Oct 8 2018, 04:24 AM
Go to the top of the page
 
cheekybuddha
post Oct 8 2018, 06:38 AM
Post#11


UtterAccess VIP
Posts: 10,464
Joined: 6-December 03
From: Telegraph Hill


The point of the CleanupCombos() function is to release the memory of the class object's associated controls.

If you just shutdown your pc without closing the form then it doesn't matter because all of your computer's memory is released!!!

However, it's always advisable to shut your applications down properly before shutting down your pc. If you don't shut Access down properly then there is a risk of data loss.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    22nd October 2018 - 07:01 AM