Solved

How do I define a variable in VBA?

Posted on 2015-01-30
6
68 Views
Last Modified: 2016-02-11
Part I
I have a 16 condition macro that I converted to VBA. Now, I'd like to learn how to set the strFilter as the variable to call up that code. Here's the code with the beginning, ending, and one of the conditions.
Private Sub cmd_Run_Report_Click()
On Error GoTo cmd_Run_Report_Click_Err

    If (Eval("[Forms]![frm_TrainingByEe]![cbo_Shift] Is Null And [Forms]![frm_TrainingByEe]![cbo_Bank] Is Null And [Forms]![frm_TrainingByEe]![cbo_Flow_ID] Is Null And [Forms]![frm_TrainingByEe]![cbo_Queue_ID] Is Not Null")) Then
        DoCmd.OpenReport "rpt_Training_Employees", acViewReport, "", "[Queue_fk]=[Forms]![frm_TrainingByEe]![cbo_Queue_ID]", acNormal
    End If

cmd_Run_Report_Click_Exit:
    Exit Sub

cmd_Run_Report_Click_Err:
    MsgBox Error$
    Resume cmd_Run_Report_Click_Exit

Open in new window

How would I define the conditions as the variable? Exactly where would I do that in the VBA code?

Part II
I would be using this  variable to open a report, which I have been made to understand to use as follows:
Docmd.OpenReport "YourReport",acViewReport ,,strFilter)

When I put this strFilter code behind a command button, would I need to add anything else to it in the Build Code window?
0
Comment
Question by:David Bigelow
  • 3
  • 2
6 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40580455
from what you posted

[Queue_fk] value is selected from [cbo_Queue_ID]

what field is filtered by  [cbo_Shift] ?
what field is filtered by  [cbo_Bank] ?
what field is filtered by  [cbo_Flow_ID] ?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40580463
is the button cmd_Run_Report in the form  "frm_TrainingByEe" ?
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40580479
Private Sub cmd_Run_Report_Click()
On Error GoTo cmd_Run_Report_Click_Err

dim strFilter as string

If Me.[cbo_Shift] & ""<>"" then
   strFilter="[Shift]= '" & me.[cbo_Shift] & "'"
end if

If me.[cbo_Bank] & ""<> "" then
   if strFilter="" then
		strFilter="[Bank]='" & me.[cbo_Bank] & "'"
		else
		strFilter=strFilter & " And [Bank]='" & me.[cbo_Bank] & "'"
	end if
end if
If me.[cbo_Flow_ID] & ""<> "" then
   if strFilter="" then
		strFilter="[Flow_ID]=" & me.[cbo_Flow_ID] & "
		else
		strFilter=strFilter & " And [Flow_ID]=" & me.[cbo_Flow_ID] & "
	end if
end if

if strFilter & ""<>"" then
   Docmd.OpenReport "YourReport",acViewReport ,,strFilter
   else
   Docmd.OpenReport "YourReport",acViewReport
end if

Open in new window

0
 
LVL 1

Author Comment

by:David Bigelow
ID: 40580484
The field filters are on the form, "frm_TrainingByEe" combo boxes.

what field is filtered by  [cbo_Shift] ? like am/pm, the field is named Shift
 what field is filtered by  [cbo_Bank] ? a team of workers, the field is named Bank
 what field is filtered by  [cbo_Flow_ID] ? workloads, the field is named Flow_fk

Yes, the button cmd_Run_Report is on the form "frm_TrainingByEe" .
0
 
LVL 1

Author Closing Comment

by:David Bigelow
ID: 40761082
At the time, this was too much for me to understand and now the database is done. I decided to leave the design here as it was without switching to vba.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now