VB6 将数据分配给数据库中的变量

VB6 Assigning data to variables from a database

我被要求对 VB6 项目进行更改。我遇到的问题是我试图从 Access 数据库中获取一些数据并将数据分配给一些变量。

我有代码:

Dta_Period.DatabaseName = DB_Accounts_Name$
Dta_Period.RecordSet = "SELECT * FROM [Period]"
Dta_Period.Refresh

table Period 包含 2 个字段。 sMonthPeriod sMonth 字段包含月份 January - December。 Period 字段存储一个 0 到 11 之间的数字,表示在客户财政年度的哪个月份分配了哪个数字。一月可能是0,也可能是11,本质上。

我需要知道哪个月哪个时期,这就是为什么我从数据库中选择了这个数据。但是,我不知道下一步该怎么做。

如何遍历 RecordSet(如果可能的话?)并找出每个月分配的号码?

我认为没有办法使用 Do Until 循环。只使用 12 个单独的查询,然后创建一个字符串数组和一个整数数组,然后遍历字符串数组直到找到正确的月份,对整数数组使用相同的索引是否更容易?

编辑 1

为了让我自己和任何试图提供答案的人都更容易理解,我修改了代码。

Dim rstPeriod As DAO.RecordSet
Dim accDB As DAO.Database

' DB_Session is a Workspace, whilst DB_Accounts_Name$ is the name of the DB I am using
Set accDB = DB_Session.OpenDatabase(DB_Accounts_Name$)

SQL = "SELECT * FROM [Period] ORDER BY [Period]"

Set rstPeriod = accDB.OpenRecordset(SQL, dbOpenDynaset)

If rstPeriod.BOF = False Then
   rstPeriod.MoveFirst
End If

Dim strMonth(11) As String
Dim pNumber(11) As Integer

伪代码思路:

Do Until rstPeriod.EOF
   Select Case currentRow.Field("Month")
     Case "January"
       strMonth(0) = "January"
       pNumber(0) = currentRow.Field("Number")
     Case "February"
       strMonth(1) = "February"
       pNumber(1) = currentRow.Field("Number")
    End Select
Loop

遍历记录集并用月份名称和月份编号填充数组。

这假定记录集 returns 不超过 12 条记录。

Public Sub LoopThroughtRecordset()
    On Error GoTo ErrorTrap

    Dim rs As DAO.Recordset
    Set rs = CurrentDb().OpenRecordset("SELECT * FROM [Period] ORDER BY [Period]", dbOpenSnapShot)
    With rs
        If .EOF Then GoTo Leave
        .MoveLast
        .MoveFirst
    End With

    Dim strMonth(11) As String
    Dim pNumber(11) As Integer

    Dim idx As Long
    For idx = 0 To rs.RecordCount -1
        strMonth(idx) = rs![Month]
        pNumber(idx) = rs![Number]
        rs.MoveNext
    Next idx

Leave:
    On Error Resume Next
        rs.Close
    Set rs = Nothing
    On Error GoTo 0
    Exit Sub

ErrorTrap:
    MsgBox Err.Description, vbCritical, CurrentDb.Properties("AppTitle")
    Resume Leave
End Sub

'strMonth(0) = January
'strMonth(1) = February
'...
'pNumber(0) = 1
'pNumber(1) = 2
'...