Access 2007,在VBA中使用Recordset和Sql语句 [英] Access 2007, Using Recordset and Sql statements in VBA

查看:406
本文介绍了Access 2007,在VBA中使用Recordset和Sql语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这段代码能正常工作,我已经尝试了所有可能的组合,最后在此处发布.这些代码应该要做的就是使用3个输入参数从表中获取一行.问题出在使用Sql语句中的日期时,它可以正常工作,但是如果我使用变量"nam"并相应地更改条件条件,它就可以工作.

非常感谢您的帮助.

I want this code to work, I have tried all possible combinations and finally posting here. All this code is supposed to do is to get a row from the table using 3 input parameters. The problem comes when using the date in Sql statement, it dosent work but if I use a variable "nam" and change the condition criteria accordingly it works.

Your help will be greatly appreciated.

Dim dbs1 As DAO.Database
Dim rst1 As DAO.Recordset
Dim strSQL1 As String
Dim temp As Date
nam = "DeEx"
 temp = [Forms]![AllProjects1]![Combo3].Value
 temp = Format(temp, "dd-mm-yyyy")
Set dbs1 = CurrentDb
'strSQL1 = "SELECT * FROM AllProjects WHERE Team= '" & nam & "'" & " AND Team= '" & nam & "'"
strSQL1 = "SELECT * FROM AllProjects " & "WHERE [StartDate]=" & Format(temp, "dd.mm.yyyy")
Set rst1 = dbs1.OpenRecordset(strSQL1)

推荐答案

首先,请阅读以下内容文章 [ozgrid论坛上的线程 [
First of all, read this article[^] and thread on ozgrid forum[^] and all related articles.

To get data from table, use the following query:
SELECT *
FROM AllProjects
WHERE [StartDate]= #09/18/2012#



我建议您使用MsgBox来构建查询并在运行时检查其内容,例如:



I recommend you to build a query and check its content in the run-time using MsgBox, for example:

strSQL1 = "SELECT * FROM AllProjects " & "WHERE [StartDate]=#" & Format(temp, "dd.mm.yyyy") & "#"
MsgBox strSQL1


您现在看到的是什么?


What you see now?


这篇关于Access 2007,在VBA中使用Recordset和Sql语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆