永发信息网

急,VBA代码出错了,来帮我看一下

答案:1  悬赏:80  手机版
解决时间 2021-05-06 20:31
  • 提问者网友:欺烟
  • 2021-05-06 17:07

这个代码是用指定单元格里的数据做为条件查询SQL server数据到指定的行列。但是出错了,各位帮我看一下!

 

Private Sub abc()
Dim oConn1 As New ADODB.Connection
Dim oRS1 As New ADODB.Recordset
Dim SQL As String
Dim InOut, IsParts As String
Dim i As Integer

currentrow = 10
i = 0
oConn1.Open "DSN=sjj;uid=sa;pwd=123456;database=jackydata"


If Cells(currentrow, 1).Value <> "" Then
   Range(Cells(currentrow, 1), Cells(currentrow, 2)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Delete Shift:=xlUp
End If

SQL = "Execute CheckStockReturnMatStatusForVod '" & Cells(6, 5).Value & "'"


Set oRS1 = New ADODB.Recordset
oRS1.Open SQL, oConn1
If oRS1.EOF Then
   MsgBox ("查询完成")
   Exit Sub
End If
i = 1
Sheet1.Cells(5, 3).Value = oRS1.Fields("NoOfRec")


currentrow = 10
Sheet1.Cells(currentrow, 1).Select

Do While Not oRS1.EOF
   Sheet1.Cells(currentrow, 1).Value = oRS1.Fields("name")
   Sheet1.Cells(currentrow, 2).Value = oRS1.Fields("phone")
 
   currentrow = currentrow + 1
   oRS1.MoveNext
   Cells(currentrow, 1).Select
Loop

If oRS1.State = adStateOpen Then
   oRS1.Close
End If
Set oRS1 = Nothing
If oConn1.State = adStateOpen Then
   oConn1.Close
End If
currentrow = 6
Cells(currentrow, 1).Select


End Sub

 

 

到这里就出错了,

Set oRS1 = New ADODB.Recordset
oRS1.Open SQL, oConn1
If oRS1.EOF Then
   MsgBox ("查询完成")
   Exit Sub
End If
i = 1
Sheet1.Cells(5, 3).Value = oRS1.Fields("NoOfRec")

 

说 运行时错误'-2147217900 (80040e14)';

      自动化(Automation)错误

 

 

各位大虾帮我看一下修改一下,多谢了!

 

 

 

最佳答案
  • 五星知识达人网友:有你哪都是故乡
  • 2021-05-06 18:15
把 for i = 0 to的0改为1,否则Mid函数出错。和你学了一招:if strValue Like "[A-Z a-z]"谢谢。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯