英文:
VBA Code does not return any value or either give an error; user-define
问题
这是我的代码:它应该返回在msaccess文本框列表中搜索字段的结果。搜索条件使用逗号分隔。
Option Compare Database
Private Sub Command7_Click()
End Sub
Public Sub btnSearch2_Click()
Dim ArrayList() As String
Dim strSQL As String
Dim i As Integer
Dim rstSearchResults As ADODB.Recordset
' 通过逗号拆分参数值并将它们存储在数组中
ArrayList = Split(Me.txtSearchCriteria2.Value, ",")
' 构建SQL查询
strSQL = "SELECT CSP_Data.Manufacturer_Part_Number, CSP_Data.CSP_Number, CSP_Data.Rec_Min_Qty, CSP_Data.CSP_Type " & _
"FROM CSP_Data " & _
"WHERE Manufacturer_Part_Number ILike '*" & Me.txtSearchCriteria2.Value & "*'"
' 创建一个新的记录集
Set rstSearchResults = New ADODB.Recordset
' 基于SQL查询打开记录集
rstSearchResults.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
' 将列表框绑定到记录集
Me.lstSearchResults2.RowSource = rstSearchResults
' 重新查询列表框以显示数据
Me.lstSearchResults2.Requery
' 清理
rstSearchResults.Close
Set rstSearchResults = Nothing
End Sub
尝试从ChatGPT获取此代码的修复,但没有用。我仍然没有收到任何响应。
英文:
Here's my code: It should return the results from search filed in msaccess text box list. Search criteria is separated with " , "
I am trying to search multiple part numbers.
Option Compare Database
Private Sub Command7_Click()
End Sub
Public Sub btnSearch2_Click()
Dim ArrayList() As String
Dim strSQL As String
Dim i As Integer
Dim rstSearchResults As ADODB.Recordset
' Split the parameter values by comma and store them in an array
ArrayList = Split(Me.txtSearchCriteria2.Value, ",")
' Construct the SQL query
strSQL = "SELECT CSP_Data.Manufacturer_Part_Number, CSP_Data.CSP_Number, CSP_Data.Rec_Min_Qty, CSP_Data.CSP_Type " & _
"FROM CSP_Data " & _
"WHERE Manufacturer_Part_Number ILike '*" & Me.txtSearchCriteria2.Value & "*'"
' Create a new recordset
Set rstSearchResults = New ADODB.Recordset
' Open the recordset based on the SQL query
rstSearchResults.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
' Bind the list box to the recordset
Me.lstSearchResults2.RowSource = rstSearchResults
' Requery the list box to display the data
Me.lstSearchResults2.Requery
' Clean up
rstSearchResults.Close
Set rstSearchResults = Nothing
End Sub
tried getting this code fixed from chatgpt but its not useful. i still dont get any response
答案1
得分: 1
你需要逐个添加where
子句:
Dim el, sep As String, arraylist, rstSearchResults
arraylist = Split(Me.txtSearchCriteria2.Value, ",")
strSQL = " SELECT CSP_Data.Manufacturer_Part_Number, CSP_Data.CSP_Number, " & _
" CSP_Data.Rec_Min_Qty, CSP_Data.CSP_Type " & _
" FROM CSP_Data WHERE "
For Each el In arraylist
If Len(Trim(el)) > 0 Then
strSQL = strSQL & sep & "Manufacturer_Part_Number Like '*" & Trim(el) & "*' "
sep = " or " '在第一次迭代后填充 or
End If
Next el
Set rstSearchResults = New ADODB.Recordset
rstSearchResults.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
英文:
You need to add where
clauses one-by-one:
Dim el, sep As String, arraylist, rstSearchResults
arraylist = Split(Me.txtSearchCriteria2.Value, ",")
strSQL = " SELECT CSP_Data.Manufacturer_Part_Number, CSP_Data.CSP_Number, " & _
" CSP_Data.Rec_Min_Qty, CSP_Data.CSP_Type " & _
" FROM CSP_Data WHERE "
For Each el In arraylist
If Len(Trim(el)) > 0 Then
strSQL = strSQL & sep & "Manufacturer_Part_Number Like '*" & Trim(el) & "*' "
sep = " or " 'populate or after first iteration
End If
Next el
Set rstSearchResults = New ADODB.Recordset
rstSearchResults.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论