Hi
I've written an application in MS Excel that uses Excel forms to enter and display information that is stored in an MS Access database.
I am developing it to allow users to search for key words in details fields.
I have a problem with my code where I am creating a SQL string on the fly bringing in text the user has entered to search on. The SQL does not bring back any records when run on code. However, if I copy the created SQL and paste into an Access query and run in Access, it does return records.
If anyone has come across this before I would appreciate directing me to links with a solution. I may end up changing the way the query is created to allow records to be returned and if so will post the solution.
The code I am using is below:
Thanks
Mark
PS, on a train as I write this, free INternet access, how cool is that...
I've written an application in MS Excel that uses Excel forms to enter and display information that is stored in an MS Access database.
I am developing it to allow users to search for key words in details fields.
I have a problem with my code where I am creating a SQL string on the fly bringing in text the user has entered to search on. The SQL does not bring back any records when run on code. However, if I copy the created SQL and paste into an Access query and run in Access, it does return records.
If anyone has come across this before I would appreciate directing me to links with a solution. I may end up changing the way the query is created to allow records to be returned and if so will post the solution.
The code I am using is below:
Code:
Private Sub cmdSearchDetails_Click()
Dim con As ADODB.Connection '// requires reference to Microsoft ActiveX Data Objects 2.8 library
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim strDB As String
Dim strSQL As String
strDB = "Data Source=" & strDataSource
Set con = New ADODB.Connection
With con
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open ConnectionString:=strDB
End With
'++ following query works as a union query searching on flag
'SELECT tbl_CallsAndProjects.PK_CallsAndProjects, tbl_CallsAndProjects.Call_Project_Detail
'FROM tbl_CallsAndProjects
'WHERE tbl_CallsAndProjects.Call_Project_Detail Like "*flag*"
'Union
'SELECT tbl_CallsAndProjects.PK_CallsAndProjects, tbl_TimeLog.ActivityNotes
'FROM tbl_CallsAndProjects INNER JOIN tbl_TimeLog ON
'tbl_CallsAndProjects.PK_CallsAndProjects = tbl_TimeLog.FK_CallsAndProjects
'WHERE tbl_TimeLog.ActivityNotes Like "*flag*";
strSQL = "SELECT cp.PK_CallsAndProjects, cp.Call_Project_Detail " _
& "FROM tbl_CallsAndProjects as cp " _
& "WHERE cp.Call_Project_Detail Like '*" & txtSearchString.Value & "*' " _
& "Union " _
& "SELECT cp.PK_CallsAndProjects, t.ActivityNotes " _
& "FROM tbl_CallsAndProjects as cp INNER JOIN tbl_TimeLog as t ON " _
& "cp.PK_CallsAndProjects = t.FK_CallsAndProjects " _
& "WHERE t.ActivityNotes Like '*" & txtSearchString.Value & "*' "
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = con
.CommandText = strSQL
.CommandType = adCmdText
End With
Set rst = cmd.Execute()
'++ code failing at this point as recordset being returned has no records
'++ weird thing is that pasting SQL into access query and running DOES
'++ return records!!!!!!! WHY?
If Not rst.EOF Then
rst.MoveFirst
frmReOpenCall.lstClosed.Column() = rst.GetRows
End If
rst.Close
con.Close
End Sub
Thanks
Mark
PS, on a train as I write this, free INternet access, how cool is that...