将 VB 代码转换为 VB 脚本
Posted
技术标签:
【中文标题】将 VB 代码转换为 VB 脚本【英文标题】:Convert VB code to VB Script 【发布时间】:2014-06-30 12:54:52 【问题描述】:我正在尝试将 VBA 代码转换为 VB 脚本,以便可以从桌面上的文件运行它。我正在尝试自动化每天手动完成的任务。有一个 MS Access 表单,我必须登录,然后单击生成 Excel 文件作为输出的按钮。
以下是我的问题。
-
我们可以把用户名和密码放在VB脚本中,这样在执行脚本时它就不会提示输入密码了吗?
能否将 Excel 输出保存到某个位置并使用日期重命名?示例 - PODueReport01022014.xls
VBA代码如下。
Public Function PODueReport() As ADODB.Recordset
Dim strSQL As String, strFrom As String, strWhere As String
strSQL = "Select distinct case when pp.NRCCorDirect='NRSC' then cm.requested_date-27-vert.LeadTime ELSE cm.requested_date-6-vert.LeadTime END AS [PO Due Date]"
strSQL = strSQL & ",cm.ID AS ChangeNumber"
strSQL = strSQL & ",cm.title as [Project Title]"
strSQL = strSQL & ",cm.requested_date as [Set Date]"
strSQL = strSQL & ",trex.RetrofitLead AS [Project Lead]"
strSQL = strSQL & ",bgt.ProjectCode as [Project Code (Target)]"
strSQL = strSQL & ",bgt.ProjectCode2 as [Project Code (Vendor)]"
strSQL = strSQL & ",pp.ExistingPartNum as [Part #]"
strSQL = strSQL & ",pp.ItemDesc as [Fixture Description]"
strSQL = strSQL & ",pp.SOURCINGSPECIALIST as [Sourcing Specialist]"
strSQL = strSQL & ",vert.Vendor"
strSQL = strSQL & ",vert.EarlyCommitQty"
strSQL = strSQL & ",vert.CommitDate"
strSQL = strSQL & ",vert.ACTUALPOCUTDATE As [PO Cut Date]"
strSQL = strSQL & ",case when bgt.ProjectCode is null and bgt.ProjectCode2 is null and trex.ActProjApprovedDate is null then 'No project code; Project Code Assigned box not checked' when trex.ActProjApprovedDate is null then 'Project Code Assigned box not checked' else 'No project code' end as [Type Of Issue] "
strFrom = "Select distinct perfect_placement_id, vendor, leadtime, EarlyCommitQty, CommitDate, PONUM, ACTUALPOCUTDATE "
strFrom = strFrom & "From CSD.dbo.TBLREVIEWQUOTEVERTICAL "
strFrom = strFrom & "where not QUOTEAPPROVALDATE is null " 'filter 2. quote approved
strFrom = strFrom & "and (ponum is null or ponum=0) " 'filter 4. no PO or PO=0
strFrom = strFrom & "and not Perfect_Placement_ID is null and Perfect_Placement_ID <> ''"
strFrom = strFrom & "and not Vendor is null and Vendor <> ''"
strFrom = "(" & strFrom & ") vert left join CSD.dbo.TBLPERFECTPLACEMENTLOCAL pp on pp.Perfect_Placement_Id = vert.Perfect_Placement_Id"
strFrom = "(" & strFrom & ") left join CSD.dbo.Retro_tblChangeRequests_Local trex on trex.Changenumber=pp.changenumber"
strFrom = "(" & strFrom & ") left join CSD.dbo.TBLBUDGETS bgt on trex.Changenumber=bgt.changenumber"
strFrom = "(" & strFrom & ") left join CSD.dbo.PRCRM_DET_W cm on cm.change_request_id=trex.PrologRecordID"
strWhere = "cm.status = 'go' "
strWhere = strWhere & " and case when pp.NRCCorDirect='NRSC' then cm.requested_date-27-vert.LeadTime ELSE cm.requested_date-6-vert.LeadTime END >='" & Date - 150 & "'" 'filter 3. PO Due date between today -150 and today +30
strWhere = strWhere & " and case when pp.NRCCorDirect='NRSC' then cm.requested_date-27-vert.LeadTime ELSE cm.requested_date-6-vert.LeadTime END <='" & Date + 30 & "'"
strWhere = strWhere & " and ((bgt.ProjectCode is null and bgt.ProjectCode2 is null) OR trex.ActProjApprovedDate is null)"
strWhere = strWhere & " and not pp.changenumber is null and pp.changenumber <> ''"
strWhere = strWhere & " and not pp.Perfect_Placement_ID is null and pp.Perfect_Placement_ID <> ''"
strWhere = strWhere & " and not pp.ExistingPartNum is null and pp.ExistingPartNum <> ''"
strWhere = strWhere & " and trex.DispositionNeeded <> 1"
strSQL = strSQL & " From " & strFrom & " Where " & strWhere
strSQL = strSQL & " Order by case when pp.NRCCorDirect='NRSC' then cm.requested_date-27-vert.LeadTime ELSE cm.requested_date-6-vert.LeadTime END, cm.ID, pp.ExistingPartNum"
Set PODueReport = SQLSet(1, strSQL, True)
End Function
非常感谢任何有关解决问题的帮助或想法。
【问题讨论】:
【参考方案1】:最好让 VBS 文件打开数据库,然后执行上面的函数。由于您想要一个特定的文件名,您最好将该信息输入到上述 VBA 中的函数中。
查看this answer 以帮助您入门。
基本上你从这个答案中学到的是你需要在你的 VBS 中创建一个“Access.Application”对象。完成此操作后,您就可以访问在 VBA 中通常必须访问的所有方法和属性。
当您更仔细地查看我链接的答案和以下两种方法时,您将开始了解您可以使用密码打开数据库,然后运行您想要执行的函数或宏。
您创建的 VBS 文件可以随时执行。
如果您希望将 VBS 文件添加到 Windows 任务计划程序,通常最好使用 c:\windows\syswow64\cscript.exe
应用程序和 .VBS 文件的完整路径作为构建任务时应用程序的参数。
Application.Run Method (Access)
Application.OpenCurrentDatabase Method (Access)
【讨论】:
以上是关于将 VB 代码转换为 VB 脚本的主要内容,如果未能解决你的问题,请参考以下文章