如何在执行之前创建可以在SQL查询中设置变量的用户表单?

时间:2013-05-10 14:42:46

标签: sql ms-access userform

我在SharePoint中持续输入数据;此表在MS Access中链接,并且每月一次通过将数据从实时SharePoint数据库复制到静态Access数据库来冻结数据。我使用SQL查询执行此操作,并且它运行良好。

以下是查询的要点(我对此示例进行了简化并删除了字段):

INSERT INTO [CALL DATA] ( FiscalMonth, FiscalYear, EncDateTime, TimeDuration )
SELECT "07-Apr" AS FiscalMonth, "FY13" AS FiscalYear, 
CallAttempts.[Attempt Date & Time], CallAttempts.[Attempt Duration (Mins)]
FROM CallAttempts
WHERE ((CallAttempts.[Attempt Date & Time]>#4/1/2013#) AND 
       (CallAttempts.[Attempt Date & Time]<#5/1/2013#) AND 
       (CallAttempts.[Attempt Duration (Mins)] IS NOT NULL));

正如您所看到的,每个月,我都必须手动更新此查询的部分内容:FiscalMonthFiscalYear的值以及日期和时间限制。这对我来说很好(我编码)但我必须把它交给非技术人员。

我不熟悉Access - 我可以创建一个在执行之前更新此查询的用户表单(我想添加下拉选项和数据选择器以供执行)吗?这甚至可能吗?

还是有更好的方法来解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

是的,你很容易。这应该让你开始:

Private Sub cmdGo_Click()
   Dim Mo As Integer
   Dim Yr As Integer
   Dim strSQL As String
   Dim Mon As String
   Dim DateStart As Date
   Dim DateEnd As Date

   strSQL = ""
   strSQL = strSQL & "INSERT INTO [call data] " & vbCrLf
   strSQL = strSQL & "            (fiscalmonth, " & vbCrLf
   strSQL = strSQL & "             fiscalyear, " & vbCrLf
   strSQL = strSQL & "             encdatetime, " & vbCrLf
   strSQL = strSQL & "             timeduration) " & vbCrLf
   strSQL = strSQL & "SELECT '07-@Mon' AS FiscalMonth, " & vbCrLf
   strSQL = strSQL & "       'fy@yr'   AS FiscalYear, " & vbCrLf
   strSQL = strSQL & "       callattempts.[attempt date & time], " & vbCrLf
   strSQL = strSQL & "       callattempts.[attempt duration (mins)] " & vbCrLf
   strSQL = strSQL & "FROM   callattempts " & vbCrLf
   strSQL = strSQL & "WHERE  ( ( callattempts.[attempt date & time] >= #@DateStart# ) " & vbCrLf
   strSQL = strSQL & "         AND ( callattempts.[attempt date & time] <#@DateEnd# ) " & vbCrLf
   strSQL = strSQL & "         AND ( callattempts.[attempt duration (mins)] IS NOT NULL ) )"

   Mo = Me.cboMonth.Value
   Yr = Me.cboYear.Value
   Mon = MonthName(Mo, True)
   DateStart = DateSerial(Yr, Mo, 1)
   DateEnd = DateAdd("m", 1, DateStart)
   strSQL = Replace(strSQL, "@mon", Mon)
   strSQL = Replace(strSQL, "@Yr", Yr)
   strSQL = Replace(strSQL, "@DateStart", DateStart)
   strSQL = Replace(strSQL, "@DateEnd", DateEnd)

   'Debug.Print strSQL
End Sub