Page 1 of 4 1234 LastLast
Results 1 to 15 of 48
  1. #1
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145

    DMAX on [Criteria] Field

    I am using a DMAX function on VBA code from a Form:



    Me.NCRNumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1

    [NCRNumber] is in the tblNCR

    The problem is I'm trying to creating a criteria of where the function only looks up the "Type field" that contains words "NCR" from tblNCR. Please see picture below.

    Click image for larger version. 

Name:	2017-06-15 14_18_57-Access - QC checklist R1 Final_Backup 6-8-17 _ Database- C__Users_QEI_OneDri.png 
Views:	33 
Size:	69.8 KB 
ID:	29127

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Not sure what the question is, but it seems that you have a form that you want to add 1 to the NCRNumber if the "TYPE" field equals "NCR".

    Maybe this will help:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If [Type] = "NCR" And IsNull(Me.ncrnumber) Then
            Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
        End If
    End Sub


    NOTE: "Type" is a reserved word (it is a property) and shouldn't be used as an object name.

  3. #3
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    ssanfu,

    I want to search the crtieria in tblNCR for Type="NCR" and only add +1 of the max number of my NCRNumber for Type="NCR".

    So there's two Types of Forms I want to sequentially and incrementally +1 to depending on which Type of Form i select (CDR or NCR). Let's say the last Number on the Table for "NCR" was 6. Then If i select Type = "NCR" on my form, the NCR number should be 7 on the form.

  4. #4
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Click image for larger version. 

Name:	2.png 
Views:	31 
Size:	63.4 KB 
ID:	29128My form looks like this

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, how about adding the code to the "NType" combo box afterupdate event
    Code:
        If [Type] = "NCR" And IsNull(Me.ncrnumber) Then
            Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
        End If

    OR

    Code:
    Private Sub cboNType_Afterupdate()
        If IsNull(Me.ncrnumber) Then
            Select Case cboNType
                Case "NCR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
                Case "CDR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='CDR'"), 0) + 1
            End Select
        End If
    End Sub

  6. #6
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    ssanfu,

    Still doesn't work. There's no errors; however the number is not changing. Somehow the Criteria Part isn't working. DMAX works without the crtieria condition.

    Any other suggestions would be helpful. Here are some pictures.
    Click image for larger version. 

Name:	3.png 
Views:	31 
Size:	59.9 KB 
ID:	29129Click image for larger version. 

Name:	4.png 
Views:	31 
Size:	57.4 KB 
ID:	29130

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Can/would you post your dB?


    Maybe you have a default value of 0 for NCRNumber. How about
    Code:
    Private Sub cboNType_Afterupdate()    
        If NZ((Me.ncrnumber),0)= 0 Then
            Select Case cboNType
                Case "NCR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
                Case "CDR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='CDR'"), 0) + 1
            End Select
        End If
    End Sub

  8. #8
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by ssanfu View Post
    Can/would you post your dB?


    Maybe you have a default value of 0 for NCRNumber. How about
    Code:
    Private Sub cboNType_Afterupdate()    
        If NZ((Me.ncrnumber),0)= 0 Then
            Select Case cboNType
                Case "NCR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
                Case "CDR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='CDR'"), 0) + 1
            End Select
        End If
    End Sub

    Good morning ssanfu,

    I can send you my db via email? Is that okay?

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    just an observation. Your NCRNumber appears to be text (preceding zeros). If this is the case then you can't add+1 to it, you need to convert it to a number first, then change it back to text

    Me.ncrnumber = format(val(Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0)) + 1,"000")

    or perhaps

    Me.ncrnumber = format(Nz(DMax("val([NCRNumber])", "tblNCR", "[Type]='NCR'"), 0) + 1,"000")

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    one other observation - is the NType field a lookup field to another table? If so, then it may be it does not have a value of 'NCR' but is a FK to the other table

  11. #11
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Thanks Ajax for the quick response.

    NCRNumber is actually a Number formatted "000". NType does have a row source query to another table for either "NCR" or "CDR".

    Click image for larger version. 

Name:	5.png 
Views:	26 
Size:	25.0 KB 
ID:	29137

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    as you can see from your post, NType is a number - 'NCR' is not a number.

    You need to use

    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[nType]=1"), 0) + 1

    or whatever number 'NCR' is in the other table.

    It is best not to use lookups in table design, otherwise you end up with this sort of problem

  13. #13
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by Ajax View Post
    as you can see from your post, NType is a number - 'NCR' is not a number.

    You need to use

    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[nType]=1"), 0) + 1

    or whatever number 'NCR' is in the other table.

    It is best not to use lookups in table design, otherwise you end up with this sort of problem
    Thanks Ajax,

    It seems to work somewhat; however, it doesn't increment the number in which I select either CDR or NCR. It just increments to the next number. Here's a picture of tbl in which the row source is from for NType.

    Click image for larger version. 

Name:	6.png 
Views:	23 
Size:	11.5 KB 
ID:	29146

    Is it better to now use the row source query? But if I don't use it... how will the user determine to use a "NCR" or a "CDR" form... I'm basically trying to include both types into one form but with separate sequentially numbering for both.

  14. #14
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Nevermind. It's not working.. I forgot I still had my code active that didn't include the criteria part.

  15. #15
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    how will the user determine to use a "NCR" or a "CDR" form
    tables do not dictate how forms work - your form would still use a combo box

    you probably need to change this code

    Code:
    Private Sub cboNType_Afterupdate()    
        If NZ((Me.ncrnumber),0)= 0 Then
            Select Case cboNType
                Case "NCR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='NCR'"), 0) + 1
                Case "CDR"
                    Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[Type]='CDR'"), 0) + 1
            End Select
        End If
    End Sub
    to just

    Code:
    Private Sub cboNType_Afterupdate()   
    
        Me.ncrnumber = Nz(DMax("[NCRNumber]", "tblNCR", "[ID]=" & cboNType), 0) + 1
    
    End Sub

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

Similar Threads

  1. Replies: 12
    Last Post: 12-25-2015, 12:47 AM
  2. DMAx Question for Report - DMax <= Dtae
    By crimedog in forum Access
    Replies: 8
    Last Post: 12-29-2014, 09:31 PM
  3. Replies: 4
    Last Post: 05-08-2014, 11:46 AM
  4. DMax with LIKE in criteria
    By tylerg11 in forum Access
    Replies: 5
    Last Post: 07-26-2013, 10:39 AM
  5. Replies: 6
    Last Post: 07-24-2012, 03:02 PM

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