vbseq

 这是连接数据库的代码

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
sPath = App.Path & "/TestResult.mdb"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.CursorLocation = adUseClient
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sPath & " ';Persist Security Info=False"
conn.Open
sql = "select * from record"
'rs.StayInSync = True
rs.Open sql, conn, adOpenDynamic, adLockOptimistic
Set MSFlexGrid1.DataSource = rs

运行提示错误:object vaiable or with block variable not set

百度查了也不知道那里的错误,没法解决了,郁闷
大家帮我看看呀~````

 

这是我以前写的一个简单统计的小程序代码,也许会对你有用(要在窗体上加一个Adodc1,visible=false,其它不用设置)
Dim cap As String
Dim ado As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mdbpath As String

Sub initcpmc()
 Set ado = New Connection
 Set rs = New Recordset
 ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
 rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
 rs.MoveFirst
  While Not rs.EOF
   cpmc.AddItem rs("产品名称")
   rs.MoveNext
  Wend
 rs.Close
 ado.Close
End Sub

Private Sub Command1_Click()
If Trim(cpmc.Text) <> "" And Trim(jianshu.Text) <> "" Then
pinming.AddItem cpmc.Text
pingshu.AddItem jianshu.Text
Else
MsgBox "产品名称或产品件数不能为空", vbOKOnly And vbInformation, "Error"
End If
heji.Text = Val(heji.Text) + Val(zongjia.Text)
cpmc.SetFocus
End Sub

Private Sub Command2_Click()
 If pinming.ListCount > 0 And pingshu.ListCount > 0 Then
 Dim kk As Double
 Set ado = New Connection
 Set rs = New Recordset
 ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
 rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
 rs.MoveFirst
 rs.Find "产品名称='" & Trim(pinming.Text) & "'"
 If Not rs.EOF Then
  kk = rs("零售价") * Val(pingshu.Text)
  heji.Text = Val(heji.Text) - kk
 End If
 rs.Close
 ado.Close
 pinming.RemoveItem pinming.ListIndex
 pingshu.RemoveItem pingshu.ListIndex
 End If
End Sub

Private Sub Command3_Click()
 Unload Me
End Sub


Private Sub cpmc_Click()
 Set ado = New Connection
 Set rs = New Recordset
 ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
 rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
 rs.MoveFirst
 rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
 If Not rs.EOF Then
  danjia.Text = rs("零售价")
 End If
 rs.Close
 ado.Close
 zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))

End Sub

Private Sub cpmc_KeyDown(KeyCode As Integer, Shift As Integer)
 Set ado = New Connection
 Set rs = New Recordset
 ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
 rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
 rs.MoveFirst
 rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
 If Not rs.EOF Then
  danjia.Text = rs("零售价")
 End If
 rs.Close
 ado.Close
 zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))

End Sub

Private Sub cpmc_LostFocus()
 'Set ado = New Connection
' Set rs = New Recordset
' ado.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & ";Persist Security Info=False"
' rs.Open "select * from jgtable", ado, adOpenStatic, adLockReadOnly
' rs.MoveFirst
' rs.Find "产品名称='" & Trim(cpmc.Text) & "'"
 'If Not rs.EOF Then
 ' danjia.Text = rs("零售价")
' End If
' rs.Close
' ado.Close
' zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
End Sub

Private Sub Form_Load()
mdbpath = App.Path & "/jgtable.mdb"
cap = Me.Caption
Me.Show

If Dir(mdbpath) = "" Then
MsgBox "系统初始化失败 (-1)" & vbCrLf & "原因:未找到所用数据库" & vbCrLf & "jgtable.mdb", vbOKOnly Or vbInformation, "错误"
Unload Me
Else
initcpmc
End If
End Sub

Private Sub jianshu_GotFocus()
SendKeys "{HOME}+{END}"
End Sub

Private Sub jianshu_KeyPress(KeyAscii As Integer)
 Dim tmp As Boolean
 If KeyAscii = 13 Then Command1_Click
 
  tmp = (KeyAscii > 57) Or (KeyAscii < 48) And (KeyAscii <> 8)
 If tmp Then
  KeyAscii = 0
 End If
End Sub

Private Sub jianshu_KeyUp(KeyCode As Integer, Shift As Integer)
 zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
End Sub

Private Sub jianshu_LostFocus()
 'zongjia.Text = (Val(danjia.Text) * Val(jianshu.Text))
End Sub

Private Sub pingshu_Click()
 Dim dd As Integer
 dd = pingshu.ListIndex
 pinming.Selected(dd) = True
End Sub

Private Sub pinming_Click()
 Dim dd As Integer
 dd = pinming.ListIndex
 pingshu.Selected(dd) = True
End Sub

Private Sub Timer1_Timer()
 Me.Caption = cap & " (" & Now & ")"
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值