Results 1 to 5 of 5
  1. #1
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186

    Conditional IF and RecordSet

    Hi all Access friends,

    I have a question regarding usage of recordset and also conditional If'

    This is the first time I will use recordset (I am not an expert with this... my code is very wrong)

    What I want is that the recordset' search into the field "precode" from my table "NC_Main_Table" if this "precode data" existed previously...

    If the answer is "No" I want to create something like Consecutive_ID = 1, otherwise if this data exist in the table make something like.... Consecutive_ID = Consecutive_ID + 1

    '****************************
    Dim Clave As String
    Dim Zero As String


    Dim Year1 As String
    Dim Mes As String
    Dim Mes1 As String
    Dim Dia As String
    Dim Dia1 As String
    Dim BU As String

    Zero = "0"
    Year1 = Year(Date) - 2000
    Mes = Month(Date)
    Dia = Day(Date)
    BU = DLookup("[Code]", "[Line_Production]", "[LineProduction]= '" & Me.Line_Production & "'")

    If Mes < 10 Then
    Mes1 = Zero + Mes

    Else

    Mes1 = Mes

    End If

    If Dia < 10 Then
    Dia1 = Zero + Dia

    Else

    Dia1 = Dia

    End If

    Me.PreCode = "NC-" & BU & "-" & Year1 & Mes1 & Dia1

    'Now I want to create the consecutive number
    'First time using RecordSet is highly probable wrong....

    Dim db As Database
    Dim rs As DAO.Recordset
    Dim Consecutive_ID As String

    Set db = CurrentDb
    Set rs = db.OpenRecordset("Select * from NC_Main_Table Where PreCode = '" & Me.PreCode & "'", dbOpenDynaset)

    If rs.EOF = False Then
    rs.MoveLast
    rs.MoveFirst

    Do Until rs.EOF

    'FROM HERE I DON'T KNOW HOW TO CONTINUE....

    '******************************
    I Highly appreciate your support!!!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    While recordsets are vital to master to use Access at the fullest I need in your case would be an overkill.

    I think all you would need is this:


    '<<<<<<<<<<<<<<<<StartOfCode>>>>>>>>>>>>>>>>>>>>>> >>>>>>
    Dim sDate As String
    Dim BU As String
    Dim sPrecode As String
    Dim Consecutive_ID As Long




    BU = DLookup("[Code]", "[Line_Production]", "[LineProduction]= '" & Me.Line_Production & "'")
    sDate = Format(Date, "yymmdd")
    sPrecode = "NC-" & BU & "-" & sDate


    If DCount("*", "[NC_Main_Table ]", "[Precode]='" & sPrecode & "'") = 0 Then
    Consecutive_ID = 1
    Else
    Consecutive_ID = Consecutive_ID + 1 'here you can use a dMax to get the max of the consecutive_id from wherever you have it
    End If


    ''to use the recordset method uncomment the lines below
    'Dim db As Database
    'Dim rs As DAO.Recordset


    'Set db = CurrentDb
    'Set rs = db.OpenRecordset("Select * from NC_Main_Table Where PreCode = '" & Me.PreCode & "';", dbOpenDynaset)


    'If rs.RecordCount = 0 Then 'the recordset is empty meaning no precode present
    ' Consecutive_ID = 1
    'Else
    ' Consecutive_ID = Consecutive_ID + 1 'here you can use a dMax to get the max of the consecutive_id from wherever you have it
    'End If
    ''clean up
    'rs.Close
    'Set rs = Nothing
    'Set db = Nothing
    '<<<<<<<<<<<<<<<<EndOfCode>>>>>>>>>>>>>>>>>>>>>>>> >>>>


    For some reason the Code tags are broken for this post in the forum
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Wow!

    Good to see it.
    Question how do you use the dMax for this? I have never use before this function

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You use it just like dLookup - https://www.techonthenet.com/access/...omain/dmax.php

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  2. Replies: 6
    Last Post: 05-26-2013, 09:10 PM
  3. Insert Recordset with conditional statement
    By mjd973 in forum Programming
    Replies: 3
    Last Post: 04-29-2013, 06:20 AM
  4. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  5. Conditional Programming within a recordset
    By Bmw in forum Programming
    Replies: 2
    Last Post: 09-27-2010, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums