Solved

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

Posted on 2008-10-13
14
438 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 2 50
SQL Use Distinct with two fields 3 20
SQL Select in Access 2003 3 42
How to add a suffix to a value in a column based on the value in another column 4 30
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

685 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