通过 VBA 从记录集中添加值 + 从输入框添加值

Add Value From Recordset + Value From Input Box Through VBA

我有一个输入框,用户可以在其中输入值。我想获取用户输入的值并将其添加到从记录集中读取的值。我已经尝试过这种语法,这对我来说并没有引发错误,但问题是,该值没有被添加到记录集中的值中。我应该更改什么才能进行简单的加法计算?

Public Function AddToValue()
Dim value2 As Variant
Dim ExportRecordSet As DAO.Recordset
Dim excelWS As Object
Dim xl As Object
Dim wb As Object
Dim TemplateWB As String
Dim row As Integer

value2 = InputBox("Enter Value To Add:", "VBA InputBox Function")

Set xl = CreateObject("Excel.Application")
TemplateWB = "C:\Test\Testwb.xlsx"
Set wb = xl.Workbooks.Add
Set excelWS = wb.Worksheets(1)
excelWS.Name = "AddedFromCode"
xl.Application.Visible = True

Set ExportRecordSet = db.OpenRecordset(" Select Total FROM localtesttable;")

If Not ExportRecordSet.EOF Then
While Not ExportRecordSet.EOF

    excelWS.Cells(row, 5).Value = ExportRecordSet("Total")+value2

Wend
End If

wb.SaveAs 

wb.Close

End Function

编辑
根据@KenWhite 的评论 - 我将明确说明我想在上面的语法中实现什么:

1) Get userinput for value2
2) read the value from the field Total
3) write to Excel the value from the field total + value2

编辑 #2
Link 我看到同时使用 If Not EOFWhile Not EOF

Looping DAO Recordsets

您需要推进行和记录集:

Public Function AddToValue()

    Dim ExportRecordSet As DAO.Recordset
    Dim excelWS As Object
    Dim xl As Object
    Dim wb As Object
    Dim TemplateWB As String
    Dim row As Integer
    Dim value2 As String

    value2 = InputBox("Enter Value To Add:", "VBA InputBox Function")

    Set xl = CreateObject("Excel.Application")
    TemplateWB = "C:\Test\Testwb.xlsx"
    Set wb = xl.Workbooks.Add
    Set excelWS = wb.Worksheets(1)
    excelWS.Name = "AddedFromCode"
    xl.Application.Visible = True

    Set ExportRecordSet = db.OpenRecordset("Select Total FROM localtesttable;")
    ' Set start row.
    row = 1
    While Not ExportRecordSet.EOF        
        excelWS.Cells(row, 5).Value = ExportRecordSet("Total") + Val(value2)
        row = row + 1
        ExportRecordset.MoveNext
    Wend

    wb.SaveAs 
    wb.Close

End Function