If Statement to Run Macro

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
Kind of an If statement.

I have an If statement that depending on the results will run one or another Macro. But I get a Debug error when I try to run it. The fist is the If statement that errors the second is one of the Macro's (which runs fine on its own - its in a module)

Code:
Private Sub Frame244_Click()
Select Case Frame244
    Case Is = 1
'run Macro
        updateQuery1
    Case Is = 2
'run Macro
        updateQuery2
End Select
End Sub
Code:
Private Sub updateQuery1()
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim qdf As DAO.QueryDef
    
    Set qdf = db.QueryDefs("qry_NIS_TSL")
    qdf.SQL = "SELECT tbl_NIS_TSL.ID_NISTSL, tbl_NIS_TSL.VendorName, tbl_NIS_TSL.VendorID, tbl_NIS_TSL.CityName, tbl_NIS_TSL.MailState, tbl_NIS_TSL.PostalCode, tbl_NIS_TSL.CountryCode, tbl_NIS_TSL.Vendor_Status, tbl_NIS_TSL.CommType, tbl_NIS_TSL.Debarred, tbl_NIS_TSL.Approval_Status, tbl_NIS_TSL.TSL_Trend, tbl_NIS_TSL.Complexity_Low, tbl_NIS_TSL.Complexity_Medium, tbl_NIS_TSL.Complexity_High, tbl_NIS_TSL.Volume_Low, tbl_NIS_TSL.Volume_Medium, tbl_NIS_TSL.Volume_High, tbl_NIS_TSL.Responsiveness_Rating, tbl_NIS_TSL.RTV_Support, tbl_NIS_TSL.Failure_Analysis, tbl_NIS_TSL.Other_Capabilities, tbl_NIS_TSL.NumberOf_Assemblies, tbl_NIS_TSL.Materials, tbl_NIS_TSL.Restrictions, tbl_NIS_TSL.Comments, tbl_NIS_TSL.CreatedBy, tbl_NIS_TSL.CreatedDate " & _
    "FROM tbl_NIS_TSL INNER JOIN SubQry_NIS_TSL ON (tbl_NIS_TSL.VendorName = SubQry_NIS_TSL.VendorName) AND (tbl_NIS_TSL.CommType = SubQry_NIS_TSL.CommType) AND (tbl_NIS_TSL.CreatedDate = SubQry_NIS_TSL.MaxOfCreatedDate)" & _
    "WHERE (((tbl_NIS_TSL.VendorName) Like [Forms]![frm_NIS_TSL]![Combo227]) And ((tbl_NIS_TSL.CommType) Like [Forms]![frm_NIS_TSL]![Combo232]) And ((tbl_NIS_TSL.Debarred) Like [Forms]![frm_NIS_TSL]![Combo229]) And ((tbl_NIS_TSL.Approval_Status) Like [Forms]![frm_NIS_TSL]![Combo234]))ORDER BY tbl_NIS_TSL.VendorName, tbl_NIS_TSL.CreatedDate DESC;"
End Sub
thanks for the help
 
Last edited:

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,067
Office Version
365
Platform
Windows
Re: Access: If Statement to Run Macro

If the two code blocks you posted above are in DIFFERENT modules, then you need to remove the word "Private" from:
Code:
[COLOR=#ff0000]Private [/COLOR]Sub updateQuery1()
Marking it as "Private" means that it cannot be seen by anything outside of that module it is contained in. You cannot call things that you cannot see.

Sometimes, people will replace the word "Private" with "Public", but I think if you leave the word off altogether, the default is "Public".
 
Last edited:

petertenthije

Board Regular
Joined
Sep 25, 2012
Messages
176
Re: Access: If Statement to Run Macro

Marking it as "Private" means that it cannot be seen by anything outside of that module it is contained in. You cannot call things that you cannot see.
You can call a private macro from another module by using "application.run".

Application.Run "ModuleName.MacroName"
 
Last edited:

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
Re: Access: If Statement to Run Macro

You can call a private macro from another module by using "application.run".

Application.Run "ModuleName.MacroName"
Think this is referring to Excel, Macros are a different object in Access, furthermore, I don't think this works with Access subroutines.
 
Last edited:

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
1,690
Re: Access: If Statement to Run Macro

Thank You!

Is there a way to set the Frame value to 1 other than using the default value?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,067
Office Version
365
Platform
Windows
Re: Access: If Statement to Run Macro

Is there a way to set the Frame value to 1 other than using the default value?
Yes, go into the Properties of the Frame, and you can rename it (as long as you choose a name that isn't already being used on that Form).
 

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
710
Re: Access: If Statement to Run Macro

Think this is referring to Excel, Macros are a different object in Access, furthermore, I don't think this works with Access subroutines.
No you can do it, do not even need the Application.Run, just use ModuleName.SuborFunctionName if the code is marked Private.

HTH
 

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
Re: Access: If Statement to Run Macro

No you can do it, do not even need the Application.Run, just use ModuleName.SuborFunctionName if the code is marked Private.

HTH

In Access 2016 this doesn't work. I have tested it as per the OP's scenario, without the private statement it works fine, with the private statement the following error is returned.

---------------------------
Microsoft Visual Basic for Applications
---------------------------
Compile error:


Method or data member not found
---------------------------
OK Help
---------------------------
 

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
710
Re: Access: If Statement to Run Macro

Hmm, strange.:confused:

I did test in 2007 before I posted, as I was keen to know.
Good to know though. Thank you.
 

Forum statistics

Threads
1,089,312
Messages
5,407,506
Members
403,150
Latest member
stellalol

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top