need function to notice cmdbutton Name

  • Thread starter perryclisbee via AccessMonster.com
  • Start date
P

perryclisbee via AccessMonster.com

Hi,

Below is a sample of code from one of my command buttons. Each button
currently has this code. I want to simplify the process by making a function
that the cmdbuttons can call. However, the one section that is hardcoded per
button is:

Me!txtGrid.Value = "A2"

I have about 50 cmdbuttons on this form, all with the same code. I need the
txtGrid value to be equal to the gridname of the cmdbutton. Is there some
code that will recognize the name of the cmdbutton clicked? That way I could
just make one function that each button will call to when clicked on. I need
it this way because by clicking on the button, I pull up a data entry screen
with button specific (IE: A2) information on it...

Thanks,

Perry



Private Sub cmdA2_Click()

Dim stDocName As String
Dim stLinkCriteria As String


txtProductSold.Value = "0"
txtProductExpired.Value = "0"
Me!txtGrid.Value = "A2"
Me!txtProduct = DLookup("product", "qryCellProduct")
Me!txtQuantitySet = DLookup("SetQuantity", "qryCellProduct")
Me!txtShadowProduct.Value = Me![txtProduct]
Me!txtShadowQuantitySet.Value = Me![txtQuantitySet]
Me!txtShadowDate.Value = Me![Date]

stDocName = "frmVendingMachineCell"
DoCmd.OpenForm stDocName

Forms!frmVendingmachineCell!txtProductShadow = DLookup("product",
"qryCellProduct")
Forms!frmVendingmachineCell!txtQuantitySet = DLookup("SetQuantity",
"qryCellProduct")
Forms!frmVendingmachineCell!txtGridShadow = Forms!frmVendingmachine!
txtGrid
Forms!frmVendingmachineCell!txtDateShadow = Forms!frmVendingmachine!
txtShadowDate

End Sub
 
S

Svetlana

Private Sub cmdA2_Click()
Dim strButtonName As String

strButtonName = Mid(Me.cmdA2.Name, 4)
Me!txtGrid= strButtonName
 
A

AccessVandal via AccessMonster.com

Try this,

Private Sub cmdA2_Click()

Dim stDocName As String
Dim stLinkCriteria As String

‘add this – txtGrid must be unbound
Me.txtGrid = GoGetControlName(Me.Form.Name)

Your cmdA2 has a Caption property, name it as “A2”
Use the control caption naming to populate the textbox “txtGrid”
when the button is click.

Create a function

Public Function GoGetControlName(strCtlName As String) As String

Dim frm As Form, ctl As Control

For Each frm In Forms
If frm.Name = strCtlName Then
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acCommandButton
'Debug.Print ctl.Name
GoGetControlName = ctl.Caption ‘your unbound txtGrid.value
End Select
Next ctl
End If
Next frm

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top