如何引用vba select语句的结果? [英] How do I reference the result of a vba select statement?

查看:217
本文介绍了如何引用vba select语句的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这对大多数人来说都是一个荒谬的问题,但我只是学习ms访问并发现我需要为应用程序的主要部分编写vba代码。

我也知道可能有一种更简单的方法可以做到这一点但是一个人从错误中学得最好。所以请幽默我。


问题:如何引用用VBA编写的select语句的结果?


示例:TempTable是一个使用Select Into语句创建的单行表,例如3列:Col1,Col2,Col3。


Dim strCol2 as String

strCol2 =" ;从TempTable中选择Col2"

DoCmd.RunSQL" strCol2"


现在,如果这是正确的,我如何在VBA中引用结果。 ie:以编程方式。


提前感谢您的帮助,

JustAPawn

I know this has to be a ridiculous question for most, but I am just learning ms access and find I need to write vba code for the main part of the application.
I also know that there is probably an easier way to do this but a person learns best from their mistakes. So please humor me.

Question: How do I reference the result of a select statement written in VBA?

Example: TempTable is a single-row table created with a Select Into statement with, say, 3 columns: Col1,Col2,Col3.

Dim strCol2 as String
strCol2 = "Select Col2 From TempTable"
DoCmd.RunSQL "strCol2"

Now, if this is correct, how do I reference the result in VBA. ie:programmatically.

Thank you in advance for your assistance,
JustAPawn

推荐答案


我知道这对大多数人来说都是一个荒谬的问题,但我只是学习ms访问并发现我需要为应用程序的主要部分编写vba代码。

我也知道有一种更简单的方法可以做到这一点,但是一个人从错误中学得最好。所以请幽默我。


问题:如何引用用VBA编写的select语句的结果?


示例:TempTable是一个使用Select Into语句创建的单行表,例如3列:Col1,Col2,Col3。


Dim strCol2 as String

strCol2 =" ;从TempTable中选择Col2"

DoCmd.RunSQL" strCol2"


现在,如果这是正确的,我如何在VBA中引用结果。 ie:以编程方式。


提前感谢您的帮助,

JustAPawn
I know this has to be a ridiculous question for most, but I am just learning ms access and find I need to write vba code for the main part of the application.
I also know that there is probably an easier way to do this but a person learns best from their mistakes. So please humor me.

Question: How do I reference the result of a select statement written in VBA?

Example: TempTable is a single-row table created with a Select Into statement with, say, 3 columns: Col1,Col2,Col3.

Dim strCol2 as String
strCol2 = "Select Col2 From TempTable"
DoCmd.RunSQL "strCol2"

Now, if this is correct, how do I reference the result in VBA. ie:programmatically.

Thank you in advance for your assistance,
JustAPawn



哦,我认为你做得很好,干得好!


我可以为你发布更新查询链接,你可以把它变成你需要做的,怎么做' '那个!

http://www.thescripts.com/forum /thread743450.html


还会发送到Access论坛以获得VBA的额外支持...


稍微!


D?k?ll

Oh, I think you''re doing fine, justapawn!

I can post the link to an update query for you and you can turn it to what you need it to do, how ''bout that!

http://www.thescripts.com/forum/thread743450.html

Will also send over to Access Forum for added support in VBA...

In a bit!

D?k?ll


谢谢你,D?k?ll,感谢您的回复......以及您的鼓励。

自从我开始这个项目以来,我花了很多时间阅读各种论坛和帮助网站。我以前从未打开ms访问程序或经历过任何db编程,但我接受了雇主的项目,我不能接受失败。


我研究了你引用我的主题,我很抱歉,但我仍然无法理解如何引用或使用Select Query的结果。 ..更新,选择进入,删除我明白了。他们将数据更改为您想要的数据。但是选择似乎在黑暗的空间里。如果我引用文本字段或表单上的任何控件,我可以使用[Forms]![The NameOfTheForm]![TheNameOfTheObject]但引用表格中的字段/单元格与表单或其他控件对象分开 - 这就是我困惑了。你用一个查询选择它但是它去哪里了?

将strSTR作为字符串调整

strSTR = DoCmd.RunSQL"从TableName中选择Col2"


...不起作用。


Dim strSTR as String

strSTR ="选择Col2从TableName"

DoCmd.RunSQL" strSTR"


...运行Select Query但不告诉我字符串/数据是在那个领域/细胞中。我只是将结果用于If Then ElseIf条件,但我需要能够引用它来使用它。


如果[表格]! ..的工作原理为什么不strSTR = [表格]![NameOfTable]![标准]


如何将选择查询的结果分配给字符串?



对不起,如果我听起来很绝望。当我意识到使用ms访问真正编程时,我认为我的大脑的一部分被抢购了我需要同时学习三种语言(ms访问 - 应用程序的可视化基础 - 结构化查询语言......)都坐在另一个上面每个都有足够的相似之处,语法和方法真的混淆了。


再次,真诚地,感谢您的回复以及您给予的任何帮助或指示。

JustAPawn
Thank you, D?k?ll, for your reply.. and your encouragement.
I''ve spent many hours reading through various forums and help sites in the last 3 months since I''ve started this project. I had never even opened the ms access program or experienced any db programming previously, but I accepted the project from my employer and I can''t accept defeat.

I studied the thread you referred me to and, my apologies but I still cannot understand how to reference, or use, the result of the Select Query. ..Update, Select Into, Delete I understand. They change data to what you want. But Select seems to be out there in dark space. If I was referencing a text field or any control on a form I can use [Forms]![TheNameOfTheForm]![TheNameOfTheObject] but in referencing a field/cell in a table separate from a form or other control object - that is where I get confused. You select it with a query but then where does it go?

Dim strSTR as String
strSTR = DoCmd.RunSQL "Select Col2 From TableName"

... doesn''t work.

Dim strSTR as String
strSTR = "Select Col2 From TableName"
DoCmd.RunSQL "strSTR"

...runs the Select Query but doesn''t tell me the string/data that is in that field/cell. I am just using that result for an "If Then ElseIf" condition but I need to be able to reference it to use it.

If [Forms]! .. works why doesn''t strSTR = [Tables]![NameOfTable]![Criteria]

How do I assign the result of the select query to a string?


Sorry If I''m sounding desperate. I think part of my brain snapped when I realized that to really program using ms access I was required to learn three languages at the same time (ms access - visual basic for applications - structured query language..) all sitting on top of the other and each with enough similarities that the syntax and methods get really mixed up.

Again, sincerely, thank you for your reply and any help or direction you give.
JustAPawn


当我第一次开始我的工作落在我身上的大型项目时,我遇到了类似的问题。当你学习并试图弄清楚如何做一些应该相对简单的事情时,这很棘手。


如果我正确理解你的困境,你需要能够以编程方式返回SELECT查询的字符串结果。下面我使用了我目前使用的方法包含了一段示例代码。可能会有更快或更专业的方式来做到这一点,但我还没有学到它。

I''ve run into similar problems when I first started the massive project my work dropped on me. It''s tricky when you''re learning and trying to figure out how to do something that should be relatively simple.

If I''m understanding your plight correctly you need to be able to return the string results of a SELECT query programically. Below I have included a piece of sample code using a method that I currently use. There might be a quicker or more professional way to do this, but I haven''t learned it yet.

展开 | 选择 < span class =codeDivider> | Wrap | 行号


这篇关于如何引用vba select语句的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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