Solved

How do I define a variable in VBA?

Posted on 2015-01-30
6
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40580463
is the button cmd_Run_Report in the form  "frm_TrainingByEe" ?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

726 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