我正在执行以下VBA代码,并且我收到运行时错误,指出Object Required。我已经在线尝试了所有建议的解决方案,但无法解决问题。
Sub connect()
Dim Password As String
Dim SQLStr As String
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
Dim Abbrevation As String
Dim Abbrev_Num As String
Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily
Server_Name = Worksheets("Sheet2").Range("b2").Value
Database_Name = Worksheets("Sheet2").Range("b3").Value ' Name of database
User_ID = Worksheets("Sheet2").Range("b4").Value 'id user or username
Password = Worksheets("Sheet2").Range("b5").Value 'Password
Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT
Cn.Open "Driver={MySQL ODBC 5.2 ANSI Driver};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
myRow = 1
myCol = 7
Abbrevation = Worksheets("Sheet2").Cells(myRow, myCol)
myCol = myCol + 1
Abbrev_Num = Worksheets("Sheet2").Cells(myRow, myCol)
SQLStr = "INSERT INTO acct_abbr(abbrev,number) VALUES('" & Abbrevation & "', '" & Abbrev_Num & "');"
DoCmd.RunSQL SQLStr
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
答案 0 :(得分:2)
Excel无法识别DoCmd。试试这个。
'DoCmd.RunSQL SQLStr
Cn.Execute (SQLStr)