Solved

How do I define a variable in VBA?

Posted on 2015-01-30
6
72 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server views 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 Access…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…

730 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