我正在尝试使Microsoft Access查询依赖于另一个表单的文本框中的值。
这是现在的标准。基本上,2014年4月1日至2015年3月31日之间的任何日期。这很有效。
>=#2014-04-01# And <#2015-04-01#
我想要一个包含年份的文本框(使用当前示例2014),并使查询条件(2014,2014 + 1)取决于此值。
我试图拆分上面的语法,然后在标准中连接,如下:
">=#" & "2014" & "-04-01# And <#" & "2015" & "-04-01#"
我收到错误“标准表达式中的数据类型不兼容”。
1。是否可以在查询条件中连接? 我也尝试过SQL CONCAT(string1,string2,string3,..),但无济于事。 如果这是可能的,那么我想我可以使用[Forms]![Form1]。[Textbox1]和([Forms]![Form1]。[Textbox1] + 1)来替换年份。 如果这是不可能的......
2。有没有更好的方法使查询条件动态化?
我尝试通过创建具有类似代码的模块来使the following solution工作:
Private m_varQueryParam As Variant
Public Function SetQueryParam(ByVal sValue as Variant)
m_varQueryParam = sValue
End Function
Public Function GetQueryParam() As Variant
GetQueryParam = m_varQueryParam
End Function
Query:
SELECT * FROM tblYourTable WHERE [FilterField] = GetQueryParam()
The VBA Code to launch the query will look like this.
SetQueryParam "your value here"
DoCmd.OpenQuery "qryYourQueryHere"
但我根本不明白如何让它发挥作用。
编辑:我创建了一个简单的访问数据库,试图让它工作。
SELECT tDateInfo.date, tDateInfo.info
FROM tDateInfo
WHERE (((tDateInfo.date)=GetQueryParam()));
这是表单的vba代码
Option Compare Database
Private Sub bSave_Click()
sValue = Me.TextBox1.Value
SetQueryParam (sValue)
End Sub
这是模块vba代码
Option Compare Database
Option Explicit
'is this necessary?
Private m_varQueryParam As Variant
Public Function SetQueryParam(ByVal sValue As Variant)
m_varQueryParam = sValue
End Function
Public Function GetQueryParam() As Variant
GetQueryParam = m_varQueryParam
End Function
查询条件为GetQueryParam()
感谢您的帮助。
答案 0 :(得分:0)
使用VBA处理参数和表单字段有点棘手。我创建了一个类似于你的简单表,如下所示: CREATE TABLE DateCalc( ID自动编号, FilterField DateTime )
以下代码将返回您想要的结果:
Sub testthis()
Dim db As dao.database
Set db = CurrentDb ' use current database
Dim qd As dao.QueryDef
Set qd = db.CreateQueryDef("") ' create anaonymous querydef
' the SQL statement correctly concatenates the parameter (Useyear) and mm/dd strings
qd.sql = "SELECT * FROM DateCalc WHERE [FilterField] >= [UseYear]" & "-04-01 And [FilterField] < [UseYear]+1" & "-04-01"
qd!UseYear = Forms!DateCalc!txtYear ' set the value of se year from the Form WHICH MUST BE OPEN
' AND the TetBox filled with the year you desire - 2014 for this example.
Dim rs As dao.recordSet
Set rs = qd.OpenRecordset
MsgBox "ID=" & rs(0) & ", Date=" & rs(1)
End Sub
新版本 抱歉,以下代码解析的第一个解决方案存在几个日期格式问题。此错误有很多其他原因,因此请确保FormName为“DateCalc”,TextBox名为“txtYear”。
您应该能够为所有查询概括以下代码(这些代码实际上有用吗?)。我现在传递TableName作为例子:
Sub DateFilter(TableName As String)
Dim db As dao.database
Set db = CurrentDb ' use current database
Dim qd As dao.QueryDef
Set qd = db.CreateQueryDef("") ' create anaonymous querydef
' the SQL statement correctly concatenates the parameter (Useyear) and mm/dd strings
Dim UseYear As Integer
UseYear = Forms!DateCalc!txtYear
Dim BegDate As Date
BegDate = CDate(UseYear & "-04-01")
Dim EndDate As Date
EndDate = CDate((UseYear + 1) & "-04-01")
qd.sql = "SELECT * FROM " & TableName & " WHERE [FilterField] >= [UseBegDate] And [FilterField] < [UseEndDate]"
qd!UseBegDate = BegDate
qd!UseEndDate = EndDate
Dim rs As dao.recordSet
Set rs = qd.OpenRecordset
Do While Not rs.EOF
MsgBox "ID=" & rs(0) & ", Date=" & rs(1)
rs.MoveNext
Loop
End Sub
答案 1 :(得分:0)
我想我找到了解决方案。
以下代码根据我的需要定义SQL,并更改Access查询的SQL。
它并不理想,因为它要求我为查询重写所有SQL,但它确实有效。
Sub ChangeQuery()
Dim Year, sqlTwo, StartDate, EndDate As String
Year = Me.txtYear.Value
StartDate = "4/1/" & Year
EndDate = "4/1/" & (Year + 1)
sqlTwo = "SELECT DateCalc.ID, DateCalc.FilterField FROM DateCalc WHERE (((DateCalc.FilterField)>=#" & StartDate & "# And DateCalc.FilterField<#" & EndDate & "#));"
tbTest.Value = sqlTwo
Dim oDB As Database
Dim oQuery As QueryDef
Set oDB = CurrentDb
Set oQuery = oDB.QueryDefs("Query1")
oQuery.SQL = sqlTwo
Set oQuery = Nothing
Set oDB = Nothing
End Sub
Private Sub Button_Click()
ChangeQuery
End Sub