Access Query

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,424
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
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,689
Office Version
365
Platform
Windows
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:

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
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:

Kamolga

Well-known Member
Joined
Jan 28, 2015
Messages
1,175
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:

Watch MrExcel Video

Forum statistics

Threads
1,102,624
Messages
5,487,940
Members
407,616
Latest member
MichaelaL

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top