Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237

    Runtime error 13 type mismatch

    I have code to auto Generate the Next Number, Im trying to put a prefix "J" in front of the number to get an outcome J1264 when generated, the field is text

    I formated the field



    "J"0000 but get a runtime error 13

    the code be hind my Generate button is -

    Private Sub cmdGenerateJobNo_Click()
    If Me.NewRecord = True Then
    Me.txtJobNumber = Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1
    End If
    End Sub

    do i need more code for this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    The format property setting works?

    Try using apostrophe delimiters.

    Me.txtJobNumber = "'" & Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1 & "'"
    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.

  3. #3
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by June7 View Post
    The format property setting works?

    Try using apostrophe delimiters.

    Me.txtJobNumber = "'" & Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1 & "'"
    No it didn’t.... the run time error was with using the format setting.

    thanks june7 I’ll let you know how I go

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,778
    Rather than trying to apply a format to the control, why not just create and assign the needed value since you are using code anyway? Something like

    Code:
    Dim lngNextNum As Long
    
    lngNextNum = Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1
    Me.TextJobNumber= "J" & Format(lngNextNum, "0000")
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Micron View Post
    Rather than trying to apply a format to the control, why not just create and assign the needed value since you are using code anyway? Something like

    Code:
    Dim lngNextNum As Long
    
    lngNextNum = Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1
    Me.TextJobNumber= "J" & Format(lngNextNum, "0000")

    Yea Micron i would prefer code as i'm already there with the code just wasn't sure how to write it, thanks that code worked a treat.

  6. #6
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by June7 View Post
    The format property setting works?

    Try using apostrophe delimiters.

    Me.txtJobNumber = "'" & Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1 & "'"
    cheers June7, Microns code did it the job

  7. #7
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by Micron View Post
    Rather than trying to apply a format to the control, why not just create and assign the needed value since you are using code anyway? Something like

    Code:
    Dim lngNextNum As Long
    
    lngNextNum = Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1
    Me.TextJobNumber= "J" & Format(lngNextNum, "0000")
    Micron i was a bit premature... this code works great the very first time it is used but if there is a job number before it with a J say J2364 then the error comes up again.

    Private Sub cmdGenerateJobNo_Click()
    Dim lngNextNum As Long


    lngNextNum = Nz(DMax("JobNumber", "tblJobDetails"), 0) + 1
    Me.txtJobNumber = "J" & Format(lngNextNum, "0000")


    End Sub

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this:

    Create a table named "tblNextNum".
    It has one field "NextNumber" of type Number - Long Integer.

    Add the max number from the field "JobNumber" in table "tblJobDetails". If the largest jobnumber is J2364, then enter 2364 in the table.

    Then modify your code:
    Code:
        Dim lngNextNum As Long
    
        lngNextNum = Nz(DMax("NextNumber", "tblNextNum"), 0) + 1    'gets the number from the new table
        Me.TextJobNumber = "J" & Format(lngNextNum, "0000")
        CurrentDb.Execute "Update tblNextNum Set NextNumber = " & lngNextNum, dbFailOnError  ' updates the NextNumber to the current lngNextNum

  9. #9
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by ssanfu View Post
    Try this:

    Create a table named "tblNextNum".
    It has one field "NextNumber" of type Number - Long Integer.

    Add the max number from the field "JobNumber" in table "tblJobDetails". If the largest jobnumber is J2364, then enter 2364 in the table.

    Then modify your code:
    Code:
        Dim lngNextNum As Long
    
        lngNextNum = Nz(DMax("NextNumber", "tblNextNum"), 0) + 1    'gets the number from the new table
        Me.TextJobNumber = "J" & Format(lngNextNum, "0000")
        CurrentDb.Execute "Update tblNextNum Set NextNumber = " & lngNextNum, dbFailOnError  ' updates the NextNumber to the current lngNextNum
    is the reason im creating a new table to see if the existing one has errors in it?

    ill let you know how i go.

  10. #10
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    ssanfl it works to a degree, the only problem is if i cancel the job the job number still increases if that makes sense e.g. if i cancel job j2365 half way through putting info in, when i put the next job in instead of it being j2365 its j2366 and so on. with my original dmax code it works as it should.....

    id like to use code, i am thinking just adding a another field called JobReference set the format setting to J and then combine them on the form.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    If the J is saved in field then must extract it before incrementing number part then concatenate it back.

    Or use Steve's approach.

    Or don't save the J and set InputMask to display J on forms and an expression to concatenate it on reports.
    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.

  12. #12
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by June7 View Post
    If the J is saved in field then must extract it before incrementing number part then concatenate it back.

    Or use Steve's approach.

    Or don't save the J and set InputMask to display J on forms and an expression to concatenate it on reports.
    id like to save the J if i can but inputmask could be the option, thanks.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Consider:

    "J" & Format(Nz(Mid(DMax("JobNumber", "tblJobDetails"),2)+1,1),"0000")

    Is this a multi-user db? There is risk simultaneous users can generate same ID. You might want to create the ID when record is committed. Do users need to see this ID during data entry?
    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.

  14. #14
    ShaunG is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jan 2018
    Posts
    237
    Quote Originally Posted by June7 View Post
    Consider:

    "J" & Format(Nz(Mid(DMax("JobNumber", "tblJobDetails"),2)+1,1),"0000")

    Is this a multi-user db? There is risk simultaneous users can generate same ID. You might want to create the ID when record is committed. Do users need to see this ID during data entry?

    is a multi-user db and yeah does have advantages with seeing ID at data entry.

    would that code go in the same as the previous dmax code?

    Private Sub cmdGenerateJobNo_Click()

    If Me.NewRecord = True Then

    Me.txtJobNumber = "J" & Format(Nz(Mid(DMax("JobNumber", "tblJobDetails"),2)+1,1),"0000")

    End If

    End Sub

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Yes, but then you need to commit the record immediately so another user will not create the same ID. This means if you cancel the input you will have an incomplete record in table or delete record and have gap in sequence. In my db I could not allow gap - every ID had to be accounted for. I have code in Cancel button that reverses user inputs and saves this 'blank' record. Then when another user starts a record, code first searches for blank record and opens it for user input. If no blank found then new ID and record created. I also have a void procedure so established records that we decide do not need to be further processed will be reported as VOIDED. DB is to report construction lab sample testing data.
    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.

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

Similar Threads

  1. ADO Parameter Runtime 13: Type Mismatch
    By ASWilliams in forum Programming
    Replies: 3
    Last Post: 06-02-2016, 10:02 AM
  2. Type 13 (Type Mismatch) error
    By sdel_nevo in forum Programming
    Replies: 5
    Last Post: 01-22-2016, 10:01 AM
  3. Replies: 2
    Last Post: 08-24-2015, 09:14 PM
  4. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  5. Replies: 1
    Last Post: 05-11-2012, 10:59 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