我的 VBA 生锈了,我似乎在画一个空白。如果重要,我正在使用 MS Access 2010。
我有一个查询的结果,它返回两个字段(Field1 是文本,Field2 是双精度)和五个记录,我想根据 Field1 中的内容将 Field2 值分配给五个不同的变量。对我来说,这是某种案例陈述——我将如何在记录集上的 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
非常感谢 !