简体   繁体   English

点击事件访问表单创建传递查询

[英]access form on click event create pass through query

I have an access form with a list of numbers [1, 2, 3, 4, 5]. 我有一张带数字[1、2、3、4、5]列表的访问表。 When a number is clicked on, i want to run a pass through query that opens a datasheet view of the query. 单击一个数字时,我要运行一个直通查询,以打开查询的数据表视图。 So for example, say [3] is clicked, then a pass through query would run SQL like 例如,假设单击了[3],则直通查询将运行SQL,例如

select * from mytable where number=3  

Do I have to use VBA for this? 我必须为此使用VBA吗? How is it done in VBA? 如何在VBA中完成? I have the log in information for the SQL server, but no permissions other than querying tables. 我具有SQL Server的登录信息,但是除了查询表外没有其他权限。

You can send SQL queries to your SQL Server databases via ADO. 您可以通过ADO将SQL查询发送到SQL Server数据库。

More on ADO: http://support.microsoft.com/kb/257819 有关ADO的更多信息: http : //support.microsoft.com/kb/257819

See Also: Connecting to SQL Server over network using ADO from Excel VBA 另请参见: 使用Excel VBA中的ADO通过网络连接到SQL Server

The easiest way to achieve what you want to do is to create and save a Query in your database which you can call DoCmd.OpenQuery on. 实现您想要的操作的最简单方法是在数据库中创建并保存一个Query,您可以将其称为DoCmd.OpenQuery。

The most efficient way to pull the data would be a pass-through query which has the predicate (WHERE element) set already (and all the connection properties as well). 提取数据的最有效方法是传递查询,该查询已设置谓词(WHERE元素)(以及所有连接属性)。 The challenge here is to pass the parameter (1-5) through to the saved pass-through query in a way you can call DoCmd.OpenQuery on it. 这里的挑战是以一种可以调用DoCmd.OpenQuery的方式将参数(1-5)传递给保存的传递查询。

A way to do this would be to on the AfterUpdate event of the updated control that supplies 1-5, create a pass-through QueryDef (see here: https://support.microsoft.com/kb/112108 ) using dynamic SQL, making sure the WHERE clause represents the number you want to filter by. 一种方法是在提供1-5的更新控件的AfterUpdate事件上,使用动态SQL创建直通QueryDef(请参见此处: https//support.microsoft.com/kb/112108 ),确保WHERE子句代表您要过滤的数字。 You'll need to set the Connect property of the new QueryDef object to have the connection string to your external db. 您需要设置新QueryDef对象的Connect属性,以将连接字符串连接到您的外部数据库。 Save the QueryDef you create, then call it by name with DoCmd.OpenQuery. 保存您创建的QueryDef,然后使用DoCmd.OpenQuery按名称进行调用。 This should provide you with the functionality you are looking for. 这应该为您提供所需的功能。

Something you actually need to do before you create the pass-through QueryDef is check for its existence and delete it if it already exists (or update its properties, I guess) or you will get an error. 创建直通QueryDef之前,您实际上需要执行的操作是检查其是否存在,如果它已经存在(或更新其属性,我想),则将其删除,否则会出现错误。

Use a saved pass-though query and DAO. 使用保存的传递查询和DAO。

You can do this for a total of 2 lines of code. 您总共可以执行2行代码。

This code will work: 该代码将起作用:

CurrentDb.QueryDefs("MyPass").SQL = "select * from mytable where number = " & Me.Number
DoCmd.OpenQuery "MyPass"

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

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