2012-01-24 76 views
5

嗨,即时通讯对于VBA来说相当新,我希望有人可以用最后一段代码来帮助我。在Excel中的VBA宏来运行SQL插入语句

我试图从一个电子表格单元格,并将它们添加到SQL表,但我有运行的SQL语句trubble。这是我迄今为止的代码。

 Private Sub ConnectDB() 

     Dim oConn As Object 

Set oConn = CreateObject("ADODB.Connection") 
oConn.Open = "DRIVER={SQL Server};" & _ 
"SERVER=SERVER02;" & _ 
"DATABASE=platform;" & _ 
"USER=5y5t3mus3r;" & _ 
"PASSWORD=*******;" & _ 
"Option=3;" 
If oConn.State = adStateOpen Then 
    MsgBox "Welcome to Database!" 
Else 
MsgBox "Sorry No Database Access." 
End If 


Dim rs As ADODB.Recordset 
Dim strSQL As String 
Dim Company As String 
Dim Address As String 
Dim Address1 As String 
Dim Address2 As String 
Dim County As String 
Dim Contact As String 
Dim Phone As String 
Dim Fax As String 


    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _ 
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";" 


Sheets("wsheet").Activate 

Set rs = New ADODB.Recordset 
rs.Source = Sql 


    With wsheet 


     MyFile = "C:\Users\Ryan.ICS\Documents\Documents\InsertStatement.txt" 
     fnum = FreeFile() 
     Open MyFile For Output As fnum 


     myRow = 2 
      myCol = 4 

      For myRow = 2 To InputBox(Prompt:="What is the last row of data?", Title:="Data Row", Default:=1) 

      myCol = 4 


Company = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 
Address = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 
Address1 = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 
Address2 = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 
Address3 = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 2 
Phone = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 
Fax = ActiveSheet.Cells(myRow, myCol) 
myCol = myCol + 1 


     strSQL = "INSERT INTO [sandbox].[5y5t3mus3r].[ryan] (Organisation, Address1, Address2, TownCity, County, Telephone, Fax) VALUES('" & Company & "', '" & Address & "', '" & Address1 & "', '" & Address2 & "', '" & Address3 & "', " & Phone & ", " & Fax & ");" 

    Print #fnum, strSQL 

    DoCmd.RunSQL strSQL ***Here is where I am haveing an error it will not run the SQL command.**** 

     oConn.Execute strSQL **** here is another tag I tried in a number of different ways but i still couldnt get the SQL statement to run 



    Next 


     End With 

      ' Find out how many rows were affected by the Insert. 
     Set rs = oConn.Execute("Select @@rowcount") 
     ' Display the first field in the recordset. 
      MsgBox rs(0) & " rows inserted" 

      oConn.Close 



      Set rs = Nothing 
      Set oConn = Nothing 

       Close #fnum 


      End Sub 

     Function esc(txt As String) 

    esc = Trim(Replace(txt, "'", "\'")) 

     End Function 

当我试图运行SQL语句时出现错误,是否需要为此或某事创建对象或方法。

任何帮助,这将非常感谢谢谢!

+1

你得到哪个错误? 'DoCmd'?这是一个Access对象,而不是Excel对象。 –

+0

你得到哪个错误?的DoCmd?这是一个Access对象,而不是Excel对象。 - Olivier Jacot-Descombes 1分钟前嗨Olivier no我得到一个运行时错误'424':Object required感谢您的快速回复 – user1167046

+2

执行'debug.print strSQL'(在您构建它之后,但在执行之前)。然后将输出的字符串复制到即时窗口。然后将查询粘贴到您最喜欢的SQL工具中并运行它。我认为最好的想法是首先得到一个工作的INSERT INTO,然后修改你的VBA使其匹配。希望这可以帮助。 – jon

回答

0

this article

它是从Excel导入数据与ADO到SQL一个简单的方法。

[]的

1

我想这是此行:

rs.Source = SQL

Source属性接受一个I​​Stream以及一个字符串,所以由于SQL ISN”在任何地方声明,它隐含地是一个值为Nothing的对象。

我的下一个猜测是下面几行,wsheet是在哪里分配的?

当然,如果我们知道这行中出现的错误...还是比较容易,如果你设置一个破发点,并进入代码这一切会更容易 - 你不需要转储变量值到文件,您可以在调试器中以交互方式查看它们。

1

而不是使用INSERT语句,我建议建立一个存储过程,并使用ADOBO.Command对象传递值进去。