在Access中的现有表中插入Excel工作表

时间:2015-01-21 21:30:19

标签: excel vba ms-access

我是VBA的新手,在这里我要从excel表中插入一些循环遍历的数据,并且在Access现有代码中,代码运行正常,但不会在表中插入任何数据,我也试试使用记录集附加该数据,但由于数据类型问题而起作用。请引导我完成它,非常感谢您提前。  这是我的代码:

Const AccessConnectionString  As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\Documents and Settings\e4umts\Desktop\New Database\IRG Analytics--New.accdb;Persist Security Info=False"
Sub Import()
 Dim dbsIRG As ADODB.Connection
 Dim ConnectionString As String
 Dim IRGConn As ADODB.Connection
 Dim Mypath As String
 Dim IRGCmd As New ADODB.Command
 Dim r As Range
 Dim column As Integer
 Dim row As Integer

 Mypath = "C:\Documents and Settings\e4umts\Desktop\New Folder\Liquidation Exceptions Report.xls"
 Set IRGConn = New ADODB.Connection
 IRGConn.ConnectionString = AccessConnectionString
 IRGConn.Open
 Set IRGCmd = New ADODB.Command
 IRGCmd.ActiveConnection = IRGConn                     
 For Each r In ActiveSheet.Range("A2", Range("A2").End(xlDown))          
      If ActiveSheet.Range("A2", Range("A2").End(xlDown)) Is Nothing Then
      IRGCmd.CommandText = _
      GetSQL( _
            r.Offset(0, 0).Value, _
            r.Offset(0, 1).Value, _
            r.Offset(0, 2).Value, _
            r.Offset(0, 3).Value, _
            r.Offset(0, 4).Value, _
            r.Offset(0, 5).Value, _
            r.Offset(0, 6).Value, _
            r.Offset(0, 7).Value, _
            r.Offset(0, 8).Value, _
            r.Offset(0, 9).Value, _
            r.Offset(0, 10).Value, _
            r.Offset(0, 11).Value, _
            r.Offset(0, 12).Value, _
            r.Offset(0, 13).Value, _
            r.Offset(0, 14).Value, _
            r.Offset(0, 15).Value, _
            r.Offset(0, 16).Value, _
            r.Offset(0, 17).Value, _
            r.Offset(0, 18).Value, _
            r.Offset(0, 19).Value, _
            r.Offset(0, 20).Value, _
            r.Offset(0, 21).Value, _
            r.Offset(0, 22).Value)

        ActiveSheet.Range("A2", Range("A2").End(xlDown)).Value = ""
        IRGCmd.Execute
    Else

        End If



    Next r

    IRGConn.Close
    Set IRGConn = Nothing
 End Sub



Function GetSQL(LoanNumber As Integer, Manager As String, Analyst As String, _
   ServicerName As String, ServicerNumber As Integer, ServicerLoanNumber As Integer, _
    PoolNumber As Integer, RemmittanceType As String, SaleType As String, ActionCode As Integer, _
     ActivityDate As Date, ActionDate As Date, LPI As Date, InterestRate As Double, PandI As Double, _
       UPB As Double, ReportedPrincipal As Double, ReportedInterest As Double, AppliedPrincipal As Double, _
         AppliedInterest As Double, InvestorPassThruRate As Double, PFPIntAdv As Double, Months As Date) As String


   Dim strSql As String

    strSql = _
             " INSERT INTO Table1" & _
             " (LoanNumber, Manager, Analyst, ServicerName, ServicerNumber, ServicerLoanNumber," & _
             " PoolNumber, RemittanceType, SaleType, ActionCode, ActivityDate, ActionDate, LPI, InterestRate," & _
             " PandI, UPB, ReportedPrincipal, ReportedInterest, AppliedPrincipal, AppliedInterest, InvestorPassThruRate, PFPIntAdv, Months )" & _
             " VALUES (" & _
             " Cstr'FannieMaeLoanNumber'),'" & Manager & "','" & Analyst & "','" & ServicerName & "'," & _
             " Cstr('ServicerNumber'),Cstr('ServicerLoanNumber'), Cstr('PoolNumber'), '" & RemmittanceType & "'" & _
             " '" & SaleType & "', Cstr('ActionCode'), #" & ActivityDate & "#, #" & ActionDate & "#,#" & LPI & "#,Cstr('InterestRate')," & _
             " Cstr('PandI'),Cstr('UPB'),Cstr('ReportedPrincipal'),Cstr('ReportedInterest'),Cstr('AppliedPrincipal'),Cstr('AppliedInterest'),Cstr('InvestorPassThruRate')," & _
             " Cstr('PFPIntAdv'),#" & Months & "#)"


    GetSQL = strSql


  End Function

1 个答案:

答案 0 :(得分:0)

非常感谢你的回复,我去了你发布的代码我真的没有做过的是我没有看到任何文件xl文件路径,我必须每个月将数据插入到表中,并且xlfile保存在特定文件夹中,给定静态名称,我所做的是首先我通过vba重写excel上的字段名称以匹配访问中的我的表字段名称,我激活xlfile,之后我尝试导入。因为我正在进行访问它自己我不认为我必须给数据库连接字符串。我真的很困惑这里如果你能为我解释它会非常好。  谢谢 的Manoj