简体   繁体   English

访问VBA简单SQL选择语句

[英]Access VBA simple sql select statement

Coming from years of web development where PHP and SQL statements were so simple, this recent task I've been required to undergo with MS Access and VBA is absolutely doing my head in at how much it complicates SQL statements. 来自多年的Web开发,因为PHP和SQL语句是如此简单,所以我最近必须要对MS Access和VBA进行的这项任务是使SQL语句变得多么复杂。 Mind you I have no prior knowledge about VBA so it could be extremely simple and I'm not just getting it, but all I want to do is 请注意,我对VBA没有任何先验知识,所以它可能非常简单,我不仅会了解它,而且我要做的就是

"SELECT type FROM tblMatter WHERE id='$id'"

When I wear my PHP cap, I want to think okay, we are going to have one row of data, that's going to be an array, and I want one object out of that array. 当我戴着PHP帽时,我想想可以,我们将有一行数据,这将是一个数组,并且我希望该数组中有一个对象。 Simple. 简单。

VBA, however, complicates the $#!t out of it. 但是,VBA使$#!t复杂化了。 So far my code looks something like this 到目前为止,我的代码看起来像这样

Dim matterSQL As String
Dim matterRS As Recordset

matterSQL = "SELECT type FROM tblMatter WHERE id'" & id & "'"
Set matterRS = CurrentDb.OpenRecordset(matterSQL)

MsgBox matterRS![type]

CurrentDb is defined much much earlier in the code to open the connection to the database, and the error is on the line containing OpenRecordset with the error: Data type mismatch in criteria expression . CurrentDb在代码中定义得更早,以打开与数据库的连接,并且该错误位于包含OpenRecordset的行上,错误为: 条件表达式中的数据类型不匹配
As I said, I'm new to VBA so I don't know what the heck I'm doing, and all the documentation on the internet is nowhere near helpful. 就像我说的那样,我是VBA的新手,所以我不知道自己在干什么,互联网上的所有文档都几乎无济于事。 But all I want to do is to get one piece of data from the table. 但是我要做的就是从表中获取一个数据。

Thanks in advance! 提前致谢!


Edit: I needed to build upon this with another query that takes the info from the last query to run. 编辑:我需要在另一个查询的基础上,使用另一个查询来运行上一个查询的信息。 Same kind of ordeal: 相同的磨难:

Dim costSQL As String
Dim costRS as Recordset

costSQL = "SELECT email FROM tblScaleOfDisb WHERE category=" & category
Set costRS = CurrentDb.OpenRecordset(costSQL)

MsgBox costRS![email]

This time I'm getting an error on the line containing OpenRecordset with the error: Too few parameters. 这次,我在包含OpenRecordset的行上遇到错误,错误为: 参数太少。 Expected 1. 预期1。
Which I don't understand because the code is practically the same as the first half of the question. 我不明白,因为代码实际上与问题的前半部分相同。 What have I done wrong? 我做错了什么?

You are missing = in the condition 您缺少=条件

try below 尝试下面

matterSQL = "SELECT type FROM tblMatter WHERE id='" & id & "'"

Also if id is numeric you don't need ' 另外,如果id是数字,则不需要'

matterSQL = "SELECT type FROM tblMatter WHERE id=" & id 

Too few parameters. 参数太少。 Expected 1. This happens when the field name in your sql query do not match the table field name if the field name are correct i believe the the datatype of category is not numeric then you have to use ' 预期的1。如果您的sql查询中的字段名称与表的字段名称不匹配(如果字段名称正确),则会发生这种情况,我认为类别的数据类型不是数字,那么您必须使用'

costSQL = "SELECT email FROM tblScaleOfDisb WHERE category='" & category &"'"

Always try to use parameterised query to avoid SQL injection 始终尝试使用参数化查询来避免SQL注入

You must understand or prepare few things before you start coding on a new platform. 在新平台上开始编码之前,您必须了解或准备一些东西。 such as

  1. Using Keywords/ Reserved keywords 使用关键字/保留关键字
  2. Capturing Errors 捕获错误
  3. basic arithmetic operations/ string operations. 基本算术运算/字符串运算。
  4. Available functions / methods 可用功能/方法
  5. Ways of cleaning your variables after using it 使用变量后清除变量的方法

in your case you also need to learn about MS ACCESS SQL. 在您的情况下,您还需要了解MS ACCESS SQL。 Which is pretty similar to standard SQL but (limited to and) strongly influenced by MS Access internal functions. 它与标准SQL非常相似,但受MS Access内部功能的影响(仅限于和)。

SQL execution will return n Rows as result. SQL执行将返回n行作为结果。 Each row will have n number of columns. 每行将有n列。 You need to understand how you need to loop through result sets. 您需要了解如何遍历结果集。

Please do have some error capturing method. 请提供一些错误捕获方法。 I will help you to understand the direction before spending hours in Google. 在帮助您在Google工作数小时之前,我将帮助您了解发展方向。

in your first SQL : you have a reserved keyword Type . 在第一个SQL中 :您有一个保留关键字Type use square brackets to escape reserved keywords. 使用方括号转义保留的关键字。 In where condition numeric fields must not have string quotes and strings must have them. 在这种情况下,数字字段不得包含字符串引号,而字符串必须包含引号。

Tip : You can use the MS Access visual query builder to build your query and copy the SQL to VBA. 提示 :可以使用MS Access可视查询构建器来构建查询并将SQL复制到VBA。

list of reserved keywords in MS ACCESS: https://support.microsoft.com/en-us/kb/286335 MS ACCESS中的保留关键字列表: https : //support.microsoft.com/en-us/kb/286335

list of functions: http://www.techonthenet.com/access/functions/ 功能列表: http : //www.techonthenet.com/access/functions/

Error handling : http://allenbrowne.com/ser-23a.html 错误处理: http//allenbrowne.com/ser-23a.html

Clean/close your objects after usage by explicitly setting as nothing: Is there a need to set Objects to Nothing inside VBA Functions 使用后通过显式设置为空来清除/关闭对象: 是否需要在VBA函数中将“对象”设置为“无”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM