Results 1 to 9 of 9
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828

    Question on a module, Public Function... Not sure how to word this,, Code is attached...

    Hi all,
    Merry Christmas

    I ran into something today and I found a way around it by just leaving a blank table "tblTransaction" but I have 6 forms that use this same code (Below)
    and just wondering if it is possible to add the following to this or should I have different MakeTranCodes() for each form....?

    Existing code is:
    Code:
    Public Function MakeTransCode() As String
    
    
        ' number from 000000 to 999999
        ' no 1s or 0s
        ' starts with T
        ' cant repeat
        
        Const MAXLOOP = 10000
        Randomize
        
        Dim Counter As Long
        Dim IsValid As Boolean
        Dim s As String
        Dim TransactionID As Long
        
        IsValid = False
        Counter = 0
        While Not IsValid And Counter < MAXLOOP
            Counter = Counter + 1
            IsValid = True
            s = "T" & Format(Int(Rnd * 1000000), "000000")
            If InStr(s, "0") <> 0 Or InStr(s, "1") <> 0 Then
                IsValid = False
            Else
                ' check if in table
                TransactionID = Nz(DLookup("TransactionID", "tblTransaction", "TransCode='" & s & "'"), 0)
                If TransactionID <> 0 Then IsValid = False
            End If
        Wend
        
        If Counter >= MAXLOOP Then
            MsgBox "WARNING: MakeTransCode Max Looped"
            Exit Function
        End If
        
        MakeTransCode = s
    
    
    End Function
    Code I want to replace the code in RED above with:


    Code:
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionDeposit", "TransCode='" & s & "'"), 0)
    
    
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionOpenBalance", "TransCode='" & s & "'"), 0)
    
    
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionPayment", "TransCode='" & s & "'"), 0)
    
    
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionPurchase", "TransCode='" & s & "'"), 0)
    
    
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionRF", "TransCode='" & s & "'"), 0)
    
    
    TransactionID = Nz(DLookup("TransactionID", "tblTransactionTransfer", "TransCode='" & s & "'"), 0)
    Sorry for poor description but i am not sure just how to ask this question....


    Thanks
    Dave

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Why not just have a table with an autonumber (always unique) , insert a new record and retrieve the value?
    You can set an autonumber to be random, rather than incremental if that helps in your process.

    You can wrap the whole thing into a function to generate a unique number.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Boolean variables instantiate as False by default and number variables instantiate as 0 by default. So the first lines setting these variables are not necessary. Just as did not need a line setting string variable to empty string - it already is by default.

    Exactly what did you have to work around? Why is leaving a blank table a solution? Not clear to me what you need.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Hi June7
    Not real sure even if I need anything as I am not clear on what that line of code does?
    'Check if in table
    Checking I would assume that it has a TransactionID to create a TransCode?
    I took out the table that was not used as couldnt use the same table for all the forms.... Gave me the same ID in each form...
    or should i say created a identical record in each form so i made different tables and when i removed the transaction table, then i
    got an error of coarse. I am not real sure I even need to check this or have that line in their...? I took a class and this was one part of it
    but i dont remember what or why this was done this way... I can just leave the blank table in there to eliminate the code error as my other forms
    work very well as long as that table is there. I jsut thought it strange and wondered if i could combine the others into that as AND, OR, IF, ElseIf, and so on
    but not sure how I would do it.
    Thanks

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Why not pass in the table name?
    However it seems your db is not normalized correctly?
    I would expect a field to identify what type of transaction it was, not a table for each?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    If you use the same function on every form you won't get a duplicate number.

    Create a table with a Autonumber - Lets call it tl_TransIDs and have two fields in it the AutoNumber and a date field, then simply call the function when you want an new unique ID

    Code:
    Public Function fnHeaderID() As Long
        
        Dim sSql As String
        Dim db As Database
        
        sSql = "INSERT INTO tl_TransIDs (TheDateFieldName ) Values (Now())"
        Set db = CurrentDb
        
        db.Execute sSql, dbSeeChanges
        fnHeaderID = db.OpenRecordset("SELECT @@IDENTITY")(0)
        Set db = Nothing
    
    
    End Function
    This will return the last autonumber inserted into the table. It will be unique.
    You can format it into something else for display purposes.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    Thank you Minty,
    I will try this method and after reading it over several times starting to see what it does, keep in mind I dont do this a lot...
    I do have a question, Is the ("SELECT @@IDENTITY")(0) something I replace with something or does that stay as is? I have
    never seen this nor used it before. Just want to make sure i understand before starting to use it.
    Thanks so much
    Dave

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    It stays as is. It is retrieving the autonumber ID just created by the db.Execute INSERT action.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    828
    thank you all,
    Have a great night and holiday season
    Will mark solved.
    Dave

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

Similar Threads

  1. Public variables in form module
    By pdanes in forum Forms
    Replies: 5
    Last Post: 10-22-2020, 02:53 AM
  2. Replies: 7
    Last Post: 05-11-2019, 11:24 AM
  3. Module Code Issue - Dlookup function
    By sarah1994 in forum Modules
    Replies: 1
    Last Post: 08-05-2018, 10:38 AM
  4. Replies: 5
    Last Post: 11-25-2017, 03:45 AM
  5. Replies: 5
    Last Post: 05-19-2015, 08:41 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