对于MS-Access中的Next循环

时间:2014-04-08 15:02:27

标签: vba ms-access append attachment ms-access-2013

我在accessforums上寻求帮助,并获得了代码:

Public Sub InsertData()
Dim dbs As dao.Database, rst As Recordset, rstInsert As Recordset
Dim sSQL As String
Dim sSQLSource As String
Dim i

Set dbs = CurrentDb

sSQLSource = "SELECT Route, Account, [Date], "
sSQLSource = sSQLSource & "[Door 1].FileData, [Door 1].FileName, [Door 1].FileType, "
sSQLSource = sSQLSource & "[Door 2].FileData, [Door 2].FileName, [Door 2].FileType, "
sSQLSource = sSQLSource & "[Door 3].FileData, [Door 3].FileName, [Door 3].FileType, "
sSQLSource = sSQLSource & "[Door 4].FileData, [Door 4].FileName, [Door 4].FileType, "
sSQLSource = sSQLSource & "[Door 5].FileData, [Door 5].FileName, [Door 5].FileType, "
sSQLSource = sSQLSource & "[Door 6].FileData, [Door 6].FileName, [Door 6].FileType, "
sSQLSource = sSQLSource & "[Door 7].FileData, [Door 7].FileName, [Door 7].FileType, "
sSQLSource = sSQLSource & "[Door 8].FileData, [Door 8].FileName, [Door 8].FileType, "
sSQLSource = sSQLSource & "[Door 9].FileData, [Door 9].FileName, [Door 9].FileType, "
sSQLSource = sSQLSource & "[Door 10].FileData, [Door 10].FileName, [Door 10].FileType, "
sSQLSource = sSQLSource & "[Door 11].FileData, [Door 11].FileName, [Door 11].FileType, "
sSQLSource = sSQLSource & "[Door 12].FileData, [Door 12].FileName, [Door 12].FileType, "
sSQLSource = sSQLSource & "[Door 13].FileData, [Door 13].FileName, [Door 13].FileType, "
sSQLSource = sSQLSource & "[Door 14].FileData, [Door 14].FileName, [Door 14].FileType, "
sSQLSource = sSQLSource & "[Door 15].FileData, [Door 15].FileName, [Door 15].FileType, "
sSQLSource = sSQLSource & "Comments "
sSQLSource = sSQLSource & "FROM Submit"

Set rst = dbs.OpenRecordset(sSQLSource)

If rst.RecordCount <> 0 Then
    Do While rst.EOF <> True
        Debug.Print rst!route
        If DCount("*", "imgdest", "[Route] = " & rst!route) = 0 Then
            Debug.Print "    NO EXISTING RECORD"
            Debug.Print "    " & rst![door 1.filename]
            If Not IsNull(rst![door 1.filename]) Then
                Debug.Print "    Image in 1"
                Set rstInsert = dbs.OpenRecordset("SELECT Route, Account, [Date], [Door 1].FileData, [Door 1].FileName, [Door 1].FileType, Comments FROM imgDest")
                rstInsert.AddNew
                rstInsert![route] = rst![route]
                rstInsert![account] = rst![account]
                rstInsert![Date] = rst![Date]
                rstInsert![Door 1.Filedata] = rst![Door 1.Filedata]
                rstInsert![door 1.filename] = rst![door 1.filename]
                rstInsert![comments] = rst![comments]
                rstInsert.Update
                rstInsert.Close
            End If
            'REPEAT THE CODE BELOW FOR EACH OF YOUR POSSIBLE IMAGE FIELDS
            If Not IsNull(rst![door 2.filename]) Then
                Debug.Print "    Image in 2"
                Set rstInsert = dbs.OpenRecordset("SELECT Route, [Door 2].FileData, [Door 2].FileName, [Door 2].FileType FROM imgDest WHERE [route] = " & rst!route)
                rstInsert.Edit
                rstInsert![Door 2.Filedata] = rst![Door 2.Filedata]
                rstInsert![door 2.filename] = rst![door 2.filename]
                rstInsert.Update
                rstInsert.Close
            End If
            If Not IsNull(rst![door 3.filename]) Then
                Debug.Print "    Image in 3"
                Set rstInsert = dbs.OpenRecordset("SELECT Route, [Door 3].FileData, [Door 3].FileName, [Door 3].FileType FROM imgDest WHERE [route] = " & rst!route)
                rstInsert.Edit
                rstInsert![Door 3.Filedata] = rst![Door 3.Filedata]
                rstInsert![door 3.filename] = rst![door 3.filename]
                rstInsert.Update
                rstInsert.Close
            End If

            Next i
        End If
        rst.MoveNext
    Loop
Else
    MsgBox "NO records to process"
End If
rst.Close
Set dbs = Nothing
End Sub

但是,每次我尝试运行此代码时,我都会给出错误&#34; Next没有For&#34;。据我了解,Next i系列将运行下一个&#34;门&#34;附加。但是,我是VB的新手,我不确定&#34; For&#34;命令使用和输入位置。我非常感谢这方面的一些帮助。

1 个答案:

答案 0 :(得分:0)

你可以而且真的应该把它干掉一点。它将极大地帮助您的代码和调试。我尽量不提供代码示例,我将无法完全测试而无法访问您的数据库我显然无法做到这一点,但这里有一个概念供您扩展

Dim numberOfDoors As Integer
Dim sSQLSource AS String

numberOfDoors = 15

sSQLSource = "SELECT Route, Account, [Date], "

For i = 1 To numberOfDoors
    sSQLSource = sSQLSource & buildSelect(i)
Next i

sSQLSource = sSQLSource & "Comments "
sSQLSource = sSQLSource & "FROM Submit"

Function buildSelect(doorNumber As Integer) As String
    Dim door AS String
    door = "Door " & doorNumber
    buildSelect = "[" & door & "].FileDate, [" & door & "].FileName, [" & door & "].FileType, "
End Function

您的SQL语句将看起来相同,代码将更具可读性。

类似的概念可以应用于您的更新查询。

AS @simoco表示Next i目前正在为您造成问题,i甚至没有在提供的代码中使用。我认为更新查询的循环是正确的方向,只需稍微模块化代码,这样您和未来的人就可以阅读并理解正在发生的事情