Dim FiscalMonth作为整数'在MTD表上的财政月份 Dim FiscalMYear作为整数'在MTD表上的财政年度 Dim WEDate作为日期''MTD表上的最大周结束日期 Dim FiscalQYear作为整数'QTD表财年' Dim FiscalQuarter as Integer''财政季度QTD表 Dim FiscalYYear整数'年度财政年度表' ''获取MTD的财政月,年和最后(最长)周结束日期查看是否是要求处理的周末的正确数据 FiscalMonth = DLookup( " [[FiscalMonthNum]"" tmptblMTDSalesData",_ " [WeekEndDate] = Forms![frmPrintReports]!GetWkMoDate") FiscalMYear = DLookup(" [Fisca lYearNum]"," tmptblMTDSalesData",_ " [WeekEndDate] = Forms![frmPrintReports]!GetWkMoDate")''如何获得MAX周结束日期?'' WEDate = DLookup(" [WeekEndDate]"" tmptblMTDSalesData",_ ''" [WeekEndDate] = Forms![frmPrintReports]!GetWkMoDate") ''获取Qtr表上的季度和年份(最大销售日期也是如此?) FiscalQYear = DLookup(" [FiscalYearNum]"" tmptblQTDSalesData",_ " [SalesDate ] = Forms![frmPrintReports]!GetWkMoDate") FiscalQuarter = DLookup(" [FiscalQtrNum]"" tmptblQTDSalesData",_ " [SalesDate] = Forms![frmPrintReports]!GetWkMoDate" ;) ''获取年度财政年度表(也需要最大销售日期?) FiscalYear = DLookup(" [FiscalYearNum]",tmptblYTDSalesData",_ " [SalesDat e] =表格![frmPrintReports]!GetWkMoDate") ''向用户显示所有日期。如果不对,请停止代码以便他们可以运行数据收集 MsgBoxMTD表上的财政月份和年份是 &安培; FiscalMonth& " & FiscalMYear _ & QTD表中的财政季度和年度是 &安培; FiscalQuarter& " " &安培; FiscalQYear _ & 年初至今表上的财政年度是指&安培;财政年度_ & 如果其中任何一项不正确,请重新运行Merch Pct以创建表格数据 结束功能 I am trying my first functions in my code. I have a set of queries that runs to create temp tables with the right data for some reports. They can run for a long time, so I want the user to know what is in the tables (date parameters) before running all the queries. This way, if the data in the tables is not for the time period the user needs for reporting, s/he knows to recreate the data. I just want to find the data in the tables and display it to the user - probably will do a YES/NO to continue or not. So, I wrote the following function, and I want to test it in the immediate window (without running the whole process from the form). I just can''t figure it out. I don''t understand the use of "passing parameters" - when you call the function (which I''ll have to do from my code as well) what do I "pass" to the function and what does that look like? I also want to find the MAX Salesdate on the tmptblMTDSales. Can I do this in DLOOKUP too? Hope someone can help - it''s very confusing to me and Help doesn''t explain for me. Thanks Code: Function GetMoQtrYear(MoEndDate As Date) '' Find the dates on the Temp Tables. If they are not right, tell the user to rerun '' Merch Pct incr/decr to put the right data in the tables used in the dashboard reports Dim FiscalMonth As Integer '' Fiscal Month on MTD Table Dim FiscalMYear As Integer '' Fiscal Year on MTD Table Dim WEDate As Date '' Max Week End date on MTD Table Dim FiscalQYear As Integer '' Fiscal Year on QTD Table Dim FiscalQuarter As Integer '' Fiscal Quarter on QTD Table Dim FiscalYYear As Integer '' Fiscal Year on YTD Table '' Get the Fiscal Month, Year and Last (max) week end date on MTD to see if it''s the '' right data for the week end requested to process FiscalMonth = DLookup("[FiscalMonthNum]", "tmptblMTDSalesData", _ "[WeekEndDate]= Forms![frmPrintReports]!GetWkMoDate") FiscalMYear = DLookup("[FiscalYearNum]", "tmptblMTDSalesData", _ "[WeekEndDate]= Forms![frmPrintReports]!GetWkMoDate") '' How to get MAX week end date? '' WEDate = DLookup("[WeekEndDate]", "tmptblMTDSalesData", _ '' "[WeekEndDate]= Forms![frmPrintReports]!GetWkMoDate") '' Get the Quarter and Year on the Qtr Table (Max sales date needed too?) FiscalQYear = DLookup("[FiscalYearNum]", "tmptblQTDSalesData", _ "[SalesDate]= Forms![frmPrintReports]!GetWkMoDate") FiscalQuarter = DLookup("[FiscalQtrNum]", "tmptblQTDSalesData", _ "[SalesDate]= Forms![frmPrintReports]!GetWkMoDate") '' Get the Fiscal Year on the Year table (Max sales date needed too?) FiscalYear = DLookup("[FiscalYearNum]", "tmptblYTDSalesData", _ "[SalesDate]= Forms![frmPrintReports]!GetWkMoDate") '' Show all dates to the user. If not right, stop the code so they can run the data gathering MsgBox "Fiscal Month and Year on MTD Table is " & FiscalMonth & " " & FiscalMYear _ & "Fiscal Quarter and Year on QTD Table is " & FiscalQuarter & " " & FiscalQYear _ & "Fiscal Year on YTD Table is " & FiscalYear _ & "If any of these is NOT right, rerun Merch Pct to create Table Data" End Function 你开始关闭 函数GetMoQtrYear(MoEndDate为日期) 然后你永远不会使用MoEndDate(传递的日期值)。 然后你用Dlookup中的过滤器 Forms![frmPrintReports]!GetWkMoDate 嗯,你需要打开frmPrintReports来获取日期。 GetWkMoDate和MoEndDate一样是 ... IOW,MoEndDate是你想要计算的值吗? 如果是这样,改变所有 表格![frmPrintReports]!GetWkMoDate to MoEndDate 典型的Dlookup看起来像 FiscalQYear = DLookup(" [FiscalYearNum]"" tmptblQTDSalesData",_ " [SalesDate] =#"& MoEndDate&" #" 日期被#'包围,字符串用或和数字没有 包围的字符。 从即时窗口你可以输入 ?GetMoQtrYear(Date()) 来测试 然后在你的表格中你可以通过 Dim var As Variant var = GetMoQtrYear(Forms!frmPrintReports!GetWkMoDate) 函数通常返回一个值;一个字符串,一个真/假值,一个 数字,一个日期等等。你真的没有回复你的 函数中的任何东西,因为你从未为函数赋值返回。 这是一个函数示例 ''As Long descibles返回的类型。 公共函数SumIt(lngA As Long,lngB as Long)As Long ''SumIt = assigns lngA + lngB的值并返回 ''总和值到你调用的地方。 SumIt = lngA + lngB 结束函数 从立即窗口,您现在可以输入 ? Sumit(1,2)You start off withFunction GetMoQtrYear(MoEndDate As Date)and then you never use MoEndDate (the passed date value).Then you use for the filters in DlookupForms![frmPrintReports]!GetWkMoDateWell, you need frmPrintReports open to get the date. Is GetWkMoDate thesame as MoEndDate...IOW, is MoEndDate the value you want to calc on?If so, change all references ofForms![frmPrintReports]!GetWkMoDatetoMoEndDateA typical Dlookup would look something likeFiscalQYear = DLookup("[FiscalYearNum]", "tmptblQTDSalesData", _"[SalesDate]= #" & MoEndDate & "#"Dates are surrounded by #''s, strings by " or '' and numbers with noenclosed characters.From the immediate window you could enter? GetMoQtrYear(Date())to testThen in your form you can passDim var As Variantvar = GetMoQtrYear(Forms!frmPrintReports!GetWkMoDate)Functions usually return a value; a string, a true/false value, anumber, a date, etc. You really aren''t returning anything in yourfunction since you never assign a value to the function to return.Here''s a function example''the As Long descibles the type returned.Public Function SumIt(lngA As Long, lngB As Long) As Long''SumIt = assigns the values of lngA+lngB and returns''the summed value to wherever you called it.SumIt = lngA + lngBEnd FunctionFrom the Immediate window you can now enter? Sumit(1,2)谢谢 - 到目前为止! 首先,我可以做到在训练模式中起作用,(lngA, lngB等),但我不能将其翻译成真实的。工作。 我已经尝试过你在这里说的话,并且有一些问题和答案 你的问题。 首先,用户在名为GetWkMoDate的表单中输入一个字段结束日期(对于报告)到字段 。我确实在临时表上放了日期,所以, 我想我可以使用这个日期在3 临时表中查找我需要的值。 我不明白将什么放入函数 语句中的变量(如果它可以解释为例如: 函数NameofFunction(NameOfVariable1PassedIn作为VarType1, NameofVariable2PassedIn作为VarType2等等,直到所有传入都是 定义)AS NameofVariable1withFunctionResult, NameofVariable2withFunctionResult。 混淆也与我的尺寸有关吗?我想在用户输入的日期带上 ,传递给它到函数,并有 函数计算Mo,年,季度,QYear和年份(也许 这些表中的一个或多个的MAX日期) ,所以我可以告诉用户 表中的这些值是什么.Mo,Year,Quarter,QYear和 年份都是Int egers和SalesDate(如果我做MAX的事情)是一个 日期格式。 我该怎么做?也许我甚至不应该做一个功能?我还必须把b 放入错误处理中,这样如果用户想要,比如12/3/05和桌面上的 日期是10/29/05 ,我可以处理错误。我认为这样的方式 我有它,DLookup提出Null因为10/29/05不在 MTD表上。 这有什么意义吗?我希望某处有一个VBA课程。我的b $ b有5本或6本书,我只是无法从简单的 例子转换成更复杂的现实世界。 我也尝试用以下函数运行: 更改我的代码之后的GetMoQtrYear(Date())我得到了Sub或Function not defined。 (我在函数声明中输入了As 日期,并将所有引用更改为#"& MoEndDate&"#"并确保表单打开日期输入。 非常感谢。 saraThanks - so far!First of all, I can do the function in the "training mode", (lngA,lngB, etc) but I can''t translate it into "real" work.I''ve tried what you said here and have some questions and an answer toyour question.First, The user enters a week ending date (for the report) into a fieldon the form called GetWkMoDate. I do put dates on the temp tables, so,I figured I could use this date to look up the values I need in the 3temp tables.I don''t understand what to put in the variables in the Functionstatement (it would help if it could be explained as, for example:Function NameofFunction (NameOfVariable1PassedIn As VarType1,NameofVariable2PassedIn as VarType2, etc. till all passed in aredefined) AS NameofVariable1withFunctionResult,NameofVariable2withFunctionResult.The confusion is related, also, to what do I dimension? I want to takethe DATE the user entered, pass it in to the function, and have thefunction figure out the Mo, Year, Quarter, QYear and Year (and maybethe MAX date on one or more of these tables), so I can tell the userwhat these values are on the tables. The Mo, Year, Quarter, QYear andYear are all Integers, and the SalesDate (if I do the MAX thing) is aDate format.How do I do this? Maybe I shouldn''t even do a function? I also have toput error handling in so that if the user wants, say, 12/3/05 and thedate on the table is 10/29/05, I can handle the error. I think the wayI have it, DLookup comes up with "Null" because 10/29/05 isn''t on theMTD table.Does this make any sense? I wish there was a VBA class somewhere. Ihave 5 or 6 books and am just not able to translate from simpleexamples into the more complex world of reality.I also tried to run the function with: ? GetMoQtrYear(Date()) afterchanging my code and I got "Sub or Function not defined". (I put in AsDate in the Function statement and changed all the references to #" &MoEndDate & "#" and made sure the form was open with the date typed in.thank you very much.sara Sara: 我对你要完成的事情有点困惑 这个函数。听起来你想要约会来自 的临时表值,然后告诉你的用户哪些记录已经建成了 。然后,如果他们需要的数量超过了已建成的数量,请给出 他们可以选择重建数据。这是正确的吗? 请澄清一下,这样我们就可以引导你朝着正确的方向前进:) JanaSara:I am a little confused about what you are trying to accomplish withthis function. It sounds like you are trying to get date values fromyour temp tables and then tell your user which records have alreadybeen built. Then, if they need more than what has been built, givethem the option to rebuild the data. Is that correct?Please clarify so we can steer you in the right direction :)Jana 这篇关于如何在即时窗口中测试函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 16:45
查看更多