?
Solved

How do I define a variable in VBA?

Posted on 2015-01-30
6
Medium Priority
?
94 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
5 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 1500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

839 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