Solved

How do I define a variable in VBA?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

17 Experts available now in Live!

Get 1:1 Help Now