Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13

    Manually autonumber a field

    Hi,



    I have a table with some fields, but one of this fields (id_seq) I need to manually autonumber it, based on another field (company). The "id_seq" field is numbered in a different sequence based on what is in the field "company".
    Exemple: id_seq company
    1 aa
    2 aa
    1 bb
    The next "id_seq" for company "bb" should be "2".

    Thanks,
    Paulo Ferreira

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    you cannot duplicate an autonumber field. you'll have to do it through form code or something and make it a number of some sort, be it a long, or something else.

  3. #3
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    I don't want to duplicate an autonumber field. What I want is create a field to have my own autonumber based on the content of the field "company".

    Paulo Ferreira

  4. #4
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    You'll have to use either VBA Code or an extra Query.

    You can count the number of Records returned for that company and just add 1.

    Code:
      Dim rstCount as DAO.Recordset
    
      Dim nbrNextNum as Integer ' Next available "autonumber"
    
      Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM MyTable WHERE [company]='bb'", dbOpenSnapshot)
    
      nbrNextNum = rstCount.RecordCount + 1
    
      rstCount.Close
    
      Set rstCount = Nothing

  5. #5
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    Hi Rawb.

    Your code returns the error "
    Too few parameters. Expected 1".
    I know that it's because I replaced "bb" with a field that is in my form, but I don't know how to do it to avoid the error.
    I'm sure that it is a simple thing to workout but I realy don't know how.
    Help please!

    Paulo Ferreira

  6. #6
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    That error usually means there's something wrong with your Query. Either the Table name or one of the Field names is misspelled.

    If you're familiar with SQL, just go back and double-check the Query for spelling errors and the such.

    If you're not familiar with SQL but know how to use Access' Query Builder, put together a Query that returns all the entries for a single company. Then switch to the SQL View and just copy and paste that Query into the VBA Code.

    Some quick pointers for checking your Query: "SELECT * FROM MyTable WHERE [company]='bb'"

    • MyTable - The name of your Table in Access. If your Table's name has a space in it, make sure to enclose it in square brackets (i.e.: [My Table])
    • [company] - The name of the field containing the company's ID or name. If the field is of any numeric type (Integer, Double, etc.) DO NOT use single quotes to encapsulate the value ([company]=3 instead of [company]='3').

  7. #7
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    Hi Rawb,

    I think I don't make my self clear in the last post.
    If the code is: Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM MyTable WHERE [company]=10", dbOpenSnapshot), where the "company" is a number, it works ok.
    If the code is: Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM MyTable WHERE [company]=FormFieldFromComboBox", dbOpenSnapshot), where the "company", it wont work and the error "Too few parameters. Expected 1" will show up.
    I know that's because I'm not writing the FormFieldFromComboBox the right way, but I already tried some different ways and it always gives me the same error.

  8. #8
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Ah, I gotcha.

    When including a Form field in code like that, you need to make sure it's outside of any quotes. If you enter it like this though, it should work.

    Code:
    Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM MyTable WHERE [company]=" & Me!FormFieldName, dbOpenSnapshot)
    The ampersand (&) is used to combine two text strings together in VBA, so what we're doing is taking the text string SELECT * FROM MyTable WHERE [company]= and adding the value contained in Me!FormFieldName to the end of it.

  9. #9
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    Hi,

    Still got an error.
    Runtime error 3075: Missing operator ([company]=).
    Once again I don't understand why.

  10. #10
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Honestly, I'm not sure what the problem is at this point.

    Can you post a screenshot of the line that's highlighted? That will probably help.

  11. #11
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    Hi Rawb,

    Here is the complete line that is highlighted: Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM hardware WHERE [company]=" & Me!Text341, dbOpenSnapshot).
    The complete error message is: Run-Time Error '3075': Syntax Error(Missing Operator) in query expression '[company]'.

  12. #12
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13
    One thing about that line.
    The field "text341" is filled based on a column of a combo box. If I replace that field with a number that corresponds to one of the companies that are in the combo box, the code works ok.

    Paulo Ferreira

  13. #13
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Try adding the following line of code just before the line that gives you the error. This code will show a popup message box with the SQL Query that's giving you problems.

    Since this is from a Combo Box, it could be that the wrong column is bound to it. You might also want to double-check the "Control Source" of the Combo Box as well as the "Bound Column" property.

    Code:
      MsgBox "SELECT * FROM hardware WHERE [company]=" & Me!Text341 & _
             vbCrLf & vbCrLf & "Me!Text341 = '" & Me!Text341 & "'"

  14. #14
    apsf68 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Location
    Portugal
    Posts
    13

    Solution I found

    Hi Rawb,

    I know now that "run-time error 3075" appends because "text341" was null when I choose a company, and it was null because I was using event "before insert". When I change it to "after update" it works fine (almost).
    The problem then was the new number for "id_seq" was never updated every time I choose a different company.
    So I have used this code:
    Dim newvalue As Long
    Dim maxvalue As Variant
    maxvalue = DMax("id_seq", "hardware", "[company]=" & Me!Text341)
    If IsNull(maxvalue) Then
    maxvalue = 0
    End If
    newvalue = maxvalue + 1
    Me!id_seq = newvalue

    Any way, if you have any suggestions to your code, please let me know.

  15. #15
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Because I didn't have any data on your Form or Table setup, there was no way for me to present a working solution. All my above code was, was an example on how to easily capture the value you're looking for. It would have been up to you to modify it for your specific needs.

    That said, I now know enough to give you a working copy:
    Code:
      Dim rstCount as DAO.Recordset
    
      Set rstCount = CurrentDb().OpenRecordset("SELECT * FROM MyTable WHERE [company]='bb'", dbOpenSnapshot)
    
      Me!id_seq = rstCount.RecordCount + 1
    
      rstCount.Close
    
      Set rstCount = Nothing
    Additionally, the functions DMax, DCount, DLookup, etc. should only be used when there is no other alternative. Those functions are slow and don't use proper garbage collection. If you're working with Form or Report Fields, they're OK to use, but in VBA code there's always a better solution.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Customizing the Autonumber field
    By wasim_sono in forum Access
    Replies: 3
    Last Post: 10-24-2014, 03:00 PM
  2. Manually changing attribute
    By michaeljohnh in forum Programming
    Replies: 4
    Last Post: 09-14-2010, 12:50 PM
  3. Exception When Executing In Java But Runs In MS Access DB Manually
    By vasuresh.kishor@gmail.com in forum Access
    Replies: 0
    Last Post: 03-11-2010, 12:22 AM
  4. Autofill field based on autonumber field
    By frevilla in forum Forms
    Replies: 1
    Last Post: 09-11-2009, 02:50 AM
  5. Problems with autonumber field in a form
    By admaldo in forum Forms
    Replies: 0
    Last Post: 02-25-2008, 11:09 AM

Tags for this Thread

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