将VB代码转换为VB脚本

时间:2014-06-30 12:54:52

标签: sql vbscript scripting access-vba scheduled-tasks

我正在尝试将VBA代码转换为VB脚本,以便可以从桌面上的文件运行它。我正在尝试自动执行每天手动完成的任务。有一个MS Access表单,我必须登录,然后单击一个生成Excel文件作为输出的按钮。

以下是我的问题。

  1. 我们可以将用户名和密码放在VB脚本中,以便在执行脚本时不会提示输入密码吗?
  2. Excel输出可以保存到某个位置并使用日期重命名吗?示例 - PODueReport01022014.xls
  3. 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 个答案:

答案 0 :(得分:0)

你可能最好这样做,以便VBS文件打开数据库,并执行上面的功能。由于您需要特定的文件名,因此最好将该信息输入上述VBA中的函数。

结帐this answer让您入门。

基本上你从答案中学到的是你需要创建一个&#34; Access.Application&#34;您的VBS中的对象。一旦这样做,您就可以访问您必须在VBA中正常访问的所有方法和属性。

当您更仔细地查看我链接的答案和以下两个方法时,您将开始了解您可以使用密码打开数据库,然后运行您要执行的函数或宏。

您创建的VBS文件可以随时执行。

如果您希望将VBS文件添加到Windows任务计划程序,通常最好使用c:\windows\syswow64\cscript.exe应用程序和.VBS文件的完整路径作为应用程序的参数,因为您正在构建任务。

Application.Run Method (Access)

Application.OpenCurrentDatabase Method (Access)