首页 \ 问答 \ 动态查询标准(Dynamic Query Criteria)

动态查询标准(Dynamic Query Criteria)

我正在尝试使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.有更好的方法使查询条件动态化吗?

我尝试通过创建具有类似代码的模块来使以下解决方案工作:

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"

但我根本不明白如何让这个工作。

编辑:我创建了一个简单的访问数据库,试图让它工作。

  • Textbox1,默认值= Date()
  • bSave,按钮
  • tDateInfo,表:日期(日期/时间),信息(文本)以及随机日期和信息。
  • 查询1:

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()

感谢您的帮助。


I'm trying to make a Microsoft Access query depend on a value in another form's textbox.

This is the criteria, as it is now. Basically, any date between April 1st 2014, and March 31st 2015. This works well.

>=#2014-04-01# And <#2015-04-01#

I'd like to have a textbox with the year (with the current example 2014), and make the query criteria (2014, 2014+1) depend on this value.

I've tried to split the above syntax, then concatenate in the criteria, as such:

">=#" & "2014" & "-04-01# And <#" & "2015" & "-04-01#"

And I get an error "Data types in the criterion expression are incompatible".

1. Is it possible to concatenate in the query criteria? I have also tried the SQL CONCAT(string1,string2,string3,..), to no avail. If this is possible, then I guess I can use [Forms]![Form1].[Textbox1] and ([Forms]![Form1].[Textbox1] + 1) to replace the years. If this is not possible...

2. Is there a better way to make the query criteria dynamic?

I tried to make the following solution work by creating a module with similar code:

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"

But I simply do not understand how to get this to work.

EDIT: I created a simple access database, to try to get this to work.

  • Textbox1, default value =Date()
  • bSave, button
  • tDateInfo, table: date (date/time), info (text) with random dates and info.
  • Query1:

SELECT tDateInfo.date, tDateInfo.info FROM tDateInfo WHERE (((tDateInfo.date)=GetQueryParam()));

Here's the form's vba code

Option Compare Database

Private Sub bSave_Click()
sValue = Me.TextBox1.Value
SetQueryParam (sValue)
End Sub

Here's the modules vba code

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

And the query criteria is GetQueryParam()

Thank you for your help.


原文:https://stackoverflow.com/questions/23934581
更新时间:2023-04-06 22:04

最满意答案

您可以使用格式化功能,例如:

DT::datatable(table.Drawdowns(rets)) %>% formatDate(c(1,2,3), method = 'toDateString')

得到: 在此处输入图像描述


You can use a formatting function, for example:

DT::datatable(table.Drawdowns(rets)) %>% formatDate(c(1,2,3), method = 'toDateString')

Gives: enter image description here

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。