vb通过变量引用列
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你问题的第一部分,我认为你应该尝试这个:
我不知道你问题的第二部分。
编辑:您的评论让我认为您的 numTime 文本值被 rst.Fields 解释为数字而不是字符串值。然而,这里的情况似乎并非如此。
打开基于相同表和字段名称的记录集,此代码抛出错误(“在此集合中找不到项目”):
但是此代码打印名为“2”的列的值:
所以我仍然不明白为什么它不适合你。但我从未使用数字作为列名;我觉得这似乎是错误的。如果您将列重命名为 MatchID、f1、f2、f3 而这种方法仍然不起作用,我会感到更惊讶。
If I understand the first part of your question correctly, I think you should try this:
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"):
But this code prints the value of the column named "2":
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.