程序实现每隔两分钟给一字段增加值,代码如下:
Dim sql As String
Dim Conn As New ADODB.Connection
Dim i As IntegerPrivate Sub Form_Load()
 Timer1.Interval = 1000
i = 1
End SubPrivate Sub Timer1_Timer()
'On Error GoTo exer
 If i > 2 * 60 Then
  i = 1
   Conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=Stocks;Data Source=127.0.0.1"
   Conn.ConnectionTimeout = 10
   Conn.Open
   sql = "update StoreIn set ProNum=ProNum+20 where ProNum is Null"  
  i = i + 1
End If
  Exit Sub   MsgBox "连接服务器失败"
End Sub
调试显示Conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=Stocks;Data Source=127.0.0.1"
有错误,“对象被打开时,操作不被允许”。帮忙看看,代码能实现所需要的功能吗?

解决方案 »

  1.   

    Conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=Stocks;Data Source=127.0.0.1"
       Conn.ConnectionTimeout = 10
       Conn.Open
    这些语句放在form_load过程
    还有你的定时器过程里面怎么没有执行更新语句?
      

  2.   

    '楼主看来是刚入门的,帮你改改Dim sql As String
    Dim Conn As New ADODB.Connection
    Dim i As IntegerPrivate Sub Form_Load()
     Timer1.Interval = 1000
    i = 1
       Conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=Stocks;Data Source=127.0.0.1"
       Conn.ConnectionTimeout = 10
       Conn.Open
    End SubPrivate Sub Timer1_Timer()
    'On Error GoTo exer
      i = i + 1'这句应该调在这里,每秒钟执行一次
     If i > 2 * 60 Then
      i = 1   sql = "update StoreIn set ProNum=ProNum+20 where ProNum is Null"  
       Conn.execute sql   '这句执行更新语句End If
      Exit Sub   MsgBox "连接服务器失败"
    End Sub