如何在尝试在Access数据库中创建搜索查询时修复运行时错误 [英] How do I fix a runtime error when trying to create a search query in access database

查看:171
本文介绍了如何在尝试在Access数据库中创建搜索查询时修复运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub Command8_Click()

将SQL作为字符串调暗



SQL =SELECT tblVisit.PatientID,tblPatients.Firstname,tblPatients。姓氏_

& DateDiff('yyyy',[tblPatients]![生日],现在())AS年龄,tblVisit。[日期访问]_

& tblVisit.Diagnosis_

& [名字]&''& [姓氏] AS [处方者姓名] FROM tblPatients INNER JOIN(tblDoctor INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.DoctorID)ON tblPatients.PatientID = tblVisit.PatientID_

& WHERE [名字] ='& Me.txtKeywords& '



Me.subClientList.Form.RecordSource = SQL

Me.subClientList.Form.Requery



结束子





错误是运行时错误'3075'
$ b查询表达式'tvist'中的$ b语法错误(缺少运算符)。[DateVisit] tblVist.Diagnosis [First Name]& ''[姓]'



我的尝试:



在查询处方者姓名和创建日期div时,我从双引号更改了

解决方案

查看您生成的字符串:

< pre lang =sql> SELECT tblVisit.PatientID,tblPatients.Firstname,tblPatients.LastnameDateDiff(' < span class =code-string> yyyy',[tblPatients]![Birthday],Now()) AS 年龄,tblVisit。[日期访问] tblVisit.Diagnosis [名字]& ' '& [姓氏] AS [Prescriber Names] FROM tblPatients INNER JOIN (tblDoctor INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.Do
ctorID) ON tblPatients.PatientID = tblVisit .PatientIDWHERE [名字] = ' 关键字'

这看起来像是有效的SQL吗给你?



你需要坐下来找出你想要发送给SQL的查询,然后填写空白以获取你想要的特定数据。你拥有的是无效的,我不知道你想要做什么!



添加到:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood ' < span class =code-string>  

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?


已经发布在尝试创建一个搜索查询,该搜索查询将在访问数据库中搜索结果。它提出错误运行时错误'3075' - Visual Basic讨论板 [ ^ ]。请不要重新发布。


 SQL =   SELECT tblVisit.PatientID,tblPatients.Firstname,tblPatients.Lastname _ 
& DateDiff('yyyy',[tblPatients]![Birthday],Now())AS Age,tblVisit。[Date访问] _
& tblVisit.Diagnosis _
& [名字]&''& [姓氏] AS [Prescriber Names] FROM tblPatients INNER JOIN(tblDoctor) INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.DoctorID)ON tblPatients.PatientID = tblVisit.PatientID _
& WHERE [Firstname] ='& .txtKeywords& '



永远不要构建SQL通过连接字符串进行查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


Private Sub Command8_Click()
Dim SQL As String

SQL = "SELECT tblVisit.PatientID, tblPatients.Firstname, tblPatients.Lastname" _
& "DateDiff('yyyy',[tblPatients]![Birthday],Now()) AS Age, tblVisit.[Date Visit]" _
& "tblVisit.Diagnosis" _
& "[First Name] & ' ' & [Last Name] AS [Prescriber Names] FROM tblPatients INNER JOIN (tblDoctor INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.DoctorID) ON tblPatients.PatientID = tblVisit.PatientID" _
& "WHERE [Firstname] = '" & Me.txtKeywords & "' "

Me.subClientList.Form.RecordSource = SQL
Me.subClientList.Form.Requery

End Sub
?

The error is runtime error '3075'
syntax error (missing operator) in query expression 'tvist'.[DateVisit]tblVist.Diagnosis[First Name]& ' ' [Last Name]'

What I have tried:

I changed from double quotes when querying the prescribers name and when creating the date div

解决方案

Look at the string you generate:

SELECT tblVisit.PatientID, tblPatients.Firstname, tblPatients.LastnameDateDiff('yyyy',[tblPatients]![Birthday],Now()) AS Age, tblVisit.[Date Visit]tblVisit.Diagnosis[First Name] & ' ' & [Last Name] AS [Prescriber Names] FROM tblPatients INNER JOIN (tblDoctor INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.Do
ctorID) ON tblPatients.PatientID = tblVisit.PatientIDWHERE [Firstname] = 'Keyword'

Does that look like valid SQL to you?

You need to sit down and work out what query you are trying to send to SQL, then "fill in the blanks" for the specific data you want. What you have isn't valid and I have no idea what you are trying to do!

Add to that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


Already posted at Trying to create a search query that will bring the results searched in access database. It is bringing up an error runtime error '3075' - Visual Basic Discussion Boards[^]. Please do not repost.


SQL = "SELECT tblVisit.PatientID, tblPatients.Firstname, tblPatients.Lastname" _
& "DateDiff('yyyy',[tblPatients]![Birthday],Now()) AS Age, tblVisit.[Date Visit]" _
& "tblVisit.Diagnosis" _
& "[First Name] & ' ' & [Last Name] AS [Prescriber Names] FROM tblPatients INNER JOIN (tblDoctor INNER JOIN tblVisit ON tblDoctor.DoctorID = tblVisit.DoctorID) ON tblPatients.PatientID = tblVisit.PatientID" _
& "WHERE [Firstname] = '" & Me.txtKeywords & "' "


Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


这篇关于如何在尝试在Access数据库中创建搜索查询时修复运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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