将整数值设置为变量时的应用程序定义或对象定义错误

时间:2017-11-02 19:06:11

标签: vba ms-access parameters integer ms-access-2007

问题:

我认为我根据用户的c参数输入设置变量Month的值的方式导致Application-defiend或对象定义的错误。因为当我设置{c = 1的c值时,程序可以正常工作。如何修复条件语句?

目标:

c=11 c是存储列号的那个,其中c = 1表示列A,11表示列K.我想根据用户的'MONTH'参数输入来设置c的值“BeginDate”= 8/1/2017

  • ex)如果1(1月)c = 4
    8(8月)c = 11

问题:

Dim c As Integer
Dim num As Integer
Dim ret(1 To num) As Integer

For num = 1 To num
    ret(num) = Month(num) + 3
Next
c = ret(num)

xlWS.Cells(xlRow, c).Formula = acRng上给出了应用程序定义或对象定义的错误 完整代码:

 Option Compare Database
 Option Explicit
  Public Function pa()

    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    Dim xlWS As Excel.Worksheet
    Dim acRng As Variant
    Dim xlRow As Integer

    Dim db As DAO.Database
    Dim qry As QueryDef
    Dim rst As Recordset
    Dim prm As DAO.Parameter
    Dim strSQL As String

    Set db = CurrentDb
    Set xlApp = New Excel.Application
    Set xlWB = xlApp.Workbooks.Open("C:\Users\J\Desktop\August 2017.xlsx")
    Set xlWS = xlWB.Worksheets("Photronics")

    xlRow = (xlWS.Columns("K").End(xlDown).Row)

    Set qry = db.QueryDefs("15_Photronic-Allen")

    qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
    qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate]

    Set rst = qry.OpenRecordset(dbOpenDynaset)

    Dim c As Integer
    Dim num As Integer
    Dim ret(1 To 15) As Integer

    For num = 1 To num
        ret(num) = Month(num) + 3
    Next
    c = ret(num)    
    xlRow = xlRow + 2

     Do Until rst.EOF
        For Each acRng In rst.Fields
            xlWS.Cells(xlRow, c).Formula = acRng
            c = c + 1
        Next acRng
        xlRow = xlRow + 1
        c = 1
        rst.MoveNext
        If xlRow > 25 Then GoTo rq_Exit
    Loop


rq_Exit:
    rst.Close
    Set rst = Nothing
    Set xlWS = Nothing
    xlWB.Close acSaveYes
    Set xlWB = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    Exit Function

End Function

0 个答案:

没有答案