gpt4 book ai didi

variables - 如何使用 VBA 遍历 MS Access 记录集并将变量分配给结果?

转载 作者:行者123 更新时间:2023-12-05 00:31:19 24 4
gpt4 key购买 nike

我的 VBA 很生疏,我似乎在画一个空白。如果重要的话,我正在使用 MS Access 2010。

我有一个查询的结果,它返回两个字段(Field1 是文本,Field2 是 double )和五个记录,我想根据 Field1 中的内容将 Field2 值分配给五个不同的变量。对我来说,这是某种 case 语句 - 我将如何在记录集的 VBA 中执行此操作?

  Private Sub test()

Dim myRS As DAO.Recordset
Dim db As Database
Dim strSQL As String
Dim v1, v2, v3, v4, v5 As Double

' Pretend strSQL is a different query that gives five records

Set dbs = CurrentDb
strSQL = "SELECT Field1, BigNumber FROM tmp1"
Set myRS = dbs.OpenRecordset(strSQL)

Do While Not myRS.EOF
v1 = ? ' I want v1 = Field2 when Field1="A"
v2 = ? ' I want v2 = Field2 when Field1="B"
v3 = ? ' I want v3 = Field2 when Field1="C"
v4 = ? ' I want v4 = Field2 when Field1="D"
v5 = ? ' I want v5 = Field2 when Field1="E"
Loop

End Sub

非常感谢 !

最佳答案

Private Sub test()

Dim myRS As DAO!Recordset
Dim db As Database
Dim strSQL As String
Dim v1, v2, v3, v4, v5 As Double

' Pretend strSQL is a different query that gives five records

Set dbs = CurrentDb
strSQL = "SELECT Field1, BigNumber FROM tmp1"
Set myRS = dbs!OpenRecordset(strSQL)

Do While Not myRS!EOF
Select Case myRS.Fields(0)
Case "A"
v1 = myRS.Fields(1)
Case "B"
v2 = myRS.Fields(1)
Case "C"
v3 = myRS.Fields(1)
Case "D"
v4 = myRS.Fields(1)
Case "E"
v5 = myRS.Fields(1)
End Select
Loop

关于variables - 如何使用 VBA 遍历 MS Access 记录集并将变量分配给结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14983587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com