Solved

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

Posted on 2008-10-13
14
434 Views
Last Modified: 2012-05-05
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
Comment
Question by:BozM
  • 7
  • 4
  • 3
14 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22701982
If you run that query in access it works well ?
0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22701985
Have you run that query in access it works well ?
0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22702017
Sorry last post. Ignore it
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:BozM
ID: 22703480
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
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22706023
I don't think your query is correct.
Where do you get this ? "WHERE [FileNo] & [SeeFileNo] & [FinishedFileNo] & [GSFinFileBoxNo] & ... like '% .... " ?
0
 

Author Comment

by:BozM
ID: 22706132
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22716175
>> 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
 

Author Comment

by:BozM
ID: 22718577
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
 

Author Comment

by:BozM
ID: 22718590
Sorry, the code snippet didn't come out right. Only Line 2 should be commented out.
0
 

Author Comment

by:BozM
ID: 22718613
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22718783
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
 

Author Comment

by:BozM
ID: 22719006
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
 

Author Comment

by:BozM
ID: 22806837
Apologies for delay - this the database - definition only.
Experts-Exch-AFOS.mdb
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 22810903
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question