Access Query

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,598
Office Version
  1. 365
Platform
  1. Windows
I am trying to return a single record from an Access table but I am struggling to return the specific record

I have been using
Code:
strQuery = "SELECT Agent FROM SageData WHERE InvDate = #" & Format(dtDate, "dd/mm/yyyy") & "# AND BoxRef = '" & rngCell.Offset(intCount, 0) & "'"
rsQuery.Open "SageData", cnConnection, adOpenKeyset, adLockOptimistic
but this returns all the records in the table.

When I have reduced the query down to
Code:
strQuery = "SELECT Agent FROM SageData WHERE Supplier = 'CNG'"
it is still returning all the records despite there only being a few in the table.

I can't work out what isn't right as I use this command quite a lot.


Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You are building the query string (strQuery), but I do not see you using it in the open query command.
I think you actually need to assign that SQL code to your rsQuery object. Otherwise, the query string you just built is in no way related to rsQuery.

Assuming rsQuery is defined as a query object, maybe something like this:
Code:
strQuery = "SELECT Agent FROM SageData WHERE InvDate = #" & Format(dtDate, "dd/mm/yyyy") & "# AND BoxRef = '" & rngCell.Offset(intCount, 0) & "'"
[COLOR=#ff0000]rsQuery.SQL = strQuery[/COLOR]
rsQuery.Open "SageData", cnConnection, adOpenKeyset, adLockOptimistic
 
Last edited:
Upvote 0
Looks like you are building a query string then just ignoring it and asking for the table to be returned:

Code:
[COLOR=#333333]rsQuery.Open "[/COLOR][COLOR=#FF0000]SageData[/COLOR][COLOR=#333333]", cnConnection, adOpenKeyset, adLockOptimistic[/COLOR]

try:
Code:
[COLOR=#333333]rsQuery.Open [/COLOR][COLOR=#333333]strQuery[/COLOR][COLOR=#333333], cnConnection, adOpenKeyset, adLockOptimistic[/COLOR]
 
Last edited:
Upvote 0
SELECT Agent FROM SageData WHERE Supplier = 'CNG'
should give you the list of Agent that have CNG as supplier. You might have duplicate agents because every time it reads CNG in supplier, it gives the 'agent' value...not all the records but all the records with CNG in supplier column.
Code:
[LEFT][COLOR=#333333][FONT=monospace]SELECT Agent 
FROM SageData 
WHERE Supplier = 'CNG'
[/FONT][/COLOR][/LEFT]GROUP BY [COLOR=#222222][FONT=Verdana]Agent[/FONT][/COLOR]
should give you the list of unique agents that have CNG in the column Supplier (reduced list).

If it is not the case, then I do not get it
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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