Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

MS Access & Visual Basic.net Query - Error Message - Expression too complex

Hi All,
I have a VB.net form with a textbox. The text from the textbox is used in the code below to search a fairly large Access database with numerous fields for matching text. The SQL seems to be fine but when I run the code I get an error message which of "Expression too complex". I know the SQL is pretty long, is that what causes the error message? Is there any way around it? Should I be using something other than DataReader? I would appreciate any pointers on this.
Thanks,
Boz.
Dim myOLEDBConnection As New OleDbConnection(My.Settings.FilesDBConnectionString)
            Dim Search1Command As New OleDb.OleDbCommand("SELECT * FROM ActiveFilesOldSystem WHERE [FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & [ClientCode] & [CoCliSur] & [CoCliFirst] & [Client1Title] & [Client1Surname] & [Client11stName] & [C1CareOf] & [C1Address1] & [C1Address2] & [C1Address3] & [C1Town] & [C1County] & [C1Postcode] & [C1Country] & [C1ClientType] & [C1PPSNumber] & [C1FaxNo] & [C1WorkPhoneNo] & [C1HomePhoneNo] & [C1MobileNo] & [C1WillNo] & [C1WillStatLim] & [Client2Title] & [Client2Surname] & [Client21stName] & [RelC1C2] & [C2CareOf] & [C2Address1] & [C2Address2] & [C2Address3] & [C2Town] & [C2County] & [C2Postcode] & [C2Country] & [C2PPSNumber] & [C2ClientType] & [C2FaxNo] & [C2WorkPhoneNo] & [C2HomePhoneNo] & [C2MobileNo] & [Client3Surname] & [Client31stName] & [RelationshipC1C23] & [Cli3Title] & [C3CareOf] & [C3Address1] & [C3Address2] & [C3Address3] & [C3Town] & [C3County] & [C3Postcode] & [C3Country] & [C3PPSNumber] & [C3ClientType] & [C3FaxNo] & [C3WorkPhoneNo] & [C3HomePhoneNo] & [C3MobileNo]  & [Client4Surname] & [Client41stName] & [Cli4Title] & [RelationshipC1C234] & [C4CareOf] & [C4Address1] & [C4Address2] & [C4Address3] & [C4Town] & [C4County] & [C4Country] & [C4Postcode] & [C4PPSNumber] & [C4ClientType] & [C4FaxNo] & [C4WorkPhoneNo] & [C4HomePhoneNo] & [C4MobileNo] & [FullDescriptionofMatter] & [Solicitor] & [CaseType] & [WillNoC1] & [WillNoC2] & [Court] & [UT1toWhom] & [UT1forWhom] & [UT1forWhat] & [UT1PartsProp] & [UT1Date] & [UT1DischargeDate] & [UT1RegisteredonFileNo] & [UT1Note] & [UT2toWhom] & [UT2forWhom] & [UT2forWhat] & [UT2PartsProp] & [UT2Date] & [UT2RegisteredonFileNo] & [UT2DischargeDate] & [UT2Note] & [UT3toWhom] & [UT3forWhom] & [UT3forWhat] & [UT3PartsProp] & [UT3Date] & [UT3RegisteredonFileNo] & [UT3DischargeDate] & [UT3Note] & [UT4toWhom] & [UT4forWhom] & [UT4forWhat] & [UT4PartsProp] & [UT4Date] & [UT4RegisteredonFileNo] & [UT4DischargeDate] & [UT4Note] & [FileFinished] & [FileOpened] & [FileDestroyed] & [EstimateDates] & [AccountDates] & [AccountPaidDates] & [FundsRequested] & [FundsReceived] & [StatOfLimitations] & [CrossRefFileName] & [CourtRecordNo] & [PapersFile] & [RelatedFiles] & [CourtListNo] & [Solicitorsforotherside] & [Status] & [InsuranceCo] & [ClaimsHandler] & [InsCoRef] & [Barrister] & [Notes]  LIKE '%' & @strSearchString1 & '%' ", myOLEDBConnection)
            With Search1Command.Parameters
                .Add(New OleDbParameter("@strSearchString1", strSearchString1))
            End With
            myOLEDBConnection.Open()
            Dim myReader As OleDb.OleDbDataReader
            myReader = Search1Command.ExecuteReader
            Dim myDT As New DataTable
            myDT.Load(myReader)
            dgContacts.DataSource = myDT
            lblRecordNumberDetails.Text = "Your search found " & myDT.Rows.Count & " results"

Open in new window

0
BozM
Asked:
BozM
  • 7
  • 4
  • 3
1 Solution
 
tiagosalgadoCommented:
If you run that query in access it works well ?
0
 
tiagosalgadoCommented:
Have you run that query in access it works well ?
0
 
tiagosalgadoCommented:
Sorry last post. Ignore it
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
BozMAuthor Commented:
Hi,
As far as I understand it, if I run the query in Access, I will need to change the SQL syntax completely. There would be then no way of knowing if the the original SQL was correct or not.
0
 
tiagosalgadoCommented:
I don't think your query is correct.
Where do you get this ? "WHERE [FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & ... like '% .... " ?
0
 
BozMAuthor Commented:
Sorry, I am not sure what you mean. Which bit of the query do you think is not correct? The query works fine if i reduce it to five or six fields so the syntax would appear to be correct.
0
 
jpaulinoCommented:
>> The query works fine if i reduce it to five or six fields so the syntax would appear to be correct.
But using the above code that you have showed or in the access query  ?
You code isn't correct and I believe that isn't working for you. Since you use parameters you should do just this in you T-SQL:
 "...  LIKE @strSearchString1"  
Then in the parameter:
.Add(New OleDbParameter("@strSearchString1", "%" & strSearchString1 & "%"))
0
 
BozMAuthor Commented:
Hi jpaulino,
Thanks for your comment. I don't mean to be contrary but when I shave down the SQL in the way I have done below (i.e. to 10 fields) it works fine. The problem seems to be connected with the number of fields rather than the SQL syntax. But I will give your suggestion a try anyway.
Thanks,
Boz.
If strtype = "Files" Then
            Dim myOLEDBConnection As New OleDbConnection(My.Settings.FilesDBConnectionString)
            'Dim Search1Command As New OleDb.OleDbCommand("SELECT * FROM ActiveFilesOldSystem WHERE [FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & [ClientCode] & [CoCliSur] & [CoCliFirst] & [Client1Title] & [Client1Surname] & [Client11stName] & [C1CareOf] & [C1Address1] & [C1Address2] & [C1Address3] & [C1Town] & [C1County] & [C1Postcode] & [C1Country] & [C1ClientType] & [C1PPSNumber] & [C1FaxNo] & [C1WorkPhoneNo] & [C1HomePhoneNo] & [C1MobileNo] & [C1WillNo] & [C1WillStatLim] & [Client2Title] & [Client2Surname] & [Client21stName] & [RelC1C2] & [C2CareOf] & [C2Address1] & [C2Address2] & [C2Address3] & [C2Town] & [C2County] & [C2Postcode] & [C2Country] & [C2PPSNumber] & [C2ClientType] & [C2FaxNo] & [C2WorkPhoneNo] & [C2HomePhoneNo] & [C2MobileNo] & [Client3Surname] & [Client31stName] & [RelationshipC1C23] & [Cli3Title] & [C3CareOf] & [C3Address1] & [C3Address2] & [C3Address3] & [C3Town] & [C3County] & [C3Postcode] & [C3Country] & [C3PPSNumber] & [C3ClientType] & [C3FaxNo] & [C3WorkPhoneNo] & [C3HomePhoneNo] & [C3MobileNo]  & [Client4Surname] & [Client41stName] & [Cli4Title] & [RelationshipC1C234] & [C4CareOf] & [C4Address1] & [C4Address2] & [C4Address3] & [C4Town] & [C4County] & [C4Country] & [C4Postcode] & [C4PPSNumber] & [C4ClientType] & [C4FaxNo] & [C4WorkPhoneNo] & [C4HomePhoneNo] & [C4MobileNo] & [FullDescriptionofMatter] & [Solicitor] & [CaseType] & [WillNoC1] & [WillNoC2] & [Court] & [UT1toWhom] & [UT1forWhom] & [UT1forWhat] & [UT1PartsProp] & [UT1Date] & [UT1DischargeDate] & [UT1RegisteredonFileNo] & [UT1Note] & [UT2toWhom] & [UT2forWhom] & [UT2forWhat] & [UT2PartsProp] & [UT2Date] & [UT2RegisteredonFileNo] & [UT2DischargeDate] & [UT2Note] & [UT3toWhom] & [UT3forWhom] & [UT3forWhat] & [UT3PartsProp] & [UT3Date] & [UT3RegisteredonFileNo] & [UT3DischargeDate] & [UT3Note] & [UT4toWhom] & [UT4forWhom] & [UT4forWhat] & [UT4PartsProp] & [UT4Date] & [UT4RegisteredonFileNo] & [UT4DischargeDate] & [UT4Note] & [FileFinished] & [FileOpened] & [FileDestroyed] & [EstimateDates] & [AccountDates] & [AccountPaidDates] & [FundsRequested] & [FundsReceived] & [StatOfLimitations] & [CrossRefFileName] & [CourtRecordNo] & [PapersFile] & [RelatedFiles] & [CourtListNo] & [Solicitorsforotherside] & [Status] & [InsuranceCo] & [ClaimsHandler] & [InsCoRef] & [Barrister] & [Notes]  LIKE '%' & @strSearchString1 & '%' ", myOLEDBConnection)
            Dim Search1Command As New OleDb.OleDbCommand("SELECT * FROM ActiveFilesOldSystem WHERE [FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & [ClientCode] & [CoCliSur] & [CoCliFirst] & [Client1Title] & [Client1Surname] & [Client11stName] LIKE '%' & @strSearchString1 & '%' ", myOLEDBConnection)
            With Search1Command.Parameters
                .Add(New OleDbParameter("@strSearchString1", strSearchString1))
            End With
            myOLEDBConnection.Open()
            Dim myReader As OleDb.OleDbDataReader
            myReader = Search1Command.ExecuteReader
            Dim myDT As New DataTable
            myDT.Load(myReader)
            dgContacts.DataSource = myDT
            lblRecordNumberDetails.Text = "Your search found " & myDT.Rows.Count & " results"
        End If

Open in new window

0
 
BozMAuthor Commented:
Sorry, the code snippet didn't come out right. Only Line 2 should be commented out.
0
 
BozMAuthor Commented:
Hi jpaulino,
I tried your suggestion and I got the same error message I got with my own code - Expression too complex. I'd appreciate any further suggestions.
Thanks,
Brian.
0
 
jpaulinoCommented:
I really don't know if you have any size limit for the concatenation ... maybe there is an workaround for it.
Can you attach the database with only that table and no data ?
0
 
BozMAuthor Commented:
Yeah, I'll give that a go. It might be later on today as I am just heading on the door to a meeting.
0
 
BozMAuthor Commented:
Apologies for delay - this the database - definition only.
Experts-Exch-AFOS.mdb
0
 
jpaulinoCommented:
Well, it really seams that doesn't work. It's too big and also doesn't work in the access query designer.
You can create an auxiliary query to split in two fields and then you look on that ones. Create a query with the name
qActiveFilesOldSystem like the snippet bellow and then change this on the code:

Dim Search1Command As New OleDb.OleDbCommand("SELECT * FROM qActiveFilesOldSystem WHERE part1 LIKE @part1 AND part2 LIKE @part2;", myOLEDBConnection)
Search1Command.Parameters.Add("@part1", OleDbType.VarChar).Value = "%" & strSearchString1 & "%"
Search1Command.Parameters.Add("@part2", OleDbType.VarChar).Value = "%" & strSearchString1 & "%"

in the end of the routine you can hide the two extra fields on the datagrididview:

Me.DataGridView1.Columns(0).Visible = False
Me.DataGridView1.Columns(1).Visible = False

SELECT ([FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & [ClientCode] & [CoCliSur] & [CoCliFirst] & [Client1Title] & [Client1Surname] & [Client11stName] & [C1CareOf] & [C1Address1] & [C1Address2] & [C1Address3] & [C1Town] & [C1County] & [C1Postcode] & [C1Country] & [C1ClientType] & [C1PPSNumber] & [C1FaxNo] & [C1WorkPhoneNo] & [C1HomePhoneNo] & [C1MobileNo] & [C1WillNo] & [C1WillStatLim] & [Client2Title] & [Client2Surname] & [Client21stName] & [RelC1C2] & [C2CareOf] & [C2Address1] & [C2Address2] & [C2Address3] & [C2Town] & [C2County] & [C2Postcode] & [C2Country] & [C2PPSNumber] & [C2ClientType] & [C2FaxNo] & [C2WorkPhoneNo] & [C2HomePhoneNo] & [C2MobileNo] & [Client3Surname] & [Client31stName] & [RelationshipC1C23] & [Cli3Title] & [C3CareOf] & [C3Address1] & [C3Address2] & [C3Address3] & [C3Town] & [C3County] & [C3Postcode] & [C3Country] & [C3PPSNumber] & [C3ClientType] & [C3FaxNo] & [C3WorkPhoneNo] & [C3HomePhoneNo] & [C3MobileNo]  & [Client4Surname] & [Client41stName] & [Cli4Title] & [RelationshipC1C234]) AS part1, ([C4CareOf] & [C4Address1] & [C4Address2] & [C4Address3] & [C4Town] & [C4County] & [C4Country] & [C4Postcode] & [C4PPSNumber] & [C4ClientType] & [C4FaxNo] & [C4WorkPhoneNo] & [C4HomePhoneNo] & [C4MobileNo] & [FullDescriptionofMatter] & [Solicitor] & [CaseType] & [WillNoC1] & [WillNoC2] & [Court] & [UT1toWhom] & [UT1forWhom] & [UT1forWhat] & [UT1PartsProp] & [UT1Date] & [UT1DischargeDate] & [UT1RegisteredonFileNo] & [UT1Note] & [UT2toWhom] & [UT2forWhom] & [UT2forWhat] & [UT2PartsProp] & [UT2Date] & [UT2RegisteredonFileNo] & [UT2DischargeDate] & [UT2Note] & [UT3toWhom] & [UT3forWhom] & [UT3forWhat] & [UT3PartsProp] & [UT3Date] & [UT3RegisteredonFileNo] & [UT3DischargeDate] & [UT3Note] & [UT4toWhom] & [UT4forWhom] & [UT4forWhat] & [UT4PartsProp] & [UT4Date] & [UT4RegisteredonFileNo] & [UT4DischargeDate] & [UT4Note] & [FileFinished] & [FileOpened] & [FileDestroyed] & [EstimateDates] & [AccountDates] & [AccountPaidDates] & [FundsRequested] & [FundsReceived] & [StatOfLimitations] & [CrossRefFileName] & [CourtRecordNo] & [PapersFile] & [RelatedFiles] & [CourtListNo] & [Solicitorsforotherside] & [Status] & [InsuranceCo] & [ClaimsHandler] & [InsCoRef] & [Barrister] & [Notes]) AS part2, *
FROM ActiveFilesOldSystem;

Open in new window

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now