What is wrong? SQL error in VBA code?

behedwin

Active Member
Joined
Dec 10, 2014
Messages
399
Hi

I can not find what i am doing wrong with this code.
Access does not give me any error message... it just does not show the correct data that i expect.

I use this code to filter in a listbox.
the problem is with the row in bold. It is something there that is not correct and i cant figure out what and why...

Code:
  strSource = "SELECT Profile_Table.Profile_ID, Profile_Table.FirstName, Profile_Table.LastName, Profile_Table.Personnummer, Profile_Table.AnstalldSom, Profile_Table.JobbarPa, Profile_Table.WorkEndDate, Profile_Table.JobbProcent " & _
    "FROM Profile_Table " & _
    "Where (Profile_Table.Profile_ID Like '*" & Me.txtSearch.Text & "*' " _
    & "or FirstName Like '*" & Me.txtSearch.Text & "*' " _
    & "or LastName Like '*" & Me.txtSearch.Text & "*' " _
    & "or Personnummer Like '*" & Me.txtSearch.Text & "*' " _
    & "or Anstalldsom Like '*" & Me.txtSearch.Text & "*' " _
    & "or JobbProcent Like '*" & Me.txtSearch.Text & "*' " _
    & "or Anstallningsform Like '*" & Me.txtSearch.Text & "*' " _
[B]    & "or JobbarPa Like '*" & Me.txtSearch.Text & "*') AND ProfileArchived Is Null AND Anstallningsform = 'Tillsvidare (fast)' OR Anstallningsform = 'Visstid (tidsbegränsad)' "[/B]
      Me.ListPicker.RowSource = strSource
      Me.ListPicker.Requery
ProfileArchived is Null does not trigger... it still shows cells that do have data in them.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
if i create a working query that i want it looks like this in SQL

SELECT Profile_Table.Profile_ID, Profile_Table.ProfileArchived, Profile_Table.FirstName, Profile_Table.LastName, Profile_Table.Personnummer, Profile_Table.AnstalldSom, Profile_Table.JobbarPa, Profile_Table.Anstallningsform, Profile_Table.JobbProcent
FROM Profile_Table
WHERE (((Profile_Table.ProfileArchived) Is Null) AND ((Profile_Table.Anstallningsform)='Visstid (tidsbegränsad)' Or (Profile_Table.Anstallningsform)='Tillsvidare (fast)'));



But i cant make it work in VBA.

Why?
 
Upvote 0
I think i found a solution.
I enclosed the two OR statments in the bold text with ()

like this
Code:
  strSource = "SELECT Profile_Table.Profile_ID, Profile_Table.FirstName, Profile_Table.LastName, Profile_Table.Personnummer, Profile_Table.AnstalldSom, Profile_Table.JobbarPa, Profile_Table.WorkEndDate, Profile_Table.JobbProcent " & _
    "FROM Profile_Table " & _
    "Where (Profile_Table.Profile_ID Like '*" & Me.txtSearch.Text & "*' " _
    & "or FirstName Like '*" & Me.txtSearch.Text & "*' " _
    & "or LastName Like '*" & Me.txtSearch.Text & "*' " _
    & "or Personnummer Like '*" & Me.txtSearch.Text & "*' " _
    & "or Anstalldsom Like '*" & Me.txtSearch.Text & "*' " _
    & "or JobbProcent Like '*" & Me.txtSearch.Text & "*' " _
    & "or JobbarPa Like '*" & Me.txtSearch.Text & "*') [B]AND [U]([/U]Anstallningsform = 'Tillsvidare (fast)' OR Anstallningsform = 'Visstid (tidsbegränsad)'[U]) [/U]AND[/B] ProfileArchived Is Null "
      Me.ListPicker.RowSource = strSource
      Me.ListPicker.Requery

Now it seems to work :D
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top