Can I apply code to a Command Button that uses a variable based on the cmdName?

adambc

Active Member
Joined
Jan 13, 2020
Messages
373
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have multiple Command Buttons that call another UserForm to pick a date - currently I have separate Private Subs for each button ...

VBA Code:
Private Sub cmdIncidentDate_Click()

Dim dateVariable As Date
dateVariable = CalendarForm.GetDate

If dateVariable <> 0 Then
    Me.txtIncidentDate.Text = Format(CLng(dateVariable), "dd/mm/yyyy")
End If

End Sub

Private Sub cmdReportedDate_Click()

Dim dateVariable As Date
dateVariable = CalendarForm.GetDate

If dateVariable <> 0 Then
    Me.txtReportedDate.Text = Format(CLng(dateVariable), "dd/mm/yyyy")
End If

End Sub


But is there a way to do this using the cmdName (where cmdName is the actual name of the button) as a variable eg ...

Code:
Private Sub cmdName_Click()

Dim dateVariable As Date
dateVariable = CalendarForm.GetDate

If dateVariable <> 0 Then
    Me.txtName.Text = Format(CLng(dateVariable), "dd/mm/yyyy")
End If

End Sub

Thanks ...
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I'd just add a separate routine like this:

Code:
Function GetFormattedDate() As String
Dim dateVariable As Date
dateVariable = CalendarForm.GetDate

If dateVariable <> 0 Then
GetFormattedDate = Format$(dateVariable, "dd/mm/yyyy")
End If
End Function

Then call that from the buttons:

Code:
Private Sub cmdIncidentDate_Click()

Me.txtIncidentDate.Text = GetFormattedDate()

End Sub

Private Sub cmdReportedDate_Click()

Me.txtReportedDate.Text = GetFormattedDate()

End Sub
 
Upvote 0
OK, but I still have to write a separate call for every button - I was hoping to be able to click cmdIncidentDate and populate txtIncidentDate (might have to be txtcmdIncidentDate) as a variable?
 
Upvote 0
You'd need a class module for that - there are numerous examples of that in the forum. Unless you have a lot of buttons, it's overkill, in my opinion.
 
Upvote 0
@RoryA

Thanks - and agree it's an overkill for what I have ...

Thanks to all for your ideas ...
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top