Vb6 使用组合框搜索并在文本框中过滤结果
我试图在VB6.0上做一个小程序,在数据库中查找记录,并根据组合框选择将其打印到文本框中,但我没有找到允许我这样做的代码 请帮忙Vb6 使用组合框搜索并在文本框中过滤结果,vb6,Vb6,我试图在VB6.0上做一个小程序,在数据库中查找记录,并根据组合框选择将其打印到文本框中,但我没有找到允许我这样做的代码 请帮忙 Dim adoCon Dim adoRs Dim strSQL As String Dim strDB As String 'Change YourDatabaseName to actual database you have strDB = "c:\path\YourDatabaseName.accdb" Set adoCon = CreateObje
Dim adoCon
Dim adoRs
Dim strSQL As String
Dim strDB As String
'Change YourDatabaseName to actual database you have
strDB = "c:\path\YourDatabaseName.accdb"
Set adoCon = CreateObject("ADODB.Connection")
adoCon.Open "Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = " & strDB & ";" & _
"Persist Security Info = False;"
'Change Table1 to your table name in MS Access
'change the name of combobox and the fieldname in MS Access table
'
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
' if combo is numeric
strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = " + [combo].Value + ";"
' if combo is text
'strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = '" + [combo].Value + "';"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Set adoRs = CreateObject("ADODB.Recordset")
'Set the cursor type we are using so we can navigate through the recordset
adoRs.CursorType = 2
'Set the lock type so that the record is locked by ADO when it is updated
adoRs.LockType = 3
'Open the tblComments table using the SQL query held in the strSQL varaiable
'adoRs.Open strSQL, adoCon
If Not adoRS.Eof() Then
[yourTextBox] = adoRs(0)
End If
adoRs.Close
adoCon.Close
Set adoRs = Nothing
Set adoCon = Nothing
下面的“BOF”不是打字错误。如果记录集为空(查询未返回任何记录),则BOF将为真
adoRs.Open strSQL, adoCon
If Not adoRS.BOF Then
'If not _BOF_ we have records so load the first record
adoRs.MoveFirst
'If first field is a string then use this
[yourTextBox] = adoRs.Fields(0).Value
'If first field is numeric then use this
[yourTextBox] = CStr(adoRs.Fields(0).Value)
Else
Msgbox "No records returned."
End If
如果您正在处理多条记录,您仍然会先执行MoveFirst,然后循环,直到EOF为true,然后处理每条记录。当没有更多记录要处理时,MoveNext将设置EOF=True
adoRs.Open strSQL, adoCon
If Not adoRS.BOF Then
'If not _BOF_ we have records so load the first record
adoRs.MoveFirst
Do While Not adoRS.EOF
'Process records here
'.
'.
'.
adoRS.MoveNext
Loop
Else
Msgbox "No records returned."
End If
我认为在Vb6中,我们使用“&”而不是“+”对于字符串连接….VB.NET与VB6不同可能您应该取消注释打开记录集的行,并在指定的位置使用对象和控件名称。和+都将连接字符串,但如果遇到空值或仅数值,则会发生不同的情况。请修改代码并向我提供最终结果其中一个在adoRs上返回错误“参数类型错误,超出可接受范围或相互冲突”。请打开strSQL,adoCon行,请提供帮助。这是VB6,因此您需要定义变量类型:Dim adoCon为ADODB.Connection,Dim adoRs为ADODB.Recordset。要使其工作,必须通过VB6 Project->References菜单设置对MIcrosoft ActiveX数据对象的引用。