473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I retrieve multiple records from a VBA Query - I only retrieve 1 record

Expand|Select|Wrap|Line Numbers
  1. LSQL = "select NDC" & "," & "Quantity" & "," & "Date1 from Invoice where Date1 between #" & datefrom & "# and  #" & dateto & "#"
  2.  
  3.         Set Lrs = db.OpenRecordset(LSQL)
  4.                'Retrieve value if data is found
  5.         If Lrs.EOF = False Then
  6.             getndc = Lrs("NDC")
  7.                           getqty = Lrs("Quantity")
  8.                        getdate1 = Lrs("Date1")
  9.                          MsgBox (getndc & ",  " & getqty & ",  " & getdate1)
  10.  
  11.         Else
  12.             getndc = "Not found"
  13.         End If
  14.                 Lrs.Close
  15.         db.Close
  16.          Set Lrs = Nothing
I created a table with date ranges from 7/1/31 to 10/1/56
but the msgbox retrieves the 1st matching record and then stops
Apr 6 '11 #1
4 2428
what I did to retrieve more than one was re-write to this:
Expand|Select|Wrap|Line Numbers
  1. Dim datecount As Date
  2. For datecount = datefrom To dateto
  3.         'Create SQL statement to retrieve value from GST table
  4.         LSQL = "select NDC" & "," & "Quantity" & "," & "Date1 from Invoice where Date1 =" & "#" & datecount & "#" & " AND NDC =" & ndc1
  5.  
  6.  
  7.  
  8.  
  9.  
  10.         Set Lrs = db.OpenRecordset(LSQL)
  11.  
  12.  
  13.         'Retrieve value if data is found
  14.         If Lrs.EOF = False Then
  15.             getndc = Lrs("NDC")
  16.  
  17.             getqty = Lrs("Quantity")
  18.  
  19.             getdate1 = Lrs("Date1")
  20.  
  21.              MsgBox (getndc & ",  " & getqty & ",  " & getdate1)
  22.  
  23.         Else
  24.             getndc = "Not found"
  25.         End If
  26.         Next datecount
  27.  
  28.         Lrs.Close
  29.         db.Close
  30.          Set Lrs = Nothing
However it take a while to run through all the dates
Apr 6 '11 #2
Rabbit
12,516 Expert Mod 8TB
That's because you're creating a recordset for each date. Instead, use one recordset and loop through the recordset.
Apr 6 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
As rabbit said, loop through the records.
Example shown below:
Expand|Select|Wrap|Line Numbers
  1. Do while not LRS.EOF
  2.    'Your Code
  3.  
  4.    LRS.MoveNext
  5. Loop
Apr 6 '11 #4
I thought my
if
end if loops
but I will definitely gives this a try
Thanks
I'll let you know how it works :D


-------------------
Awesome works great!!!!
Thanks you so much !!!!!!!!!!!!
Apr 6 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: crispy | last post by:
On this page, you'll see that some PubID's are repeated several times: http://www.ihea.info/pages/website/test1.asp That's because some records have more than one attribute. For example, Title...
3
by: Fred | last post by:
I have a query that returns multiple identical records, however it should only return one. Indeed there is only one record for the OrderActionTypecode of 'P' yet there are two orderactions so it...
3
by: Wim Verhavert | last post by:
Hi all, I'm bothered with this question for weeks now.... Is it possible to edit multiple records at once using VBA? I have this continuous form and depending on the selection the user makes, I...
2
by: Jason | last post by:
I have a table of 650,000 records associated to contracts. Some of the contracts have multiple records but each contract only has one Active Record (there might be several inactive records). ...
1
by: Neil | last post by:
Hello everyone, I am making a program at the moment, and need HELP!!!. I'll start with the database, my field's r: fldSku - which a 6 digit number, no more no less (145682) fldDescription - which...
1
by: Verticon:: | last post by:
I have a situation where I need a table if bad items to match to. For example, The main table may be as: Table Main: fd_Id INT IDENTITY (1, 1) fd_Type VARCHAR(100) Table Matcher:...
9
by: Jeremy | last post by:
I have a situation where i am trying to run two query's and retrieve one record from each query, then using Union combine them into one recordset. The First Query is strait forward and should just...
1
by: Jordan M. | last post by:
Hi, Hoping to get some help modifying the following query that I have... TABLE: NAMES ID, FirstName, LastName TABLE: EMAILS ID,LinkID,Email,LastUpdateDate
1
by: ilangoraj | last post by:
I use below query to retrieve unmatch record SELECT * FROM Emp1 WHERE NOT exists (SELECT * FROM Emp2 WHERE Emp1.Eid=Eid and Emp1.Ename=Ename)
8
by: jmarcrum | last post by:
Hello all, i have a continuous form that displays about 1000 records and opens when I click a button. When the form opens, the user has the option of checking a checkbox that is located beside...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.