Ms access 其他的 sql=sql&“'”和rstRecordSet.Fields(j)&“,” 如果结束 下一个 sql=Left(sql,Len(sql)-1“删除最后一个” sql=sql&;” CurrentDb.executesql rstRecordSet.MoveNext 循环直到rstRecordSet.EOF=True 如果结束 混合物,关闭 设置连接=无 Set cmdCommand=Nothing Set rstRecordSet=Nothing 出口接头 错误处理: 对于混合连接中的每个错误。错误 strError=“Error#”&错误编号&vbCr&_ “”&Err.Description&vbCr&_ “(来源:&&Err.Source&”)和vbCr&_ “(SQL状态:”&Err.SQLState&“”&vbCr&_ “(NativeError:&Err.NativeError&”)和vbCr 如果Err.HelpFile=“”,则 strError=strError&“没有可用的帮助文件” 其他的 strError=strError&_ “(帮助文件:”&Err.HelpFile&“&vbCr&_ “(HelpContext:&Err.HelpContext&”)和_ vbCr&vbCr 如果结束 调试.打印strError 下一个 下一步继续 设置连接=无 Set cmdCommand=Nothing Set rstRecordSet=Nothing 出口接头 端接头

Ms access 其他的 sql=sql&“'”和rstRecordSet.Fields(j)&“,” 如果结束 下一个 sql=Left(sql,Len(sql)-1“删除最后一个” sql=sql&;” CurrentDb.executesql rstRecordSet.MoveNext 循环直到rstRecordSet.EOF=True 如果结束 混合物,关闭 设置连接=无 Set cmdCommand=Nothing Set rstRecordSet=Nothing 出口接头 错误处理: 对于混合连接中的每个错误。错误 strError=“Error#”&错误编号&vbCr&_ “”&Err.Description&vbCr&_ “(来源:&&Err.Source&”)和vbCr&_ “(SQL状态:”&Err.SQLState&“”&vbCr&_ “(NativeError:&Err.NativeError&”)和vbCr 如果Err.HelpFile=“”,则 strError=strError&“没有可用的帮助文件” 其他的 strError=strError&_ “(帮助文件:”&Err.HelpFile&“&vbCr&_ “(HelpContext:&Err.HelpContext&”)和_ vbCr&vbCr 如果结束 调试.打印strError 下一个 下一步继续 设置连接=无 Set cmdCommand=Nothing Set rstRecordSet=Nothing 出口接头 端接头,ms-access,vba,ado,Ms Access,Vba,Ado,此错误最常见的原因是对包含Access数据库的文件夹的权限不正确。您需要设置写入权限。您是指access程序的mdb文件吗?我是指包含mdb文件(以及mdb文件本身)的文件夹。权限已完全控制所有人。mdb还可以完全控制所有人。下一个最常见的错误是SQL Server上的权限错误。当OP说它在不同的服务器上工作,但在Prod上抛出错误时,这就暗示了这一点。我认为这个答案是完全错误的。MDB前端上的权限如何从SQL Server产生ODBC错误? Sub extractInboundCdr() On

此错误最常见的原因是对包含Access数据库的文件夹的权限不正确。您需要设置写入权限。

您是指access程序的mdb文件吗?我是指包含mdb文件(以及mdb文件本身)的文件夹。权限已完全控制所有人。mdb还可以完全控制所有人。下一个最常见的错误是SQL Server上的权限错误。当OP说它在不同的服务器上工作,但在Prod上抛出错误时,这就暗示了这一点。我认为这个答案是完全错误的。MDB前端上的权限如何从SQL Server产生ODBC错误?
Sub extractInboundCdr()
On Error GoTo Error_Handling
   Dim conConnection As New ADODB.Connection
   Dim cmdCommand As New ADODB.Command
   Dim rstRecordSet As New ADODB.Recordset
   Dim Err As ADODB.Error
   Dim strError As String


   Dim eventPlanCode As String
   Dim visitedCountry As String
   Dim startDateTxt As String
   Dim startDate As Date
   Dim endDate As Date
   Dim imsi As String
   Dim currentMonth As String
   Dim nextMonth As String
   Dim currentYear As String
   Dim nextYear As String
   Dim temp As Integer
   Dim i As Integer
   Dim j As Integer

   With conConnection
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = CurrentDb.Name
    .Open
   End With
   conConnection.CommandTimeout = 0

   With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "SELECT * FROM Opt_In_Customer_Record;"
    .CommandType = adCmdText
   End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
   End With

   If rstRecordSet.EOF = False Then
        rstRecordSet.MoveFirst
        Do
            eventPlanCode = rstRecordSet!Event_Plan_Code
            visitedCountry = rstRecordSet!Visited_Country
            startDateTxt = rstRecordSet!start_date
            imsi = rstRecordSet!imsi
            currentMonth = Mid$(startDateTxt, 1, 3)
            currentYear = Mid$(startDateTxt, 8, 4)


            nextMonth = ""
            If (currentMonth = "Jan") Then
                currentMonth = "01"
                nextMonth = "02"
            ElseIf (currentMonth = "Feb") Then
                currentMonth = "02"
                nextMonth = "03"
            ElseIf (currentMonth = "Mar") Then
                currentMonth = "03"
                nextMonth = "04"
            ElseIf (currentMonth = "Apr") Then
                currentMonth = "04"
                nextMonth = "05"
            ElseIf (currentMonth = "May") Then
                currentMonth = "05"
                nextMonth = "06"
            ElseIf (currentMonth = "Jun") Then
                currentMonth = "06"
                nextMonth = "07"
            ElseIf (currentMonth = "Jul") Then
                currentMonth = "07"
                nextMonth = "08"
            ElseIf (currentMonth = "Aug") Then
                currentMonth = "08"
                nextMonth = "09"
            ElseIf (currentMonth = "Sep") Then
                currentMonth = "09"
                nextMonth = "10"
            ElseIf (currentMonth = "Oct") Then
                currentMonth = "10"
                nextMonth = "11"
            ElseIf (currentMonth = "Nov") Then
                currentMonth = "11"
                nextMonth = "12"
            ElseIf (currentMonth = "Dec") Then
                currentMonth = "12"
                nextMonth = "01"
            Else
                GoTo Error_Handling
            End If

            temp = Val(currentYear)
            temp = temp + 1
            nextYear = CStr(temp)

            Exit Do
        Loop Until rstRecordSet.EOF = True
   End If


   Set cmdCommand = Nothing
   Set rstRecordSet = Nothing
   Set connConnection = Nothing

   With conConnection
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = CurrentDb.Name
    .Open
   End With
   conConnection.CommandTimeout = 0

   Dim thisMonthTable As String
   Dim nextMonthTable As String

   thisMonthTable = "dbo_inbound_rated_all_" & currentYear & currentMonth

   If (currentMonth = "12") Then
        nextMonthTable = "dbo_inbound_rated_all_" & nextYear & nextMonth
   Else
        nextMonthTable = "dbo_inbound_rated_all_" & currentYear & nextMonth
   End If

   With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "(SELECT A.IMSI_NUMBER, A.CALL_DATE, A.CALL_TIME, A.VOL_KBYTE, A.TOTAL_CHARGE ,datevalue(A.call_date), A.Service_Code As theDate FROM " & thisMonthTable & " AS A INNER JOIN Opt_In_Customer_Record AS B on A.imsi_number = B.imsi where A.Service_Code = 'GPRS' and Datevalue(A.call_date) >= Datevalue(B.start_date) And Datevalue(A.call_date) < (Datevalue(B.start_date) + val(LEFT(B.event_plan_code, 1))) ) " & _
                   "UNION " & _
                   "(SELECT A.IMSI_NUMBER, A.CALL_DATE, A.CALL_TIME, A.VOL_KBYTE, A.TOTAL_CHARGE ,datevalue(A.call_date), A.Service_Code As theDate FROM " & nextMonthTable & " AS A INNER JOIN Opt_In_Customer_Record AS B on A.imsi_number = B.imsi where A.Service_Code = 'GPRS' and Datevalue(A.call_date) >= Datevalue(B.start_date) And Datevalue(A.call_date) < (Datevalue(B.start_date) + val(LEFT(B.event_plan_code, 1))) ) " & _
                   "Order by A.IMSI_NUMBER, theDate"
    .CommandType = adCmdText
   End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockReadOnly
    .Open cmdCommand
   End With


   If rstRecordSet.EOF = False Then
        rstRecordSet.MoveFirst
        Do
            Dim sql As String
            sql = "insert into IB_CDR values ("

            For j = 0 To rstRecordSet.Fields.Count - 3 '''''Last 2 fields is not inserted
                If (j = 3 Or j = 4) Then '''''These fields are number
                    sql = sql & rstRecordSet.Fields(j) & ","
                Else
                    sql = sql & "'" & rstRecordSet.Fields(j) & "',"
                End If
            Next


            sql = Left(sql, Len(sql) - 1) '''''Remove the last ','
            sql = sql & ");"

            CurrentDb.Execute sql

            rstRecordSet.MoveNext

        Loop Until rstRecordSet.EOF = True
   End If



   conConnection.Close
   Set conConnection = Nothing
   Set cmdCommand = Nothing
   Set rstRecordSet = Nothing

   Exit Sub

Error_Handling:
For Each Err In conConnection.Errors
        strError = "Error #" & Err.Number & vbCr & _
            "   " & Err.Description & vbCr & _
            "   (Source: " & Err.Source & ")" & vbCr & _
            "   (SQL State: " & Err.SQLState & ")" & vbCr & _
            "   (NativeError: " & Err.NativeError & ")" & vbCr
        If Err.HelpFile = "" Then
            strError = strError & "   No Help file available"
        Else
            strError = strError & _
               "   (HelpFile: " & Err.HelpFile & ")" & vbCr & _
               "   (HelpContext: " & Err.HelpContext & ")" & _
               vbCr & vbCr
        End If

        Debug.Print strError
    Next

    Resume Next
Set conConnection = Nothing
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Exit Sub

End Sub