vb通过变量引用列

发布于 2024-11-03 05:18:19 字数 742 浏览 1 评论 0原文

我正在尝试使用 vb 脚本更新 Access 数据库,方法是将足球得分线放入取决于进球时间的列中。

所以我有一个表 tblScoreLinesByMinute,其中包含

matchID 1 2 3 4.... 89 90

我在脚本中设置的

Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblScoreLinesByMinute", dbOpenTable)

列让我们假设主队在第 3 分钟进球 我使用变量 numTime = 3 捕获了这一点,并创建了变量 strScoreline = 10。 我希望更新类似的表

rst![numTime] = strScoreline

,但如果我尝试此操作或 CStr(numTime) 我会收到运行时错误“在此集合中找不到项目”。硬编码的

rst![3] = strScoreline 确实有效,

我也热衷于将其他列设置为相关的得分线。假设客队在第88分钟扳平比分。我所追求的是让这个特定比赛的行显示“00”的第 1,2 列、第 3 至 87 列(包括“10”)和第 88-90 列“11”的得分线,

如果有的话,我也可以使用 mssql一个更简单的方法

I am trying to update an Access database with a vb script by putting a soccer scoreline into a column dependent on when a goal is scored.

So I have a table, tblScoreLinesByMinute, with columns

matchID 1 2 3 4.... 89 90

which in my script I have set

Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblScoreLinesByMinute", dbOpenTable)

Let us say there was a goal scored by the home team in the 3rd minute
I have captured this with a variable numTime = 3 and created a variable strScoreline = 10.
I wish to update the table something like

rst![numTime] = strScoreline

but if I try this or CStr(numTime) I get a runtime error "Item not found in this collection". A hard coded

rst![3] = strScoreline does work

I am also keen to set the other columns to the relevant scoreline. Let us say that the away team equalized in the 88th minute. What I am after is having the row for this particular match showing a scoreline for columns 1,2 of '00', columns 3 to 87 inc '10' and columns 88-90 '11'

I could use mssql as well if there is a simpler method for that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-10 05:18:19

如果我正确理解你问题的第一部分,我认为你应该尝试这个:

rst.Fields(numTime) = strScoreline

我不知道你问题的第二部分。

编辑:您的评论让我认为您的 numTime 文本值被 rst.Fields 解释为数字而不是字符串值。然而,这里的情况似乎并非如此。

打开基于相同表和字段名称的记录集,此代码抛出错误(“在此集合中找不到项目”):

numTime = "0"
Debug.Print rst.Fields(numTime)

但是此代码打印名为“2”的列的值:

numTime = "2"
Debug.Print rst.Fields(numTime)

所以我仍然不明白为什么它不适合你。但我从未使用数字作为列名;我觉得这似乎是错误的。如果您将列重命名为 MatchID、f1、f2、f3 而这种方法仍然不起作用,我会感到更惊讶。

If I understand the first part of your question correctly, I think you should try this:

rst.Fields(numTime) = strScoreline

I have no clue about the second part of your question.

Edit: Your comment made me think your numTime text values were being interpreted as numbers rather than string values by rst.Fields. However, that doesn't appear to be the case here.

Opening a recordset based on the same table and field names, this code throws an error ("Item not found in this collection"):

numTime = "0"
Debug.Print rst.Fields(numTime)

But this code prints the value of the column named "2":

numTime = "2"
Debug.Print rst.Fields(numTime)

So I still don't understand why it's not working for you. But I've never used numbers as column names; it just seems wrong to me. I would be more surprised if you renamed your columns as MatchID, f1, f2, f3 and this approach still doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文