Results 1 to 13 of 13
  1. #1
    sunil@ is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    7

    Exclamation type error

    I have a table called transactions. i have created a form for data entry in which a field called "mon" where I set a [Event Procedure] before update for preventing duplicate entry in the table. What i am looking for... when I put a number in the "mon" field, it will check in the table that customerid (text field) and mon (numeric field) should not be pre existed. When i run the below code it appeared as "Type Mismatch",,,,, please anybody help me to settle the problem. Thanks

    Private Sub MONTH_BeforeUpdate(Cancel As Integer)
    Dim strMsg As String, strTitle As String
    Dim intStyle As Integer


    If DLookup("[customerid]", "[Transactions]", "[customerid]= '" & Me![CUSTOMERID] & "'" & " And " & "[mon] = " & Me![mon]) Then

    strMsg = "Entry will not be accepted, as the similar data is already existed in the table"
    strTitle = "DUPLICATE ENTRY"
    intStyle = vbOKOnly
    MsgBox strMsg, intStyle, strTitle


    Cancel = True
    End If
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    for DLookup, you have to put that as part of a logical test or assign to a variable. So like
    IF DLookup(...) = something (or use comparison operations such as >, < and so on).

    Or
    myVariable = DLookup( ... ) , then do a comparison or logical test against myVariable.

    Put another way, this
    DLookup("[customerid]", "[Transactions]", "[customerid]= '" & Me![CUSTOMERID] & "'" & " And " & "[mon] = " & Me![mon])

    returns something (let's assume it returns "ACME"). Your line now reads as

    If ACME Then
    which makes no sense, yes?
    Last edited by Micron; 04-13-2023 at 01:11 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Why not look up the syntax for the commands you want to use. Generally they have examples of use as well?
    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

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Is customerid numeric or string?

    Code:
    Private Sub MONTH_BeforeUpdate(Cancel As Integer)
    Do you have a field "Month"? Month is a reserved word.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    If you want to avoid doubles on he combination of 2 fields, the simplest and most sure way to do so is putting an unique index on the combination of those fields

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Agree. Using a compound index means no code required. Table field validation can have custom message too.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    sunil@ is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    7
    No, it is a text name only, my field name is mon (numeric)

  8. #8
    sunil@ is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    7
    Quote Originally Posted by Micron View Post
    for DLookup, you have to put that as part of a logical test or assign to a variable. So like
    IF DLookup(...) = something (or use comparison operations such as >, < and so on).

    Or
    myVariable = DLookup( ... ) , then do a comparison or logical test against myVariable.

    Put another way, this
    DLookup("[customerid]", "[Transactions]", "[customerid]= '" & Me![CUSTOMERID] & "'" & " And " & "[mon] = " & Me![mon])

    returns something (let's assume it returns "ACME"). Your line now reads as

    If ACME Then
    which makes no sense, yes?
    Sir, thank u for the reply ...... well sir can u please send the code that apply to your logic.... thanks

  9. #9
    sunil@ is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    7
    Quote Originally Posted by NoellaG View Post
    If you want to avoid doubles on he combination of 2 fields, the simplest and most sure way to do so is putting an unique index on the combination of those fields
    sir, there may be a same value in cutomerid field similarly in mon field also the values may be same, but my conditions is that there should not be commination of similar value in a row of a table. Exp. if your table is have row which contains coustomerid = "BB123" and mon = 3 then no new entry should allow for customerid = "BB123" & mon = 3.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    you probably want dcount, not dlookup.

    Code:
    If DCount("*", "[Transactions]", "[customerid]= '" & Me.[CUSTOMERID]  & "' And [mon] = " & Me.[mon]) > 0 then . . .
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by sunil@ View Post
    Sir, thank u for the reply ...... well sir can u please send the code that apply to your logic.... thanks
    If DLookup("[customerid]", "[Transactions]", "[customerid]= '" & Me![CUSTOMERID] & "'" & " And " & "[mon] = " & Me![mon]) > 0 Then

    In post 9 you seem to be saying that what you want is exactly what a compound index provides so I don't understand your comments. The data in a compound index behaves like this:

    A 1
    A 2
    A 3
    B
    1
    B 2
    B 3
    C 1
    C 2
    C 3
    B
    1
    <-- would not be allowed again
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    sunil@ is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2023
    Posts
    7
    Quote Originally Posted by Micron View Post
    If DLookup("[customerid]", "[Transactions]", "[customerid]= '" & Me![CUSTOMERID] & "'" & " And " & "[mon] = " & Me![mon]) > 0 Then

    In post 9 you seem to be saying that what you want is exactly what a compound index provides so I don't understand your comments. The data in a compound index behaves like this:


    A 1
    A 2
    A 3
    B
    1
    B 2
    B 3
    C 1
    C 2
    C 3
    B
    1
    <-- would not be allowed again
    Thank u sir, now it is working...........a big thanks and god bless you sir

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You're welcome. Glad you have a solution, although I'm not sure what that is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Intermittant #Type! type mismatch error.
    By Ranger351w in forum Programming
    Replies: 4
    Last Post: 09-27-2020, 07:53 PM
  2. Type 13 (Type Mismatch) error
    By sdel_nevo in forum Programming
    Replies: 5
    Last Post: 01-22-2016, 10:01 AM
  3. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  4. Error: Run-time error '13' Type mismatch
    By uronmapu in forum Access
    Replies: 1
    Last Post: 09-07-2012, 05:38 AM
  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