select records with Unique values in certain fieldsth (1 Viewer)

ootkhopdi

Registered User.
Local time
Today, 13:00
Joined
Oct 17, 2013
Messages
181
Hi..
i have a database in which in one table having employee posting details..
like . where he works in which month or year..

table view as
month year posting place
jan 2016 A
feb 2016 A
march 2016 B
April 2016 B
.....
Nov 2016 C
..........
Feb 2017 A
.......
Sep 2017 C

as you see. employee changes his posting palaces..
so i want result as
Month Year posting Place
Jan 2016 A
March 2016 B
Nov 2016 C
Feb 2017 A
Sep 2017 C

it mean i want only that months when employee data value change in posting place..

thanks in advance
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
I think this will do it..
You have a table called "tblLastPost" with the following Fields ID, MTH, YEAR, POST, Flg,
I have added an extra field called "flg" in boolean format... This field will be marked as the record that was the first time of posting...

Code:
Public Sub fRSL_LastPost()
'RSL = Record Set Loop
Dim strSubName As String
Dim strModuleName As String

strSubName = "fRSL_LastPost"
strModuleName = "Form - " & Me.Name
'strModuleName = "basModNameHere"

On Error GoTo Error_Handler
    
    Dim curDB As DAO.Database
    Dim rst As DAO.Recordset

    Set curDB = CurrentDb
    

'SELECT ID, MTH, YEAR, POST, Flg
'FROM tblLastPost


Dim strSQL_RSL As String
strSQL_RSL = "SELECT ID, MTH, YEAR, POST, Flg FROM tblLastPost"

    Dim strLastPost As String
    Dim lngCounter As Long
   
            Set rst = curDB.OpenRecordset(strSQL_RSL, dbOpenDynaset)
               
                    Do Until rst.EOF
                        
                        'First Pass Only
                        If lngCounter = 0 Then
                        
                            rst.Edit
                            rst!Flg = True
                            rst.Update
                            strLastPost = rst!POST
                        
                        Else
                        
                            If rst!POST <> strLastPost Then
                                rst.Edit
                                rst!Flg = True
                                rst.Update
                                strLastPost = rst!POST
                            End If
                            
                        End If
                        lngCounter = lngCounter + 1
                        rst.MoveNext
                    Loop
    
Exit_ErrorHandler:
        rst.Close
    Set rst = Nothing
    Set curDB = Nothing
    
    Exit Sub

Error_Handler:  'Version - 1a
    Dim strErrFrom As String
    Dim strErrInfo As String
        
        strErrFrom = "Error From:-" & vbCrLf & strModuleName & vbCrLf & "Subroutine >>>>>>> " & strSubName
        strErrInfo = "" & vbCrLf & "Error Number >>>>> " & Err.Number & vbCrLf & "Error Descscription:-" & vbCrLf & Err.Description
            
            Select Case Err.Number
                 Case 1 'When Required, Replace Place Holder (1) with an Error Number
                    MsgBox "Error produced by Place Holder please check your code!" & vbCrLf & vbCrLf & strErrFrom & strErrInfo
                Case Else
                    MsgBox "Case Else Error" & vbCrLf & vbCrLf & strErrFrom & strErrInfo
            End Select
        Resume Exit_ErrorHandler

End Sub     'fRSL_LastPost
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:30
Joined
May 7, 2009
Messages
19,226
put this in a Standard Module:

NOTE: change the name of table and fields to
the correct name of fields and table of your database.

Code:
Public Function fnLocation() As String
    Dim db As dao.Database
    Dim rs As dao.Recordset
    Dim strlocation As String

    Set db = CurrentDb
    Set rs = db.OpenRecordset("select [Year] & [Month], [posting place] from yourTable;", dbOpenSnapshot)
    With rs
        If Not (.BOF And .EOF) Then .MoveFirst
        strlocation = rs(1)
        fnLocation = rs(0)
        .MoveNext
        While Not .EOF
            If strlocation <> rs(1) Then
                strlocation = rs(1)
                fnLocation = fnLocation & "/" & rs(0)
            End If
            .MoveNext
        Wend
        .Close
    End With
    Set rs = Nothing
    Set db = Nothing
End Function


now create a query based on your table:

SELECT yourTable.Month, yourTable.Year, yourTable.[posting place]
FROM yourTable where Instr(1,fnLocation(),[Year] & [Month])>0
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:30
Joined
Feb 19, 2002
Messages
43,196
Tables and queries are unordered sets. If you don't apply a specific sort order, there is no way to ensure any particular order. Access has a tendency to fool us because when we compact the database, each table gets rewritten in PK order. If the data shown was actually written in PK order AND NO UPDATES to the table were made, then and only then Access would probably return the records as expected. SQL Server, probably not simply because server based relational databases use very different methods of retrieval. However, if any entries were made out of PK order or any updates were done to any records, all bets are off.

The reason that record updates affect sequence is because Access treats all text columns as variable length. Each record is placed on the disk one record immediately following the other. If you update record #385 and make it larger than it was when it was last written, Access can't save it back where it was so it writes a pointer and puts the actual record after the last record in the physical table space. So now record #385 comes after #718 and if you add a new record, it comes after #385 instead of after #718.

I didn't examine either method so I can't tell you which will work better but both need to be modified to use an order by clause to ensure the data is in the correct order.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Thanks Pat! I was going to add the Order By clause, but 'aven't 'ad any feed back from the OP so I didn't bother!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:30
Joined
Feb 19, 2002
Messages
43,196
I spoke up because this is a subtle bug. It could show up immediately but more likely would lurk for months or years if the BE is regularly compacted just because the BE is Jet/ACE and if there's a lot of data, no one is ever going to examine it all.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Hi Pat, I appreciate you mentioning it, I'm surprised you saw it - I don't think many people would have! The other thing I haven't done is beginning of and ending of file, I don't know if that's necessary anymore? I'm not 100% sure.
 

ootkhopdi

Registered User.
Local time
Today, 13:00
Joined
Oct 17, 2013
Messages
181
Thanks to All but i can't Understand what is the code and where i post it...

please give me simple steps..
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Do you have a table like this:-

Fields:-
ID, MTH, YEAR, POST, Flg,



Sent from my SM-G925F using Tapatalk
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:30
Joined
May 7, 2009
Messages
19,226
What is the actual Name of your Table and the Actual Names of the fields.
Problem, always is OPs don't give the real names of their table and fieldnames at first, but they want real solution at once.


If you can give the real names then when i use them in the code, you will likely to follow it and gives you idea of what the function is doing.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Problem, always is OPs don't give the real names of their table and fieldnames at first, but they want real solution at once.

LOL Ariel, I've noticed in the last couple of years, the interaction with OP's has deteriorated! It has become more like:- "I have this problem fix it and give me the solution"...

It used to be; "I've got this problem, I've tried this, I've tried that and a now I'm stuck! Can you steer me in the right direction please!

There is a world of difference between the two.

The only thing that has changed that may well be contributing to this problem is that we now have social media. In other words people expect you do their bidding! I can't think of anything else that's caused it.

I have adopted a new strategy to answering question, I don't give too much away, too quickly, too easily. I encourage the OP to work a little bit for the solution, as a bare minimum, answer a few questions ....
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Really it boils down to "our purpose" I don't think the purpose is to do people's work for them. I think the purpose is to educate them so they can do the work for themselves. It's the same old story; "Give a man a fish & he eats for a day. Teach a man to fish and he eats for life!
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:30
Joined
Jul 9, 2003
Messages
16,268
Hi Pat, I appreciate you mentioning it, I'm surprised you saw it - I don't think many people would have! The other thing I haven't done is beginning of and ending of file, I don't know if that's necessary anymore? I'm not 100% sure.

Just tidying up the thread. I mentioned BOF & EOF, I've just done a little refresher by Reading Allen Browne's article here:- VBA Traps: Working with Recordsets ... I'm still none the wiser! The main problem is if you feed it an empty recordset then you'll probably get an error. There are various ways of testing for this... See ~Allen Browne~
 

plog

Banishment Pending
Local time
Today, 02:30
Joined
May 11, 2011
Messages
11,634
LOL Ariel, I've noticed in the last couple of years, the interaction with OP's has deteriorated! It has become more like:- "I have this problem fix it and give me the solution"......Really it boils down to "our purpose" I don't think the purpose is to do people's work for them.


I agree, but I don't blame the initial posters entirely. Most of this lies at the feet of respondents who simply post undocumented code without trying to find out the big picture issue or determine what the initial posters has done. Or if the solution they seek is what they truly need.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:30
Joined
Feb 19, 2002
Messages
43,196
I'm still none the wiser!
Allen's article doesn't address this issue.

It's something that should be obvious if you understand that tables and queries are unordered sets. The problem is that most people have never read enough about Relational Database theory to obtain that fact and are simply fooled by Access as I mentioned. However, you can prove the problem exists by doing what I said earlier. You just have to create a large enough recordset to span multiple sectors so you can force Access to have to write an updated record at the end of the table space rather than back where it was originally. Anyone who has used SQL Server or other RDBMS would have seen this behavior fairly frequently so they would always know that sorting is necessary if sequence is relevant as it would be on a form or report or in some batch processes.

If you are creating a process that relies on a specific record order, then you MUST use an Order By clause on a query (Order By is not available on a table) to to ensure the data is in the order you need for processing.
 

Users who are viewing this thread

Top Bottom