Solved Simulate {Enter} Key being pressed through vba (1 Viewer)

raziel3

Registered User.
Local time
Yesterday, 21:34
Joined
Oct 5, 2017
Messages
275
First, let me state I'm working with a barcode scanner.

On my form (SALES) I have a Combobox (cboSalesUPC). As soon as the barcode is scanned, cboSalesUPC validates it and then it is sent to the field sfSalesUPC in the subfrom (subfrmSales).

If the barcode is not found, it triggers an event to open up a form (ITEMPOP) in popup mode. The user finds the Item by name and then clicks a button to run this sub

Code:
Forms!Sales.Form.cboSalesUPC = Me.wsPNAME.Column(0)
DoCmd.Close acForm, "ITEMPOP"
Forms!Sales.Form.cboSalesUPC.SetFocus

The barcode scanner after scanning, simulates the {Enter} key being pressed.

How do I simulate this action in VBA because after the value is passed from ITEMPOP to cboSalesUPC, it just remains in cboSalesUPC and is not being passed on to sfSalesUPC .

Other info:
The barcode exists in the source table but cannot be picked up by the scanner for example:
798713110016 <-- the scanner will read this (BEER)
798713110016-CASE <---- the scanner cannot read that is why the ITEMPOP form is needed to search by name (BEER CASE)
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:34
Joined
May 7, 2009
Messages
19,231
you add code to your "previous" code to add it to the subform:

Forms!Sales.Form.cboSalesUPC = Me.wsPNAME.Column(0)
Forms!Sales!sfSalesUPC.Form!theControlName = Me.wsPNAME.Column(0)
DoCmd.Close acForm, "ITEMPOP"
Forms!Sales.Form.cboSalesUPC.SetFocus
 

raziel3

Registered User.
Local time
Yesterday, 21:34
Joined
Oct 5, 2017
Messages
275
you add code to your "previous" code to add it to the subform:

Forms!Sales.Form.cboSalesUPC = Me.wsPNAME.Column(0)
Forms!Sales!sfSalesUPC.Form!theControlName = Me.wsPNAME.Column(0)
DoCmd.Close acForm, "ITEMPOP"
Forms!Sales.Form.cboSalesUPC.SetFocus
I didn't want to pass it directly into the subfrom. I wanted it to go to the cboSalesUPC first then to the subfrom control.

The reason is, I have some code in the After Update event of cboSalesUPC that I need to run before sending it to the subform control.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:34
Joined
May 21, 2018
Messages
8,525
The afterupdate event of cboSalesUPC will not fire if the value is set by code directly. Control events only occurs through GUI interface. You would need to call the after update event then

Code:
Dim SubFrm as access.form
set SubFrm = Forms!Sales.Form
SubFrm.cboSalesUPC = Me.wsPNAME.Column(0)
DoCmd.Close acForm, "ITEMPOP"
SubFrm.cboSalesUPC.SetFocus
'need to make the after update event public
SubFrm.cboSalesUPC_AfterUpdate()
'Not sure what you mean by sending it to the subform, but in order for changes to show you may need
Forms!sales.dirty = false
 

raziel3

Registered User.
Local time
Yesterday, 21:34
Joined
Oct 5, 2017
Messages
275
'Not sure what you mean by sending it to the subform, but in order for changes to show you may need
After the barcode is scanned into cboSalesUPC, it is passed to a Combobox named sfSalesUPC in the subform. The subform is a continuous form named subfrmSales

Syntax Error at SubFrm.cboSalesUPC_AfterUpdate()

Error 438 Object does not support this property or method if I change it to SubFrm.cboSalesUPC.AfterUpdate
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:34
Joined
May 21, 2018
Messages
8,525
Sorry remove the ().
SubFrm.cboSalesUPC_AfterUpdate

See a working demo.

Often people do not like to make the event procedures public just because it is a little confusing. For clarity people will take the event procedure code and move it to a seperate public procedure. Then call the public procedure. So instead of this
Code:
Public Sub cboUPC_AfterUpdate()
   MsgBox "This is code in the after update of cboUPC.  It must be Public to be called from another form."
End Sub

You can do this.
Code:
Private Sub cboUPC_AfterUpdate()
  SomeMethod
End Sub

Public Sub SomeMethod
  'THIS MUST BE PUBLIC
   MsgBox "Code is hear for after update."
end sub

There is no difference in how this works. The main reason is my "SomeMethod" if for readability. The procedure could have a more descriptive name and when called from externally it means more than
CboUPC_AfterUpdate().
It could be something like
ValidateAndAssignUPC
 

Attachments

  • DemoUPC.accdb
    412 KB · Views: 366

raziel3

Registered User.
Local time
Yesterday, 21:34
Joined
Oct 5, 2017
Messages
275
It works. Thanks @MajP. Never even considered making a form event public. This opens up a whole new way of doing things.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:34
Joined
May 21, 2018
Messages
8,525
Sorry you are not making the "event" public you are making an event procedure ( or also called an event handler) public. It is semantics, but words have meaning. See my very detailed discussion on Events because you may get some other ideas..
 

Users who are viewing this thread

Top Bottom