简体   繁体   English

在VB6中执行查询

[英]Executing a query in VB6

I'm trying to execute a query in VB6. 我正在尝试在VB6中执行查询。 At the moment I have this: 目前,我有这个:

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset


Private Sub Form_Load()
 Set conn = New ADODB.Connection
 conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & App.Path & "\DB1.mdb; Persist Security Info = False"
 conn.Open
End Sub

Private Sub Command1_Click()
 Dim strSQL As String
 strSQL = "SELECT * FROM customers WHERE name = " & Text2.Text
 rs.Open strSQL, conn
End Sub

This results in a object variable or with block variable not set . 这将导致object variable or with block variable not set

I'm kind of new to VB and Access databases, so any answers or links to good tutorials would be appreciated. 我是VB和Access数据库的新手,因此感谢您提供任何答案或指向好教程的链接。

rs is never instantiated. rs从未实例化。 Try 尝试

Dim rs As New ADODB.Recordset

Add

Set rs = New ADODB.Recordset

before running rs.Open. 在运行rs.Open之前。 Same idea as the connection, the object needs to be instantiated first. 与连接相同,需要首先实例化该对象。 Other than that you're ok. 除此之外,你还好。

It's also good practice to close the rs and connection when you're done with them. 完成rs和连接后,这也是一种好习惯。

You don't instantiate the rs in your coding 您没有在编码中实例化rs

Dim rs As New ADODB.Recordset

Also check your query that is use the name in where condition as below if the name is text type 另外,请检查您的查询是使用namewhere如下如果名称是文本类型条件

strSQL = "SELECT * FROM customers WHERE name = '" & Text2.Text & "'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM