Hello,
I have created a search form containing combo boxes and command buttons and would like add an option group to the form.
On my search form, the user must select a department (deptcombo), and then must select a supervisor (supcombo), with the option of selecting an employee (empcombo), before they can click on the button that opens the required form. I would like to add in an option group where, after selecting the department and supervisor, the user then selects the data year (2011, 2012, etc.) and from there clicks on a command button to pull up the appropriate form containing data for the year selected. I have included the field DataYear to all my tables and queries and need to find a way to separate the years when pulling the reports/forms. (I am very new to vba, and have been trying for almost a week to find the right code, with no luck.)
The code I have for one of the command buttons is as follows: (this command button pulls the previous 6 months of data; I need to separate the years somehow)
Private Sub CmdPrev6Mths_Click()
On Error GoTo CmdPrev6Mths_Click_Err
If Forms!Switchboard!DeptCombo & ""= "" And [Forms]![Switchboard]![SupCombo] & ""="" Then
Msgbox "Missing Dept and Supervisor"
Exit Sub
End if
If Forms!Switchboard!DeptCombo & ""= "" Then
Msgbox "Missing Dept"
Exit Sub
End if
If [Forms]![Switchboard]![SupCombo] & ""="" Then
Msgbox "Missing Supervisor"
Exit Sub
End if
<I WOULD LIKE TO ENTER CODE FOR THE DATAYEAR OPTION GROUP HERE>
OPTION VALUE 2 = 2011
OPTION VALUE 1 = 2012
If (Forms!Switchboard!DeptCombo = "Tech Ops") Then
' Open Tech Ops Scorecard Metrics Form
DoCmd.OpenForm "_frm_Scorecard_Mth", acNormal, "", "[_qry_Scorecard_Mth]![Supervisor] Like ""*"" & [Forms]![Switchboard]![SupCombo] & ""*"" And [_qry_Scorecard_Mth]![EmpName] Like ""*"" & [Forms]![Switchboard]![EmpCombo] & ""*"" And [_qry_Scorecard_Mth]![MthEndDate]>=Date()-183", , acNormal
End If
If (Forms!Switchboard!DeptCombo = "Plant Ops") Then
' Open Plant Ops Scorecard Metrics Form
DoCmd.OpenForm "_frm_Plant_Mth", acNormal, "", "[_qry_Plant_Mth]![Supervisor] Like ""*"" & [Forms]![Switchboard]![SupCombo] & ""*"" And [_qry_Plant_Mth]![EmpName] Like ""*"" & [Forms]![Switchboard]![EmpCombo] & ""*"" And [_qry_Plant_Mth]![MthEndDate]>=Date()-183", , acNormal
End If
DoCmd.Close acForm, "Switchboard"
CmdPrev6Mths_Click_Exit:
Exit Sub
CmdPrev6Mths_Click_Err:
MsgBox Error$
Resume CmdPrev6Mths_Click_Exit
End Sub
Someone told me to use either the Year or DateDiff functions, but I don’t know how to code those to bring it all together. I am extremely new to vba and am really hoping you can help me.