Important alert: (current site time 7/16/2013 1:34:59 AM EDT)
 

VB icon

A recordset distincts values from different fields

Email
Submitted on: 8/26/2003 10:33:42 AM
By: Manos Emmanouhl 
Level: Beginner
User Rating: Unrated
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VBA MS Access
Views: 18548
 
     Find distinct values and sum the results into a diferent field the difference is you don't have to waste the other fields!! it will report the last records found
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A recordset distincts values from different fields
' Description:Find distinct values and sum the results into a diferent field
the difference is you don't have to waste the other fields!!
it will report the last records found
' By: Manos Emmanouhl
'
' Returns:returns a fine recordset .. but you have to do updatebatch if you wana save it to the database
... i only wanted to print it..
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=48004&lngWId=1'for details.'**************************************

Public recSelect As New Recordset
Public recbck As New Recordset
Public Function findclearp()
Dim recSelect1 As String
Dim recSelect2 As String
Dim recSelectbck As Integer
Dim n As Integer
Dim nn As Integer
Dim hh As Integer
Dim tt As Integer
Dim tt1 As Integer
Dim tt2 As Integer
n = 0
hh = 1
recSelect.MoveLast
recSelectbck = recSelect.RecordCount
recSelect.Close
recSelect.LockType = adLockBatchOptimistic
recSelect.Open
recSelect1 = ""
recSelect2 = ""
recSelect.MoveLast
'MsgBox recSelectbck
For tt1 = 1 To recSelectbck
'select whatever fields you want here
recSelect1 = recSelect![barcode]
recSelect2 = recSelect![foodid]
n = n + 1
For tt2 = 1 To recSelectbck
recSelect.MovePrevious
If recSelect.BOF = True Then GoTo eendofile2
'select whatever fields you want here
If recSelect1 = recSelect![barcode] And recSelect2 = recSelect![foodid] Then
recSelect.Delete
hh = hh + 1
recSelectbck = recSelectbck - 1
If recSelectbck <= 1 Then GoTo eendofile
End If
Next tt2
eendofile2:
nn = n - 1
recSelect.MoveLast
For tt = 1 To nn
If recSelect.BOF = True Then GoTo eendofile
recSelect.MovePrevious
Next tt
'then sum the results into a field
recSelect![monada] = hh
hh = 1
recSelect.MoveLast
For tt = 1 To n
If recSelect.BOF = True Then GoTo eendofile
recSelect.MovePrevious
Next tt
If recSelect.BOF = True Then GoTo eendofile
'recSelect.MoveNext
Next tt1
eendofile:
'make a clone in case you want to close the first recordset
Set recbck = recSelect.Clone
End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/27/2003 4:17:35 AM

i'd like to report a "bug"
replace the :
If recSelectbck <= 1 Then GoTo eendofile
with
'If recSelectbck <= 1 Then GoTo eendofile
cause if you have only one record it will report 0 quantity ...
;) peace .....
(If this comment was disrespectful, please report it.)

 
8/28/2003 5:22:29 AMkmtai

Could you exlain more about the 'bug' ?

is it need
if recSelectbck <= 1 Then
GoTo eendofile
else
....
endif


(If this comment was disrespectful, please report it.)

 
8/28/2003 6:43:33 AM

just del this "if recSelectbck <= 1 Then
GoTo eendofile"
cause if there is only one barcode
it will find null quantity..
(If this comment was disrespectful, please report it.)

 
9/12/2003 1:28:23 AMManos Emmanouhl

ok this was my first serious loop in vb .. it is nice but my supervisor didn't like it .. he thought that the extra fields didn't need .. so by accident i discovered another way to get results from specific fields without doing alot of work .. in sql statements ..
sqltext3 = " select idnum, barcode, foodid , date ,time ,etc from etc where etc < 67 order by date , time "
sqltext4 = " select distinct foodid , count(*) from ('" & sqltext3 & "')"
this does a lot of work with no trouble
(If this comment was disrespectful, please report it.)

 
11/23/2003 8:20:26 PM

Its good
(If this comment was disrespectful, please report it.)

 
11/27/2003 10:48:16 AMManos Emmanouhl

its good ... then code or the sql statements ?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.