vba循环遍历记录集中的字段,而另一个记录集不是EOF

时间:2015-10-12 09:30:49

标签: database vba ms-access access-vba ms-access-2010

我正在为访问数据库(Access 2010)编写一些代码,需要从表中提取非空字段到另一个表(tbl_TempProducts到tbl_BrandsStocked)。虽然这样做,我想要“复制和粘贴”的字段的行需要拆分成另一个表的各行。只要传输的值与我们传输的记录在同一记录中,tbl_TempProducts中记录的第一个值应该用作tbl_BrandsStocked中每个新记录的第一个值。我想在tbl_BrandsStocked中为tbl_TempProducts中的每个第7个字段创建一条新记录。

请参阅图表HERE

代码可以工作,但是将代码“粘贴”到目标表中的顺序是不正确的。

请原谅我,如果这还不够清楚,因为这是我的第一篇文章!

如果需要,我会发布更多信息.. :)

请参阅以下代码:

Private Sub btnTransfer_Click()

Dim dbs As DAO.Database
Dim temp As DAO.Recordset
Dim bStocked As DAO.Recordset

Dim fld As DAO.Field

Dim AutoID As String
Dim Product As String
Dim varProd As String
Dim PackSize As String
Dim priceType As String
Dim casesSold As String

Dim accountNumber As Integer
Dim firstRun As Boolean
Dim counter As Integer


Set dbs = CurrentDb
Set temp = dbs.OpenRecordset("SELECT * FROM tbl_TempProducts WHERE id IS NOT NULL")
Set bStocked = dbs.OpenRecordset("SELECT * FROM tbl_BrandsStocked")

counter = 0
firstRun = True

temp.MoveFirst

Do While temp.EOF = False


    For Each fld In temp.Fields

        If fld.Name <> "" Then

            If counter = 1 Then
                    AutoID = Nz(fld.value, "")

                If AutoID <> "" Then
                    AutoID = Nz(fld.value, "")
                    bStocked.AddNew
                    bStocked!AccountNo = AutoID
                    bStocked.upDate

                    If accountNumber <> AutoID Then
                        On Error Resume Next
                        accountNumber = AutoID
                    End If
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 2 Then
                Product = Nz(fld.value, "")

                If Product <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!Brand = Product
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 3 Then
                varProduct = Nz(fld.value, "")

                If varProduct <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!Variation = varProduct
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 4 Then
                PackSize = Nz(fld.value, "")

                If PackSize <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!PackSize = PackSize
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 5 Then
                priceType = Nz(fld.value, "")

                If priceType <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked![RRP-PMP] = priceType
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 6 Then
                casesSold = Nz(fld.value, "")

                If casesSold <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!CPW = casesSold
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If
            End If
        End If

    counter = counter + 1

            If counter >= 7 Then
                counter = 2
                bStocked.AddNew
                bStocked!AccountNo = accountNumber
                bStocked.upDate
            End If

    Next

    temp.MoveNext

    counter = 0
    firstRun = True

Loop

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM [tbl_BrandsStocked] WHERE [Brand] Is null"
DoCmd.SetWarnings True

Set dbs = Nothing
Set temp = Nothing
Set bStocked = Nothing
Set fld = Nothing

End Sub

2 个答案:

答案 0 :(得分:0)

除了发布样本数据外,您还应该查看代码:

  • 您从counter = 0开始,但没有此案例,因此第一个字段将始终被忽略。故意?
  • firstRun已设置但从未使用过
  • 而不是所有的bStocked.MoveLast, .Edit, .Update你应该在bStocked中有一个你写的当前记录。这将使您的代码更易读。

修改

我建议这样的结构:

strValue = Nz(fld.value, "")
If strValue <> "" Then
    Select Case counter
        Case 1: accountNumber = Val(strValue)   ' add error handling!
                bStocked.AddNew
                bStocked!AccountNo = accountNumber
        Case 2: bStocked!Brand = strValue       ' Product
        Case 3: bStocked!Variation = strValue   ' varProduct
        ' etc 4..6
    End Select
    counter = counter + 1

    If counter >= 7 Then
        bStocked.upDate     ' save new record

        bStocked.AddNew
        bStocked!AccountNo = accountNumber
        counter = 2
    End If
Else
    ' For an empty field you simply move to the next field
End If

Next fld
' save last record
bStocked.upDate 

答案 1 :(得分:0)

我现在已经解决了这个问题。我在目标表中获取不正确值的主要原因是因为不需要使用“accountNumber”变量。相反,我在循环遍历代码时使用“AutoID”变量值作为目标表上的第一个字段。

非常简单的修复,但不幸的是它确实花了我一段时间,因此发布的原因是因为我需要额外的一双眼睛!

工作代码:

Private Sub btnTransfer_Click()

Dim dbs As DAO.Database
Dim temp As DAO.Recordset
Dim bStocked As DAO.Recordset

Dim fld As DAO.Field

Dim AutoID As String
Dim Product As String
Dim varProd As String
Dim PackSize As String
Dim priceType As String
Dim casesSold As String

Dim accountNumber As Integer
Dim counter As Integer


Set dbs = CurrentDb
Set temp = dbs.OpenRecordset("SELECT * FROM tbl_TempProducts WHERE id IS NOT NULL")
Set bStocked = dbs.OpenRecordset("SELECT * FROM tbl_BrandsStocked")

counter = 0
firstRun = True
accountNumber = 0
AutoID = 0

temp.MoveFirst

Do While temp.EOF = False


    For Each fld In temp.Fields

        If fld.Name <> "" Then

            If counter = 1 Then
                    AutoID = Nz(fld.value, "")

                If AutoID <> "" Then
                    AutoID = Nz(fld.value, "")
                    bStocked.AddNew
                    bStocked!AccountNo = AutoID
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 2 Then
                Product = Nz(fld.value, "")

                If Product <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!Brand = Product
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 3 Then
                varProduct = Nz(fld.value, "")

                If varProduct <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!Variation = varProduct
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 4 Then
                PackSize = Nz(fld.value, "")

                If PackSize <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!PackSize = PackSize
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 5 Then
                priceType = Nz(fld.value, "")

                If priceType <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked![RRP-PMP] = priceType
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If

            ElseIf counter = 6 Then
                casesSold = Nz(fld.value, "")

                If casesSold <> "" Then
                    bStocked.MoveLast
                    bStocked.Edit
                    bStocked!CPW = casesSold
                    bStocked.upDate
                Else
                    counter = counter - 1
                End If
            End If
        End If

        counter = counter + 1

        If counter >= 7 Then
            counter = 2
            bStocked.AddNew
            bStocked!AccountNo = AutoID
            bStocked.upDate
        End If

    Next

    temp.MoveNext

    counter = 0

Loop

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM [tbl_BrandsStocked] WHERE [Brand] Is null"
DoCmd.SetWarnings True

Set dbs = Nothing
Set temp = Nothing
Set bStocked = Nothing
Set fld = Nothing

End Sub